问题描述
基本上,我想在执行所有文件中的所有测试之前登录一次.
Basically, I want to login once before all my tests in all files are executed.
我应该使用before钩子在每个测试文件中调用我的登录命令,还是有办法在所有测试之前进行一次?
Should I call my login command in each test file using the before hook or is there any way to do it once before all tests?
推荐答案
简短答案:您可以在 supportFile中的
before
钩子中的 before中编写您的登录命令.
(在其他规格文件之前自动加载的文件).此 before
钩子将在其他测试文件中的任何代码之前运行.
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?
我建议只在每个单独的spec文件中的 before
钩子中包含登录命令.
I would recommend just having the login command in a before
hook in each individual spec file.
我还进一步建议将您的登录命令放在 beforeEach
挂钩中,以避免在测试之间共享任何状态.
I would also further recommend having your login command in a beforeEach
hook to avoid sharing any state in between tests.
这篇关于在使用赛普拉斯的所有测试套件之前,如何执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!