简体   繁体   English

测试套件设置方法是针对每个测试执行一次,还是仅针对所有测试执行一次?

[英]Is the test suite setup method executed once for each test, or only once for all?

I know the answer may differ for each test framework. 我知道每个测试框架的答案可能不同。 But for the ones you know, what should happen? 但对于你知道的人,会发生什么?

在NUnit中,您有TestFixtureSetUp ,它只在夹具运行中的所有测试之前运行一次,而SetUp在每个测试方法运行之前运行。

In MSTest you have TestInitializeAttribute 在MSTest中,您有TestInitializeAttribute

When run in a load test, the method marked with this attribute will run once for every virtual user iteration in the test. 在负载测试中运行时,使用此属性标记的方法将针对测试中的每个虚拟用户迭代运行一次。 If you need to do initialization operations once, that apply to the entire test, use the ClassInitializeAttribute . 如果需要执行一次适用于整个测试的初始化操作,请使用ClassInitializeAttribute

AssemblyInitializeAttribute is run once for all tests in all classes. AssemblyInitializeAttribute对所有类中的所有测试运行一次。

This naturally depends on the frameworks, and for the concrete answers to this you should check the relevant documentation. 这自然取决于框架,对于具体的答案,您应该检查相关文档。

Set up methods for tests, or fixtures are useful, but they should not be abused. 设置测试方法,或者固定装置很有用,但不应滥用它们。 If unit tests have complex set up methods you could argue they are more so integration tests, and thus should be refactored. 如果单元测试有复杂的设置方法,你可以说它们更像是集成测试,因此应该重构。 A complex test set up is a code smell. 复杂的测试设置是代码气味。 On the other hand, set up methods used wisely can reduce duplication and make tests more readable and maintainable. 另一方面,明智地使用设置方法可以减少重复并使测试更具可读性和可维护性。

In junit4 you have annotations available to mark both kind of setup/teardown methods. junit4中,您可以使用注释来标记这两种设置/拆卸方法。 Here is the summary: 以下是摘要:

  • running setup before each test suite use @BeforeClass 在每个测试套件使用@BeforeClass 之前运行安装程序
  • running tear down after each test suite use @AfterClass 在每个测试套件使用@AfterClass 运行拆卸
  • running setup before each test method in your suite use @Before 在套件中的每个测试方法之前运行设置使用@Before
  • running tear down after each test method in your suite use @After 在套件中的每个测试方法使用@After 运行拆卸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 所有测试套件的TestNG运行方法一次 - TestNG run method once for all test suite Xcode单元测试 - 设置和拆卸一次 - Xcode unit test — setup and teardown only once 如何在Spring Boot中的所有测试案例之前仅设置一次独立Controller? - How to setup standalone Controller only once before all test cases in Spring Boot? 所有单元测试仅准备一次集成测试测试数据 - Prepare Integration Test test data only once for ALL Unit Tests JUNIT:为大量测试类只运行一次安装程序 - JUNIT : run setup only once for a large number of test classes 测试可观察性仅评估一次 - Test observable evaluated only once 如何在多个测试项目中只运行一次 singleton 方法 - How to run singleton method only once in multiple test projects 鼻子测试单一设置功能调用一次 - Nose test single setup function called once python单元测试中的两个setUp()函数,每个函数一次运行,一个仅运行一次 - Two setUp() functions in python unit testing, one that runs for every test, one that only runs once 使用 pytest 和 unittest runner 从两个终端运行测试套件,但只显示 unittest runner 结果并执行一次 - Running test suite from both terminal using pytest and unittest runner but show only unittest runner results and execute once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM