Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add test case grouping in Cypress

I am currently working on UI Integration tests using Cypress. I am looking for ways to add test case grouping in cypress similar to the standard TestNG. I was not able to find any grouping features in cypress documentation. I did find this post: link where grouping is done using tags. I am looking for a simpler way for test case grouping.

Here is my use case: I have tests for different features like feature1,2,3 in below example and each feature has different test cases. I would like to run my tests for individual features like Feature 1. Is there a way to run test1 of Feature 1. Note: I am not looking for .only or .skip. I would like to add grouping and run these tests using CLI for a particular group. Has anyone worked on these before?


describe('Feature1', () => {
    it('test1', () => {
    })

    it('test2', () => {
    })

    it('test3', () => {
    })

})

describe('Feature2', () => {
    it('test1', () => {
    })

    it('test2', () => {
    })

    it('test3', () => {
    })
})


describe('Feature3', () => {
    it('test1', () => {
    })

    it('test2', () => {
    })

    it('test3', () => {
    })
}) 



Thanks, Saahith

like image 666
sgv393 Avatar asked Nov 27 '20 23:11

sgv393


People also ask

Which command is used to group the tests in Cypress?

cypress run --group <name> Group recorded tests together under a single run. You can add multiple groups to the same run by passing a different name.

How does Cypress handle multiple tabs?

Browser Automation with Cypress and Gherkin 2022 Cypress does not have a specific command to work with tabs. It has a workaround method in jQuery through which it handles the tabs. In the html code, a link or button opens to a new tab, because of the attribute target.


2 Answers

You can dynamically skip a test by using this.skip(), which can be applied conditionally based on, say, an environment variable.

To do it globally add a beforeEach() in cypress/support/index.js.

beforeEach(function() {

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

Note, you must use a function() not an arrow function.

The variable testName includes the text from nested context(), describe(), and it(), for example, in the sample assertions.spec.js provided by Cypress

This

context('Assertions', () => {
  beforeEach(() => {
    cy.visit('https://example.cypress.io/commands/assertions')
  })

  describe('Implicit Assertions', () => {
    it('.should() - make an assertion about the current subject', () => {

has a testName of

"Assertions Implicit Assertions .should() - make an assertion about the current subject"

In package.json

  "scripts": {
    "cy:open": "cypress open",
    "cy:filter:implicit": "set CYPRESS_TEST_FILTER=Implicit & cypress open"
  },

Note the CYPRESS_ prefix, but in the code it's just TEST_FILTER.

Then,

yarn cy:filter:implicit

will skip all the "Explicit Assertions" tests.

like image 65
Richard Matsen Avatar answered Sep 23 '22 17:09

Richard Matsen


One way to do this is to use the Cypress-Select-Tests plugin.

1.Install the plugin using npm install --save-dev cypress-select-tests

2.Once installed, write under cypress/plugins/index.js:

const selectTestsWithGrep = require('cypress-select-tests/grep')
module.exports = (on, config) => {
  on('file:preprocessor', selectTestsWithGrep(config))
}

Now based on your requirement you can execute your tests like:

 ## run tests with "works" in their full titles 
 $ npx cypress open --env grep=works

 ## runs only specs with "foo" in their filename 
 $ npx cypress run --env fgrep=foo

 ## runs only tests with "works" from specs with "foo" 
 $ npx cypress run --env fgrep=foo,grep=works

 ## runs tests with "feature A" in the title 
 $ npx cypress run --env grep='feature A'

 ## runs only specs NOT with "foo" in their filename 
 $ npx cypress run --env fgrep=foo,invert=true

 ## runs tests NOT with "feature A" in the title 
 $ npx cypress run --env grep='feature A',invert=true

Now in case you want to write your own custom logic to filter out tests, you can do that as well. In your cypress/plugins/index.js use this module as a file preprocessor and write your own pickTests function.

const selectTests = require('cypress-select-tests')

// return test names you want to run
const pickTests = (filename, foundTests, cypressConfig) => {

  // found tests will be names of the tests found in "filename" spec
  // it is a list of names, each name an Array of strings
  // ['suite 1', 'suite 2', ..., 'test name']

  // return [] to skip ALL tests
  // OR
  // let's only run tests with "does" in the title

  return foundTests.filter(fullTestName => fullTestName.join(' ').includes('does'))
}

module.exports = (on, config) => {
  on('file:preprocessor', selectTests(config, pickTests))
}

You can refer to these examples as well for further reference: cypress-select-tests-example and cypress-examples-recipes grep.

like image 30
Alapan Das Avatar answered Sep 24 '22 17:09

Alapan Das