cypress 测试_在gitlab管道中并行运行cypress测试

cypress 测试

Learn to run e2e tests faster to save both from time and resources.

学习更快地运行e2e测试,以节省时间和资源。

Okay, now for some reason, you’ve finally chosen to write some cypress tests for your development environment. Then some time, you have realized that your tests are run forever in the pipeline.

好的,现在由于某种原因,您最终选择为您的开发环境编写一些赛普拉斯测试。 然后一段时间,您已经意识到测试将在管道中永远运行。

Image for post

Waiting for the e2e tests more than 20 minutes is not the best way to perform this stage of the pipeline and there is a better way which is the parallelization of the job.

等待e2e测试超过20分钟不是执行此阶段管道的最佳方法,还有更好的方法是并行工作。

运行单独的测试文件 (RUN SEPARATE TEST FILES)

Before diving into the parallelization, let’s have a look at how e2e tests are run.

在深入研究并行化之前,让我们看一下e2e测试的运行方式。

This test:e2e command runs the all cypress tests that are in the cypress folder. But, if we give spec flag to this command, we can run specific files which is the main aim of the parallelization because each job will run some files, not all of them.

test:e2e命令运行cypress文件夹中的所有cypress测试。 但是,如果给此命令指定spec标志,则可以运行特定文件,这是并行化的主要目的,因为每个作业将运行一些文件,而不是全部文件。

Image for post
yarn cypress:run —help
赛普拉斯:run —帮助

For example, if the script in the package.json file is changed with:

例如,如果将package.json文件中的脚本更改为:

Then specified test files can be run with:

然后可以使用以下命令运行指定的测试文件:

yarn test:e2e cypress/integration/example-test-1.spec.js
Image for post
Only one test file have been found
仅找到一个测试文件

Also, multiple test files can be run with comma separated argument.

此外,可以使用逗号分隔的参数运行多个测试文件。

yarn test:e2e example-test-1.spec.js,example-test-2.spec.js,

The real question in here is that how Gitlab will understand to run which test files?

这里真正的问题是,Gitlab将如何理解运行哪些测试文件?

在GITLAB上运行多个作业 (RUN MULTIPLE JOBS ON GITLAB)

On .gitlab-ci.yml file, parallel keyword can be added to run multiple jobs for the given stage. In below, this value is set to 7 which means 7 different job will be run for the stage called e2e which runs the e2e tests.

在.gitlab-ci.yml文件上,可以添加parallel关键字以在给定阶段运行多个作业。 在下面,此值设置为7,这意味着将在运行e2e测试的称为e2e的阶段运行7个不同的作业。

Image for post
7 different job will be run with this adjustment
通过此调整将运行7个不同的作业

If the script is run with this cypress/integration/example-test-1.spec.js argument, then 7 different job will run the same test file which is not the purpose of this article.

如果使用此cypress / integration / example-test-1.spec.js参数运行脚本,则7个不同的作业将运行同一测试文件,这不是本文的目的。

分离测试文件 (SEPARATING THE TEST FILES)

Now, new script file will be written and whatever that script file logs to the screen, that test files will be run.

现在,将写入新的脚本文件,并且无论该脚本文件记录到屏幕上什么,该测试文件都将运行。

- yarn test:e2e "$(node scripts/cypress-parallel.js)"

In here, cypress-parallel.js file will be run and that script will determine which test files will be run.

在这里,将运行cypress-parallel.js文件,该脚本将确定将运行哪些测试文件。

NODE_TOTAL variable is the value of 7 because number of jobs is determined with this in .gitlab-ci.yml file.

NODE_TOTAL变量的值为7,因为作业数量是在.gitlab-ci.yml文件中确定的。

NODE_INDEX is the index number of the jobs which can have from 1 to 7 for this example.

在本示例中,NODE_INDEX是作业的索引号,该索引号可以从1到7。

And TEST_FOLDER includes all of e2e tests. The next step is the get all file names in this folder because it will be needed to pass the file paths.

TEST_FOLDER包括所有e2e测试。 下一步是获取此文件夹中的所有文件名,因为传递文件路径将需要它。

We define a method called walk which basically gets all the files in given directory.

我们定义了一种名为walk的方法,该方法基本上获取给定目录中的所有文件。

// walk('./cypress/integration')
[
'cypress/integration/example-test-1.spec.js',
'cypress/integration/example-test-2.spec.js',
'cypress/integration/example-test-3.spec.js',
]

Finally, it is desired to separate test files into different jobs. To exemplify, if there is 7 jobs and 13 test files then first 6 jobs will run 2 test files and last job will run only 1. For that purpose, we can filter the test files with their index.

最后,希望将测试文件分成不同的作业。 例如,如果有7个作业和13个测试文件,则前6个作业将运行2个测试文件,而最后一个作业将仅运行1个。为此,我们可以使用其索引过滤测试文件。

If the NODE_INDEX = 3, NODE_TOTAL = 5 and total number of test files is 27, then this method will return 2, 7, 12, 17, 22 and 27. test files.

如果NODE_INDEX = 3, NODE_TOTAL = 5,并且测试文件总数为27,则此方法将返回2、7、12、17、22和27个测试文件。

结论 (CONCLUSION)

As you might notice, this is not the best way to handle the parallelization. There are better ways to separate these files into the jobs but even with this adjustment, the required time to run the job is decreased to 1.27 minutes from 20 minutes which is quite enough at least for our development. Furthermore, if any test fails on job number 5 for example, only that job will be run again, not the whole tests.

您可能会注意到,这不是处理并行化的最佳方法。 有更好的方法将这些文件分离到作业中,但是即使进行了此调整,运行作业所需的时间也从20分钟减少到了1.27分钟,这至少对于我们的开发来说已经足够了。 此外,例如,如果任何测试在第5个作业上失败,则仅该作业将再次运行,而不是整个测试。

翻译自: https://medium.com/trendyol-tech/running-cypress-tests-parallel-in-gitlab-pipeline-56b1fa4cb286

cypress 测试

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值