【问题标题】:How to add test case grouping in Cypress如何在 Cypress 中添加测试用例分组
【发布时间】:2021-03-10 16:40:28
【问题描述】:

我目前正在使用 Cypress 进行 UI 集成测试。我正在寻找在柏树中添加测试用例分组的方法,类似于标准的 TestNG。我无法在 cypress 文档中找到任何分组功能。我确实找到了这篇文章:link,其中使用标签进行分组。我正在寻找一种更简单的测试用例分组方法。

这是我的用例:我对下面示例中的 feature1、2、3 等不同功能进行了测试,每个功能都有不同的测试用例。我想为功能 1 等单个功能运行测试。有没有办法运行功能 1 的 test1。注意:我不是在寻找 .only 或 .skip。我想添加分组并使用 CLI 为特定组运行这些测试。 以前有人研究过这些吗?


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

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

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

})

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

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

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


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

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

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



谢谢, 萨希思

【问题讨论】:

    标签: testng grouping cypress testcase ui-testing


    【解决方案1】:

    您可以使用this.skip() 动态地skip 测试,可以根据环境变量有条件地应用。

    要全局执行此操作,请在 cypress/support/index.js 中添加 beforeEach()。

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

    注意,您必须使用function() 而不是箭头函数。

    变量testName 包含来自嵌套context()、describe() 和it() 的文本,例如,在赛普拉斯提供的示例assertions.spec.js 中

    这个

    context('Assertions', () => {
      beforeEach(() => {
        cy.visit('https://example.cypress.io/commands/assertions')
      })
    
      describe('Implicit Assertions', () => {
        it('.should() - make an assertion about the current subject', () => {
    

    有一个testName 的

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

    在 package.json 中

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

    注意 CYPRESS_ 前缀,但在代码中它只是 TEST_FILTER。

    那么,

    yarn cy:filter:implicit
    

    将跳过所有“显式断言”测试。

    【讨论】:

    • 非常感谢这个工作。我已经从 cli 传递了测试组变量,并使用全局 support/index.js 文件中给出的代码来跳过特定组的测试。
    【解决方案2】:

    一种方法是使用Cypress-Select-Tests 插件。

    1.使用npm install --save-dev cypress-select-tests安装插件

    2.安装后,写在cypress/plugins/index.js:

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

    现在根据您的要求,您可以执行如下测试:

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

    现在,如果您想编写自己的自定义逻辑来过滤测试,您也可以这样做。在您的cypress/plugins/index.js 中使用此模块作为文件预处理器并编写您自己的pickTests 函数。

    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))
    }
    

    您也可以参考这些示例以获得进一步参考:cypress-select-tests-example 和 cypress-examples-recipes grep。

    【讨论】:

    猜你喜欢
    • 2022-06-29
    • 2014-01-18
    • 2022-08-23
    • 2017-12-05
    • 2023-01-17
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多