如何通过将其指定为项目来让 Cypress 运行特定的测试文件夹?

Jas*_*Jas 19 javascript command-line cypress

我有以下用于使用 Cypress 的目录结构:

cypress-automation
    cypress
        fixtures
        integration
           apps
              pulse
              smhw-prod
              smhw-qa
                  folder-a
                       sample-spec.js
        examples
        plugins
        screenshots
        support
        videos
        node_modules
        cypress.json // this is where the file currently is
        package-lock.json
        package.json
Run Code Online (Sandbox Code Playgroud)

期待

我想要做的是运行smhw-qa文件夹内的所有测试(那里有许多规范文件)..并且能够--project使用 CLI使用命令传递它。

目前,如果我只运行 `--run` 没有任何其他参数所有的规范文件,从所有文件夹将开始运行这是不可取的,因为有多个应用程序“项目”(smhw-qa,smhw-prod 等)在这个结构内。这将允许我仅根据需要从单个文件夹运行规范文件。

我也知道使用--run命令来“运行”特定文件夹,但我想使用“项目”来组织这些测试,以便以后更容易识别它们。

我已经查看了显示--project命令使用的文档,但是我需要帮助了解我还需要设置什么才能完成这项工作。通过这个,我指的是package.json文件。

到目前为止,
我尝试遵循为测试“嵌套文件夹”提供的示例:https : //github.com/cypress-io/cypress-test-nested-projects

package.json:添加了以下脚本:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "smhw-qa": "cypress run --project ./cypress/integration/apps/smhw-qa"
  },
Run Code Online (Sandbox Code Playgroud)

当我像这样通过终端运行它时:

?  cypress-automation npx cypress run --project ./cypress/integration/apps/smhw-qa
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Can't run because no spec files were found.

We searched for any files inside of this folder:

/Users/jaswindersingh/Documents/cypress-automation/cypress/integration/apps/smhw-qa/cypress/integration
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?我的cypress.json文件应该放在其他地方吗?

我希望能够将“应用程序”中的每个文件夹设置为项目,然后能够使用cypress run --project命令运行这些项目。

这也将使我在连接 CI 时更容易运行特定的测试文件夹,以便在我选择时只运行特定的项目(及其规范文件)。

jno*_*dim 35

我想你正在寻找 --spec path/to/folder/*.js

您可以在一个文件夹中运行所有测试,甚至可以在一个文件夹的所有子文件夹中运行,

前任/ npx cypress run --spec cypresss/integration/subsetA/**/*-spec.js

将在 cypress/integration 的“subsetA”文件夹下的所有文件夹中运行所有 .js 规范。

所以在你的情况下:npx cypress run --spec cypress/integration/apps/smhw-qa/**/*-spec.js应该做到这一点。

https://docs.cypress.io/guides/guides/command-line.html#How-to-run-commands

  • 不要忘记使用 "(双引号)指定文件名。例如 `npx cypress run --spec "cypress/integration/apps/smhw-qa/**/*-spec.js"`。否则可能会不起作用(对我来说,在 Linux 管道上运行它时失败了,即使在我的 Windows 本地计算机中首先工作:)) (3认同)