How to add test case grouping in Cypress

风格不统一 提交于 2021-01-03 09:40:37

问题


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


回答1:


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() into cypress/suport.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.




回答2:


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.



来源:https://stackoverflow.com/questions/65045102/how-to-add-test-case-grouping-in-cypress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!