赛普拉斯:运行 只有一个测试

Cypress: run only one test

我只想切换 运行 一项测试,这样我就不必等待我的其他测试来查看一项测试的结果。

目前,我注释掉了我的其他测试,但这真的很烦人。

有没有办法在 Cypress 中只切换 运行 一个测试?

到运行只有一个文件

cypress run --spec path/to/file.spec.js

或使用 glob 模式:

cypress run --spec 'path/to/files/*.spec.js'

Note: you need to wrap your glob patterns in single quotes to avoid shell expansion!

至运行一个文件中只有一个测试

您可以按照 the Cypress docs

中所述使用 .only
it.only('only run this one', () => {
  // similarly use it.skip(...) to skip a test
})

it('not this one', () => {
})

此外,您可以对 describecontext 块执行相同的操作

编辑:

还有一个很好的 VSCode 扩展,可以使 adding/removing .only 更容易使用键盘快捷键。它称为 Test Utils(使用 ext install chrisbreiding.test-utils 安装)。它适用于 js、coffee 和 typescript:

有多种方法可以实现这一点。

  1. 您可以将 .only 添加到 itdescribe 请参阅@bkucera 答案
  2. 您可以按照 the doc here 中的说明从终端执行此操作
     npx cypress run --record --spec "cypress/integration/my-spec.js"
    
     npm run cypress -- --record --spec "cypress/integration/my-spec.js"
    

我发现有一种方法可以跳过我不需要的测试运行(在当前测试中),那就是使用: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()

都会跳过整个测试

你可以运行这样的测试。

赛普拉斯 运行 --spec **/file.js

您可以通过在 testrunner 方法调用(describeit 等)前面加上 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', () => { ... });
});

通过终端运行一个特定的文件:

 npx cypress run --record --spec "cypress/integration/my-spec.js"

 npm run cypress -- --record --spec "cypress/integration/my-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
  1. 一个非常简单的解决方案是在您的测试前加上数字,因为测试框架通常会默认 运行 按 alpha/numeric 顺序进行测试 - 所以如果我必须检查一个规范文件 -我会将内容复制到一个文件 0-[file-name].spec 中,然后重新 运行 测试命令。测试完成后 - 您终止测试 运行 - 因为您将获得您正在寻找的结果。此答案针对的是您的测试框架被抽象化的项目,并且作为开发人员,您没有测试框架的所有可用选项。不是最好的答案,但它有效并且直观且超级容易做到。我发现这是一种避免添加一堆条件 skips() 或 only() 调用的方法,这些调用不会进入生产,必须删除,您可以轻松地将文件模式添加到 .gitignore 文件中,这样这些本地文件未签入。

使用 cypress open 执行时在测试脚本中使用@focus 关键字

做这种 运行 的最好方法是使用赛普拉斯提供的 .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 的 运行

这类似于您可能熟悉的 karma 和 jasmine 中的 fit 和 fdescribe。

您可以使用 it.skipxit

跳过 cypress 中的测试