【问题标题】:Cypress - Controlling which tests to run - Using Cypress for seedingCypress - 控制要运行的测试 - 使用 Cypress 进行播种
【发布时间】:2020-01-09 10:46:46
【问题描述】:

在集成文件夹中,我目前有 4 个文件夹:

|- seeding [Folder1]
|  |- seeding1
|  |- seeding2
|  |- seeding3
|  |- ...
|
|- testing-feature-1 [Folder2]
|  |- test1
|  |- test2
|  |- test3
|  |- ...
|
|- testing-feature-2 [Folder3]
|  |- test4
|  |- test5
|  |- test6
|  |- ...
|
|- testing-feature-3 [Folder4]
|  |- test7
|  |- test8
|  |- test9
|  |- ...

在我看来,更多的“testing-feature-x”文件夹会随着时间的推移而出现。


我希望能够控制要运行的文件夹。现在我可以运行“所有测试”或单个测试。我是唯一一个缺少“在文件夹中运行规范”的人吗?!或勾选框来选择要运行的测试?

以下是一些有用的场景:

  • 功能 2 已重制。我想在“testing-feature-2”文件夹中运行所有测试。

    目前如何实现:可以用这个终端命令完成:
    npx cypress run --spec 'cypress/integration/testing-feature-2/**/*' --browser canary --no-exit
    这显然是可行的,但仍然......
  • 功能 2 和 3 已重制。我想在“testing-feature-2”和“testing-feature-3”文件夹中运行所有测试。

    目前如何实现:可以通过这两个终端命令完成:
    npx cypress run --spec 'cypress/integration/testing-feature-2/**/*' --browser canary --no-exit
    npx cypress run --spec 'cypress/integration/testing-feature-3/**/*' --browser canary --no-exit
    更烦人一些。但还是可以的。
  • 即将推出全新版本。我想运行除“播种”文件夹之外的所有测试(如果出现问题,可能一遍又一遍)。

    目前如何实现它:为了做到这一点,我需要将“种子”文件夹移出集成文件夹(暂时)。然后我可以运行所有测试。但是,如果一个功能出现了,那么我又回到了终端。
    相当烦人。很多步法。但仍然可行。

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: javascript cypress


    【解决方案1】:

    您可以在一些 npm 脚本的帮助下做您想做的事,免去重复输入繁琐的命令。

    在你的 package.json 中做这样的事情:

    "scripts": {
        "cypress:open": "cypress open",
        "cypress:open:feature-1": "cypress open --config integrationFolder=tests/cypress/integration/feature-1",
        "cypress:open:feature-2": "cypress open --config integrationFolder=tests/cypress/integration/feature-2"
    }
    

    npm run cypress:open 将在 integration 文件夹中运行测试

    npm run cypress:open-feature-1 将在 integration/feature-1 文件夹中运行测试

    您可以将播种函数放入cypress/support/index.js 并将它们添加到全局对象中,如下所示:

    global.school = () => {
      faker.seed(123) // Seeding faker means you get the same details every time
      return {
        _id: faker.random.number(),
        name: `${faker.address.city()} School`,
        slug: faker.lorem.word(),
        email: 'demo@ccc.me',
        password: 'password'
      }
    }
    

    在您的测试脚本中,您可以使用如下行调用此函数:

    const s = school() // Get a fresh object that looks like a school
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 2021-10-07
      • 2020-12-04
      • 1970-01-01
      相关资源
      最近更新 更多