首页 > 解决方案 > 赛普拉斯:如何对“烟雾”、“理智”和“回归”等测试进行分组

问题描述

我面临如何将我的测试分组为“烟雾”、“理智”和“回归”的问题?请建议我们如何使用柏树来实现这一目标。

提前致谢。

标签: cypress

解决方案


我正在使用此插件https://github.com/cypress-io/cypress-grep将测试标记为smokestable等。您可以查看其主页以了解使用情况和用例。我建议使用此选项,因为它很简单。

或者,如果您愿意,您可以创建自己的插件来根据测试用例名称过滤测试,因为 Cypress 还提供了钩子(从 Mocha 借来的)。

before(() => {
  // root-level hook
  // runs once before all tests
})

beforeEach(() => {
  // root-level hook
  // runs before every test block
})

afterEach(() => {
  // runs after each test block
})

after(() => {
  // runs once all tests are done
})

describe('Hooks', () => {
  before(() => {
    // runs once before all tests in the block
  })

  beforeEach(() => {
    // runs before each test in the block
  })

  afterEach(() => {
    // runs after each test in the block
  })

  after(() => {
    // runs once after all tests in the block
  })
})

由于我们希望此实现适用于所有规范,因此放置它的正确位置是 /cypress/support/index.js

beforeEach(function() {
    // get tags passed from command
    const tags = Cypress.env('tagName').split(",");

    // exit if no tag or filter defined - we have nothing to do here
    if ( !tags ) return;

    // get current test's title (which also contains tag/s)
    const testName = Cypress.mocha.getRunner().suite.ctx.currentTest.title

    // check if current test contains at least 1 targetted tag
    for (let i = 0; i < tags.length; i++){
        if ( testName.includes(tags[i]) ) return;
    }

    // skip current test run if test doesn't contain targetted tag/s
    this.skip();
})

在您的规范文件中,您可以为您的测试添加标签/s

describe('this is a test for tests selection', () => {
    it('test #1 [@smoke, @functional, @e2e]', () => {
        console.log("this is a test #1")
    })

    it('test #2[@e2e]', () => {
        console.log("this is a test #2")
    })
})

最后,在您的命令中,您指定用于过滤测试的标签

cypress_tagName="@functional, @smoke" npm run cy:open

推荐阅读