Search code examples
testnggroupingcypresstestcaseui-testing

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


Solution

  • 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.