[英]How to run before on most cypress tests but not all
I have a login test I want to code for cypress.我有一个登录测试,我想为 cypress 编写代码。 Currently, I need to login before all tests so i use a before block.目前,我需要在所有测试之前登录,所以我使用了 before 块。 But I want to automatically run the login test before all tests except for 2 or 3 files.但我想在除 2 或 3 个文件之外的所有测试之前自动运行登录测试。 Is that possible?那可能吗? Or do I need to move the login code into the describe blocks themselves.或者我是否需要将登录代码本身移动到描述块中。 I'd rather segment the tests because I don't want so much redundancy, writing the same code so many times.我宁愿对测试进行分段,因为我不想要太多的冗余,多次编写相同的代码。
The official Cypress documentation on hooks has a great example showing where before
and beforeEach
hooks are placed in the describe
and it
blocks, as well as outside of the describe
block. 赛普拉斯官方关于钩子的文档有一个很好的例子,展示了before
和beforeEach
钩子在describe
和it
块以及describe
块之外的位置。
One thing they mention is to be wary of root level hooks, because of its unpredictable order of execution when running your specs together.他们提到的一件事是要警惕根级别的钩子,因为当你一起运行你的规范时,它的执行顺序是不可预测的。
As far as I can see, you have a couple options here.据我所知,你有几个选择。 1) You can have your hooks explicitly stated in each of your spec files, so instead of having a root level login, you call that for each spec file that needs to run in. 2) You create a sort of base file where you can toggle whether the spec file will execute the login functionality. 1)您可以在每个规范文件中明确说明您的钩子,因此您可以为需要运行的每个规范文件调用它而不是具有根级别登录。2)您可以创建一种基本文件,您可以切换规范文件是否将执行登录功能。
But again, #2 may lead to some unpredictable behavior.但同样,#2 可能会导致一些不可预测的行为。 My suggestion is to keep it simple and clean, and have your hooks specified directly in your spec files.我的建议是保持简单和干净,并在你的规范文件中直接指定你的钩子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.