目前,我注释掉其他测试,但这真的很烦人。
在Cypress中,有没有一种方法可以仅切换运行一个测试?
cypress run --spec path/to/file.spec.js
或使用通配符模式:
cypress run --spec 'path/to/files/*.spec.js'
注意:您需要使用单引号将全局模式包裹起来以避免Shell扩展!
您可以使用像Cypress文档中所述的.only
。
it.only('only run this one', () => {
// similarly use it.skip(...) to skip a test
})
it('not this one', () => {
})
此外,您还可以对describe
和context
块执行相同的操作。
还有一个很好的VSCode
扩展程序,可通过键盘快捷方式更轻松地添加/删除.only
。它称为Test Utils(使用ext install chrisbreiding.test-utils
安装)。它适用于js、coffee和typescript:
.only()
可以附加到多个测试用例上,只有这些测试用例会被运行。因此,您可以使用它来运行不止一个测试用例。 - awesame.only()
才能实现。这从技术上讲是编辑测试。你是说.only()
是可怕的做法还是注释掉测试是可怕的做法?无论哪种方式,这两个选项都会给你相同的结果。我同意,注释掉测试并不理想,但OP意识到了这一点,并提出了问题。 - PhillyStafford有多种方法可以实现这一点。
it
或describe
中添加.only
,请参考@bkucera的回答。 npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
cypress run --spec **/file.js
此命令将运行名为"file.js"的测试文件。进行此类测试的最佳方法是使用cypress提供的.only关键字。
为了运行来自多个describe函数中的一个描述函数中的所有测试用例,请在所需的describe函数中添加.only。
describe("1st describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe.only("2nd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe("3rd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
因此,这里只有第二个描述将运行。
类似地,如果您想在一个描述中运行一些测试用例,请在要运行的所有测试用例前面添加.only。
describe("describe statement", () => {
it("Should check xx", async function(){
});
it.only("Should check yy", async function(){
});
it.only("Should check zz", async function(){
});
});
因此,这里将运行yy和zz的it。
这类似于您可能熟悉的karma和jasmine中的fit和fdescribe。
您可以使用it.skip或xit在cypress中跳过测试。
您可以通过在测试运行器方法调用(describe
,it
等)前面添加x
来静音不需要的测试套件和特定用例。
这样看起来就像:
// this whole testsuite will be muted
xdescribe('Visit google', () => {
it('should visit google', () => { cy.visit('https://google.com/'); });
});
// this testsuite will run
describe('Visit youtube', () => {
it('should visit youtube', () => { cy.visit('https://youtube.com/'); });
// this testcase will be muted
xit('is not necessary', () => { ... });
});
我发现一种跳过不需要运行的测试(在当前测试中)的方法,那就是使用:this.skip();
it('test page', function () {
// skip this test for now
this.skip();
cy.visit('http://example.com/')
cy.contains('test page').click()
cy.url()
.should('include', '/test-page/')
})
1. 使用常规函数作为其第二个参数是很重要的,这在箭头函数中不可用。
2. 无论我们在哪里编写此.skip(),整个测试都将被跳过。
已经存在最为知名的解决方案,只需要在控制台中添加一个简单的参数即可。
https://github.com/cypress-io/cypress/tree/develop/npm/grep只需运行以下命令:
npx cypress run --env grep="测试名称" --spec "文件名"
Cypress .only() 函数仅用于开发。
.only
要好得多,原因在上面的讨论中已经提到了。https://stackoverflow.com/questions/55054337/cypress-run-only-one-test#comment135834689_55054363 - undefined只针对想要执行的测试项使用 .only ,然后通过 npx cypress run --spec path/to/your-file.spec.js 运行该规范。
cypress run -- --spec 'path/to/files/*.spec.js'
或者
npm run --spec 'path/to/files/*.spec.js'
这对我有用。
非常感谢。
path/something.test.jsx
,而命令npx cypress run --spec path/something.test.jsx
在终端中会出现以下异常:Can't run because no spec files were found.
We searched for any files matching this glob pattern:
...
令人惊讶的是,以下内容可以运行测试一个文件(前提是你已经安装了jest):
jest path/something.test.jsx
npx cypress run --spec path/to/file.spec.js
。 - Chris Perry