首页 > 解决方案 > Cypress - 控制要运行的测试 - 使用 Cypress 进行播种

问题描述

在集成文件夹中,我目前有 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”文件夹。


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

以下是一些有用的场景:

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

标签: javascriptcypress

解决方案


你可以在一些 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:openintegration将在文件夹中运行测试

npm run cypress:open-feature-1integration/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

我希望这会有所帮助


推荐阅读