Cypress:仅运行一个测试

263
我想要仅运行一个测试,以便我不必等待其他测试完成才能看到一个测试的结果。
目前,我注释掉其他测试,但这真的很烦人。
在Cypress中,有没有一种方法可以仅切换运行一个测试?

1
在 macOS 上,使用以下正确答案的变体来运行一个文件: npx cypress run --spec path/to/file.spec.js - Chris Perry
16个回答

394

只运行一个文件

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', () => {
})

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

编辑:

还有一个很好的VSCode扩展程序,可通过键盘快捷方式更轻松地添加/删除.only。它称为Test Utils(使用ext install chrisbreiding.test-utils安装)。它适用于js、coffee和typescript:

enter image description here


12
值得注意的是,.only() 可以附加到多个测试用例上,只有这些测试用例会被运行。因此,您可以使用它来运行不止一个测试用例。 - awesame
6
可怕的做法。我不应该需要编辑我的测试文件来排除哪些测试被运行。 - Dragas
1
因为我随后需要编辑测试文件,这违背了测试的核心概念。请参考Maven Surefire插件的“-Dtest”标志,该标志能够指定应运行哪些测试,甚至可以指定要运行哪个单独测试。这有助于确定修改SUT的哪个测试,并且不会在自身之后清理,导致其他测试随机失败。编辑测试是无效的。 - Dragas
1
好的,我理解了。但这不是Maven。如果你想/需要只运行一个Cypress测试,你必须在测试中添加.only()才能实现。这从技术上讲是编辑测试。你是说.only()是可怕的做法还是注释掉测试是可怕的做法?无论哪种方式,这两个选项都会给你相同的结果。我同意,注释掉测试并不理想,但OP意识到了这一点,并提出了问题。 - PhillyStafford
2
@PhillyStafford 我认为Dragas只是在说,如果Cypress允许从命令行指定一个测试,那么它会更好,这样我们就不需要编辑测试文件来重新运行一个特定的测试。我完全同意。我很惊讶在Cypress中没有找到这样的方法。其他测试框架都允许这样做。 - undefined
显示剩余3条评论

62

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

  1. 您可以在itdescribe中添加.only,请参考@bkucera的回答。
  2. 您可以按照此处的文档说明从终端执行。
 npx cypress run --record --spec "cypress/integration/my-spec.js"

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

4
包括CLI示例使这个答案对我来说是最完整和有用的。 - BradGreens
@Morlo Mbakop,你能帮忙回答这个问题吗?https://stackoverflow.com/questions/71605624/how-to-generate-only-test-coverage-from-a-specific-folder-with-tests-in-cypress - Asking

12
您可以按照如下方式运行测试:

cypress run --spec **/file.js

此命令将运行名为"file.js"的测试文件。

这个应该有更多的赞。Jest有一个非常好用的CLI,你可以输入部分文件名来查找它。这是Cypress最接近的功能了。 - Ryan E.

9

进行此类测试的最佳方法是使用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中跳过测试。


9

您可以通过在测试运行器方法调用(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', () => { ... });
});

6

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


4

3
请注意,目前需要一个单独的npm包/插件才能实现此功能:https://github.com/cypress-io/cypress/tree/develop/npm/grep - burnettk
这应该是被接受的答案。它比使用.only要好得多,原因在上面的讨论中已经提到了。https://stackoverflow.com/questions/55054337/cypress-run-only-one-test#comment135834689_55054363 - undefined

2

只针对想要执行的测试项使用 .only ,然后通过 npx cypress run --spec path/to/your-file.spec.js 运行该规范。


1
你可以使用这个。
cypress run -- --spec 'path/to/files/*.spec.js'

或者

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

这对我有用。

非常感谢。


第一行有效,第二行无效 - Nick Roz

1
我的测试文件结构如下: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

你能帮忙解决这个问题吗?https://stackoverflow.com/questions/71605624/how-to-generate-only-test-coverage-from-a-specific-folder-with-tests-in-cypress - Asking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接