测试套件设置方法是为每个测试执行一次,还是只执行一次?
我知道每个测试框架的答案可能有所不同。但对于你认识的人来说,应该发生什么?
I know the answer may differ for each test framework. But for the ones you know, what should happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 NUnit 中,您有
TestFixtureSetUp
在夹具中的所有测试运行之前仅运行一次,而SetUp
在每个测试方法运行之前运行。In NUnit, you have
TestFixtureSetUp
which runs only once before all tests in the fixture run andSetUp
which runs before each test method is run.在 MSTest 中,您有 TestInitializeAttribute
在负载测试中运行时,标有此属性的方法将为测试中的每个虚拟用户迭代运行一次。如果您需要执行一次适用于整个测试的初始化操作,请使用 ClassInitializeAttribute。
AssemblyInitializeAttribute
对所有类中的所有测试运行一次。In MSTest you have 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.
AssemblyInitializeAttribute
is run once for all tests in all classes.这自然取决于框架,对于这个问题的具体答案,您应该检查相关文档。
设置测试方法或夹具很有用,但不应滥用。如果单元测试有复杂的设置方法,你可能会说它们更重要的是集成测试,因此应该重构。复杂的测试设置是一种代码味道。另一方面,明智地使用设置方法可以减少重复并使测试更具可读性和可维护性。
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.
在junit4中,您可以使用注释来标记这两种安装/拆卸方法。摘要如下:
@BeforeClass
;@AfterClass
code>@Before
@之后
In junit4 you have annotations available to mark both kind of setup/teardown methods. Here is the summary:
@BeforeClass
@AfterClass
@Before
@After