如何在 Cypress 中运行与更改文件相关的测试

问题描述 投票:0回答:2

我正在使用 cypress 来设置 E2E 测试。

但是我面临着一些麻烦,因为每次我实现一个新功能或重构一些代码时,我都需要运行所有测试来看看我的修改是否不会破坏我的应用程序中的某些内容。

在 Jest 中,我们有标志

--findRelatedTests
,仅运行修改后的相关测试文件。

我想知道是否有办法在 Cypress 中做同样的事情。

jestjs cypress e2e-testing file-watcher
2个回答
2
投票

您在寻找插件 cypress-watch-and-reload 吗?

// cypress.json

{
  "cypress-watch-and-reload": {
    "watch": ["page/*", "src/*.js"]  // watch source and page-object (helper) files
  }
}

YouTube - 当应用程序文件更改时重新运行 Cypress 测试


1
投票

如果您在本地尝试,那么 1,如果要跳过特定的测试组或测试用例,一种方法是在上下文末尾添加 .skip 或它会阻塞。例如,context.skip() 或 it.skip()。

context.skip('Test group', () => {
  // This whole test group will be skipped
  it('Test case 1', () => {
    // This test case will not run because test group is skipped
  });
});
context('Test group', () => {
  it.skip('test case1', () => {
    // Test case one will be skipped
  });

  it('test case2', () => {
    // Detail for test case two
// This will execute
  });
});
  1. 仅运行特定/修改的测试 您可以在 context 或 it 块的末尾添加 .only,例如 context.only() 或 it.only()。
// Only 1st test group will run
  context.only('test group1', () => {
    it('test case', () => {
      // Test case one will run
    });

    it('test case2', () => {
      // Test case two will run
    });
  });

  // The 2nd test group will not run
  context('test group2', () => {
    it('test case3', () => {
      // Test case three will be skipped
    });

    it('test cas4', () => {
      // Test case three will be skipped
    });
  });
context('test group', () => {
    it.only('test case1', () => {
      // Test case one will run
    });

    it('test case2', () => {
      // Test case two will be skipped
    });
  });
  1. 使用 cypress.json 有条件地运行测试 如果您想运行特定的测试文件,有条件地运行测试是使用 cypress.json 文件。

如果您只想在 test.spec.js 文件中运行测试,则只需在 cypress.json 中添加测试文件的文件路径即可。

{
  "testFiles": "**/test.spec.js"
}

运行多个测试文件

{
  "testFiles": ["**/test-1.spec.js", "**/test-2.spec.js"]
}

忽略测试运行中的特定测试

{
  "ignoreTestFiles": "**/*.ignore.js"
}

使用命令行有条件地运行测试

npx cypress run --spec "cypress/integration/test.spec.js"

要在文件名以 .spec.js 结尾的特定文件夹中运行所有测试

npx cypress run --spec "cypress/integration/testgroup-directory-name/*.spec.js"

如果您正在使用 CI/CD,那么这将对您有所帮助,并且您可以得到一个想法。 使用 cypress-grep 更快地执行测试

© www.soinside.com 2019 - 2024. All rights reserved.