Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for random spec ordering when running tests #2908

Open
joelzimmer opened this issue Dec 6, 2018 · 12 comments
Open

Allow for random spec ordering when running tests #2908

joelzimmer opened this issue Dec 6, 2018 · 12 comments
Labels
stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist

Comments

@joelzimmer
Copy link

Current behavior:

When running specs in cypress, the files run in alphabetical order, and tests within each spec run in order. See #2901.

Desired behavior:

It'd be great to pass a flag into cypress (or for it to do some sort of optimization like Jest does) to randomize the order of test runs. This would allow tests that rely on other state to fail and could help increase confidence in how the app works.

Versions

3.1.3, Alpine Linux, Electron

@jennifer-shehane jennifer-shehane added type: feature New feature that does not currently exist external: api stage: proposal 💡 No work has been done of this issue labels Dec 7, 2018
@Strajk
Copy link
Contributor

Strajk commented Apr 16, 2019

Just noticed there is a library for randomizing tests in Mocha by the almighty @bahmutov, would be great to integrate it to Cypress :)

https://github.com/bahmutov/rocha

@bahmutov
Copy link
Contributor

bahmutov commented Apr 16, 2019 via email

@hcharley

This comment has been minimized.

@heathd

This comment has been minimized.

2 similar comments
@mxygem

This comment has been minimized.

@sumitngupta

This comment has been minimized.

@terencechow
Copy link

terencechow commented Feb 25, 2020

+1

If you share where the location is in this repo that grabs spec files I can take a stab at this. Would it be enough to:

  1. randomize the spec files order
  2. use https://github.com/bahmutov/cypress-select-tests to randomize the tests within the spec files?

Or is there another reason that randomizing spec file order might break things? (Does something depend on spec file ordering?)

@mncharlton
Copy link
Contributor

I've had a go at randomising the order of the tests (it blocks) within a spec, it's not perfect yet, but is a start - https://www.npmjs.com/package/cypress-random-test-order

@jennifer-shehane
Copy link
Member

@mncharlton Could you open a pull request to add this plugin to our documentation?

Instructions here as well as a PR Template Checklist. Thanks!

@mncharlton
Copy link
Contributor

Thanks for the prompt @jennifer-shehane! PR is here: cypress-io/cypress-documentation#3024

@duraz0rz
Copy link

Any other movement on this? Seems like the cypress-random-test-order plugin is deprecated by the owner as they don't have time to work on issues.

@AlexandreRozier
Copy link

AlexandreRozier commented Oct 15, 2024

Hi, we'd also love to have randomized test execution (at least at spec file level), is there an alternative to brewing our own execution ordering ( - based on @bahmutov's plugin for instance) ? Cheers :)

EDIT : I achieved random test execution using the config.specPattern property : #390 , this is heavily inspired from @bahmutov's @cypress/grep plugin.
This is very hacky but works for the time being...

// cypress/plugins/randomizeSpecs.ts
import * as fs from 'node:fs/promises';
/**
 * @param {Cypress.ConfigOptions} config
 */
export default async function randomizeSpecsPlugin(config) {
  if (!config?.env?.randomizedTestsSeed) {
    return config;
  }

  const specPattern = /.*.cy.ts/;
  const integrationFolder = process.cwd();

  const allFiles = await fs.readdir(integrationFolder, { recursive: true });
  const specFiles = allFiles.filter(s => specPattern.exec(s)).map(s => 'cypress/' + s);
 
  // Run your  custom shuffling function here - for me it's modifying specFiles in-place 
  shuffle(specFiles);

  // Warning : we're overriding existing specPattern to ensure tests are run with this new order
  // Any existing specPattern will be OVERWRITTEN
  // See https://github.com/cypress-io/cypress/issues/390  as to why we're doing things this way
  config.specPattern = specFiles;
  return config;
}

// cypress/cypress.local.config.ts

import randomizeSpecsPlugin from './plugins/randomizeSpecs';
...
async setupNodeEvents(on, config) {
    
      await randomizeSpecsPlugin(config);
      return config
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests