Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

testng - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...