【发布时间】:2019-07-29 23:05:27
【问题描述】:
我想切换只运行一项测试,因此我不必等待其他测试才能看到一项测试的结果。
目前,我注释掉了我的其他测试,但这真的很烦人。
有没有办法在Cypress 中切换只运行一个测试?
【问题讨论】:
标签: javascript automated-tests cypress e2e-testing
我想切换只运行一项测试,因此我不必等待其他测试才能看到一项测试的结果。
目前,我注释掉了我的其他测试,但这真的很烦人。
有没有办法在Cypress 中切换只运行一个测试?
【问题讨论】:
标签: javascript automated-tests cypress e2e-testing
进行此类运行的最佳方法是使用赛普拉斯提供的 .only 关键字。
要在多个描述函数中的一个描述函数中运行所有测试用例,请在所需的描述中添加 .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(){
});
});
所以这里只有第二个描述会运行。
同样,如果您想在 1 描述中运行一些测试用例,请在要运行的所有测试用例前面添加 .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 将运行
这类似于你可能熟悉的fit and fdescribe in karma and jasmine。
您可以使用 it.skip 或 xit
跳过 cypress 中的测试【讨论】:
使用 cypress open 执行时,在测试脚本中使用 @focus 关键字
【讨论】:
cypress run --spec path/to/file.spec.js
或使用全局模式:
cypress run --spec 'path/to/files/*.spec.js'
注意:您需要将 glob 模式用单引号括起来以避免外壳扩展!
您可以使用.only,如the Cypress docs 中所述
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() 可以附加到多个测试中,并且只有这些测试会运行。因此,您可以使用它来运行多个测试用例。 ?
【讨论】:
我的测试文件具有这样的结构 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
【讨论】:
你可以用这个
cypress run -- --spec 'path/to/files/*.spec.js'
或
npm run --spec 'path/to/files/*.spec.js'
它对我有用。
非常感谢
【讨论】:
通过终端运行特定文件:
npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
【讨论】:
您可以通过在 testrunner 方法调用前加上 x(describe、it 等)来静音不需要的测试套件和特定案例
所以它看起来像:
// 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。无论我们在哪里写 this.skip()
【讨论】:
你可以像这样运行测试。
cypress run --spec **/file.js
【讨论】:
有多种方法可以实现这一目标。
.only添加到it 或describe 参见@bkucera 答案npx cypress run --record --spec "cypress/integration/my-spec.js" npm run cypress -- --record --spec "cypress/integration/my-spec.js"
【讨论】: