Guru Prasad
Guru Prasad

Reputation: 13

Cypress - Running only specific test cases from the test suite

I have 5 test cases in single suite file (TestCase1, TestCase2, TestCase3, TestCase4 and TestCase5) within integrations folder.

My Requirement: I need to run only TestCase2 and TestCase5.

Command to run a single test case from the command line:

./node_modules/.bin/cypress run --spec 'cypress/integration/TestCase2.js

Query: How can I run TestCase2 and TestCase5 together using --spec command?

Upvotes: 1

Views: 11659

Answers (1)

user14783414
user14783414

Reputation:

Your question is a bit confusing, you say 5 test cases in single suite file but your example cypress run --spec 'cypress/integration/TestCase2.js implies individual spec files.

I'll give you an answer both ways.


Individual spec files

Please see the third example docs - cypress run --spec which runs two tests, comma separated.

cypress run --spec "cypress/integration/TestCase2.spec.js,cypress/integration/TestCase5.spec.js"

Single suite file

Please see How to add test case grouping in Cypress which uses a beforeEach() to check test names passed in by ENV variable.

Add this at the top of your single test suite

beforeEach(function() {

  const testFilter = Cypress.env('TEST_FILTER');
  if (!testFilter) return;                       // exit if no filter defined

  const testName = Cypress.mocha.getRunner().test.fullTitle();
  if (!testFilter.includes(testName)) {
    this.skip();
  }
})

Run with

set CYPRESS_TEST_FILTER=Test2;Test5 & cypress open

or

cypress open --env TEST_FILTER=Test2;Test5  

Note when using the --env flag, the test names must not be separated by a comma since Cypress uses that internally to delimit multiple ENV variables.

Upvotes: 2

Related Questions