简体   繁体   中英

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

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.

In junit4 you have annotations available to mark both kind of setup/teardown methods. Here is the summary:

  • running setup before each test suite use @BeforeClass
  • running tear down after each test suite use @AfterClass
  • running setup before each test method in your suite use @Before
  • running tear down after each test method in your suite use @After

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM