有什么方法可以在本地机器上运行并行 cypress 测试,例如 5 个流

Lev*_*nko 18 continuous-integration cypress

我正在尝试在本地机器上设置 cypress 并运行并行测试。但我找不到一些信息如何做到这一点。

Nic*_*asi 11

从技术上讲,这是可能的。Cypress 不推荐它,因为在同一台机器上运行多个 cypress 实例会消耗大量资源(总而言之是 CPU),并且会降低整个机器的性能并导致无用的结果。

无论如何,如果您的资源有限并且无法使用官方仪表板,或者您没有一个以上的 CI 服务器可用,您可以在一台机器上运行您的测试,启动 cypress run 多次,将您的测试套件划分到多个文件夹中。

我创建了一个名为cypress-parallel( https://github.com/tnicola/cypress-parallel )的 npm 库,它(在第一次运行之后)根据测试运行历史记录和每个子集平衡和拆分测试套件子集它启动一个赛普拉斯命令。它还从所有规范文件中收集结果,并在执行结束时记录它们。在性能方面,似乎使用 2 个进程可以将整体测试执行时间缩短多达 40%。

  • @EvgeniiBazhanov 你在 Mac 上运行它吗?我已经尝试解决这个问题,请查看新版本 0.1.4 (2认同)
  • 现在您可以传递自己的规范文件夹:v.0.1.5 中的 https://github.com/tnicola/cypress-parallel#scripts-options。如果其他功能不起作用,请直接在存储库上提出问题,否则我们将向这篇文章中的所有人发送垃圾邮件。 (2认同)
  • 实际上,对于纱线,也许我应该在您的存储库中创建一个包含更多详细信息的问题 https://github.com/tnicola/cypress-parallel/issues 问题是我正在尝试在我们的工作项目中实现您的库,所以我需要创建另一个带有匿名数据的简单项目 (2认同)

Mr.*_* J. 8

好吧,我有点在本地并行运行它们。一些使用的想法:

  1. 我有一台 MacBook,所以它是为 iOS 实现的。
  2. 我的应用程序在 Docker 容器中运行,我只需要一个实例即可同时运行多个测试。通过我的终端,我创建了多个文件,将规范拆分为单独的 .command-files,如下所示:echo "cd <PROJECT_DIRECTORY> && npx cypress run --spec cypress/integration/<SPECS_DIRECTORY>/*" > cypress.command; chmod +x cypress.command 您可以在.command 后面堆叠多个目录/文件--spec,因此--spec cypress/integration/<SPECS_DIRECTORY>/* cypress/integration/<SPECS_DIRECTORY2>/*也是有效的。
  3. 假设我有 2 个 .command 文件。我可以用这个命令启动那些: open cypress-01.command cypress-02.command
  4. 这将启动两个单独的终端,都运行每个文件中提到的规范。

这将我的本地测试运行时间从 1.5 小时减少到 15 分钟。


luc*_*ail 6

A)最“天真的”(1分钟完成)解决方案,假设您使用的是 linux/macO,实际上工作得不错(只是在本地重新运行回归),&最后有一个简单的 bash 脚本

# to keep vid,pic could cause issue when trying to write and delete at the same time
export CYPRESS_trashAssetsBeforeRuns=false
XDG_CONFIG_HOME=/tmp/cyhome1 cypress run -spec cypress/integration/first.spec.js,cypress/integration/another1.spec.js &
XDG_CONFIG_HOME=/tmp/cyhome2 cypress run -spec cypress/integration/another2.spec.js,cypress/integration/another3.spec.js &
Run Code Online (Sandbox Code Playgroud)

B)但是如果你想要一些更“复杂”的东西,请继续阅读:

然而,在我们的测试中,我们在 4 个数据中心(aws、gc)上运行相同的回归,并且在每个数据中心上运行多个品牌(有些是为了冗余,有些是特定于该 DC 位置),因此根据我们的需求,我们不需要进行规格平衡。与柏树进程相当平行。

到目前为止,这似乎运作良好,您需要几个先决条件,您可以在此处阅读。我们必须解决一些问题。

  1. Xvfb 竞争条件
  2. 能够限制线程数量
  3. 配置文件锁定问题
  4. 图像访问问题

  1. 在运行我们的并行运行脚本之前启动 Xvfb。
    # Start x11 server to avoid race condition in threads
    Xvfb :96 &
    # Make all cypress instances connect to the spawned x11
    export DISPLAY=:96
    # Read 4)
    export CYPRESS_trashAssetsBeforeRuns=false
    # read below (that's where the parallelization happens node.js 10+) 
    node ./main.js
Run Code Online (Sandbox Code Playgroud)
  1. 有更好、更强大的解决方案,但这似乎对我们有用。上面的 bash 运行下面的内容main.js。每个品牌数组都是并行执行的,但await每个系列的执行forEachSeries都是并行执行的,如果没有它,您将只是并行运行所有内容(而不是 2 个,而是 4 个threads)。因此,只要您可以创建一个数组,第一级数组的数量将定义并行线程的数量。您可以在谷歌上搜索平衡数组函数并使用它来平衡数组,如果您决定平衡规格而不是“品牌”,如下所示,您只需修改传递给的命令,awaitedSpawn()例如XDG_CONFIG_HOME=/tmp/cyhome${cfg.id} cypress run --spec {cfg.spec}.
# to keep vid,pic could cause issue when trying to write and delete at the same time
export CYPRESS_trashAssetsBeforeRuns=false
XDG_CONFIG_HOME=/tmp/cyhome1 cypress run -spec cypress/integration/first.spec.js,cypress/integration/another1.spec.js &
XDG_CONFIG_HOME=/tmp/cyhome2 cypress run -spec cypress/integration/another2.spec.js,cypress/integration/another3.spec.js &
Run Code Online (Sandbox Code Playgroud)
    # Start x11 server to avoid race condition in threads
    Xvfb :96 &
    # Make all cypress instances connect to the spawned x11
    export DISPLAY=:96
    # Read 4)
    export CYPRESS_trashAssetsBeforeRuns=false
    # read below (that's where the parallelization happens node.js 10+) 
    node ./main.js
Run Code Online (Sandbox Code Playgroud)
  1. 上面这部分代码解决了这个问题XDG_CONFIG_HOME=/tmp/cyhome1

  2. 只需设置 cypress 环境变量,trashAssetsBeforRuns=false 一种方法是使用 cypress.json 或如 bash 脚本中所示1)