[英]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:
以下是摘要:
@BeforeClass
@BeforeClass
之前运行安装程序 @AfterClass
@AfterClass
后运行拆卸 @Before
@Before
@After
@After
后运行拆卸
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.