我如何在赛普拉斯所有测试套件之前执行代码 [英] How can I execute code before all tests suite with Cypress

查看:71
本文介绍了我如何在赛普拉斯所有测试套件之前执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想在执行所有测试之前登录一次。我的测试文件分为几个文件。

Basically, I want to login once before all my tests are executed. My tests files are split on several files.

我应该使用before钩子在每个测试文件中调用我的登录命令,还是有什么办法可以一次完成所有操作?测试?

Should I call my login command in each test file using the before hook or is there any way to to do it once before all tests ?

推荐答案

简短答案:您可以在 supportFile 内的钩子之前(在其他规格文件之前自动加载的文件)。此之前挂钩将在其他测试文件中的任何代码之前运行。

Short answer: You can write your login command in a before hook within the supportFile (the file that is loaded automatically before your other spec files). This before hook will run before any of the code in your other test files.

建议:话虽如此,这种方法几乎没有灵活性来改变您的个人测试将来可能需要的文件,例如:

Recommendations: That being said, this approach leaves little flexibility for variation in your individual test files that you may want in the future like:


  • 如果要为一个测试以不同的方式播种数据库怎么办?

  • 如果您想以具有不同权限的其他用户身份登录怎么办?

  • 如果您需要在 onBeforeLoad 一次?

  • What if you want to seed the database differently for one test?
  • What if you want to log in as a different user with different permissions?
  • What if you need to do something in onBeforeLoad once?

我建议只在之前输入登录命令钩在每个单独的规范文件中。

I would recommend just having the login command in a before hook in each individual spec file.

我还要进一步建议在 beforeEach 钩中将您的登录命令包含在< a href = https://docs.cypress.io/guides/references/best-practices.html#Having-tests-rely-on-the-state-of-previous-tests rel = noreferrer>避免共享在测试之间任何状态。

I would also further recommend having your login command in a beforeEach hook to avoid sharing any state in between tests.

这篇关于我如何在赛普拉斯所有测试套件之前执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆