如何在赛普拉斯中添加测试用例分组 [英] How to add test case grouping in Cypress
问题描述
我目前正在使用赛普拉斯进行UI集成测试.我正在寻找与标准TestNG类似的在赛普拉斯中添加测试用例分组的方法.我无法在赛普拉斯文档中找到任何分组功能.我确实找到了这篇文章:链接在哪里分组使用标签完成.我正在寻找一种更简单的测试用例分组方法.
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.
这是我的用例:在下面的示例中,我对不同的功能(例如Feature1,2,3)进行了测试,每个功能都有不同的测试用例.我想对功能1等单个功能进行测试,是否可以运行功能1的test1.注意:我不是在寻找.only还是.skip .我想添加分组并使用CLI针对特定组运行这些测试.以前有没有人从事过这些工作?
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', () => {
})
})
谢谢,萨希斯(Saahith)
Thanks, Saahith
推荐答案
您可以使用 this.skip()
动态地 skip
测试,该测试可以有条件地应用例如基于环境变量.
You can dynamically skip
a test by using this.skip()
, which can be applied conditionally based on, say, an environment variable.
要在全局范围内执行此操作,请在 cypress/suport.index.js 中添加 beforeEach()
.
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();
}
})
请注意,您必须使用 function()
而不是箭头函数.
Note, you must use a function()
not an arrow function.
变量 testName
包括嵌套的 context()
, describe()
和 it()
中的文本>例如,在赛普拉斯提供的示例 assertions.spec.js 中
The variable testName
includes the text from nested context()
, describe()
, and it()
, for example, in the sample assertions.spec.js provided by Cypress
此
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 .
Note the CYPRESS_ prefix, but in the code it's just TEST_FILTER.
然后
yarn cy:filter:implicit
将跳过所有的显式断言",测试.
will skip all the "Explicit Assertions" tests.
这篇关于如何在赛普拉斯中添加测试用例分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!