How can I run just one test with Cypress?
I want to easily run only one specific test, so that I don’t have to wait for all the other tests to finish. Currently, I comment out my other tests, but this becomes cumbersome and time-consuming.
Is there a way to toggle and run just one test in Cypress without commenting on others? I’m looking for a way to cypress run specific test easily.
Hi
Yes there is a way to run just one specific test file in Cypress:
Use .only
to isolate the test: You can use the .only modifier on the test or describe the block to run a specific test. This will ensure that only the specified test runs and the others are skipped.
it.only('should test only this test', () => {
// test code
});
This is the easiest way to cypress run specific tests without modifying your test suite structure.
Hope this is helpful
Use --spec with cypress run: You can specify a particular test file to run using the --spec flag when running Cypress from the command line. This is useful if you want to run tests from a specific file.
npx cypress run --spec 'cypress/integration/myTestFile.spec.js'
This command allows you to cypress run specific test files directly without altering any individual test cases.
Adding to @alveera.khn
You can also filter by test name with --env variable to filter and run specific tests.
For example, you can pass an environment variable to select a test or group of tests:
npx cypress run --env testName=mySpecificTest
Then, inside your test file, you can check for this environment variable and conditionally run your test based on it:
if (Cypress.env('testName') === 'mySpecificTest') {
it('should run this test', () => {
// test code
});
}
This allows you to cypress run specific tests dynamically by passing in the test name via the command line.