开发者

Does NUnit create a new instance of the test fixture class for each contained test method nowadays?

开发者 https://www.devze.com 2023-03-24 17:20 出处:网络
As written in a fairly old book XUnit Patterns NUnit 2.0 did not create new test fixtures for each test, and because of that if tests were manipulating some state of fixture it became shared and could

As written in a fairly old book XUnit Patterns NUnit 2.0 did not create new test fixtures for each test, and because of that if tests were manipulating some state of fixture it became shared and could开发者_开发知识库 cause various bad side effects.

Is this still the same? I tried to find it on official site but failed, and havent used NUnit for a while.


The fixture is created once for all of the tests in that fixture.

For a given fixture class, a FixtureSetup method is run once for all of the tests in a fixture, and a Setup method is run once for each test. So, any state that needs to be reset should be done in a Setup method (or TearDown, which is run at the end of each test.)


Since 3.13 you can configure that with

LifeCycle.SingleInstance    A single instance is created and shared for all test cases
LifeCycle.InstancePerTestCase   A new instance is created for each test case

https://docs.nunit.org/articles/nunit/writing-tests/attributes/fixturelifecycle.html


I found that this was an issue that affected me and also found this link which provides a bit of history to the issue; https://blogs.msdn.microsoft.com/jamesnewkirk/2004/12/04/why-variables-in-nunit-testfixture-classes-should-be-static

I think one of the biggest screw-ups that was made when we wrote NUnit V2.0 was to not create a new instance of the test fixture class for each contained test method.

Not yet tested this in V3 to see if its changed

0

精彩评论

暂无评论...
验证码 换一张
取 消