问题
NUnit has a OneTimeSetup attribute: https://github.com/nunit/docs/wiki/OneTimeSetUp-Attribute. I am trying to think of some scenarios when I can use it. However, I cannot find any GitHub examples online (even my link does not have an example):
Say I had some code like this:
[TestFixture]
public class MyFixture
{
IProduct Product;
[OneTimeSetUp]
public void OneTimeSetUp()
{
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
Product = fixture.Create<Product>();
}
[Test]
public void Test1()
{
//Use Product here
}
[Test]
public void Test2()
{
//Use Product here
}
[Test]
public void Test3()
{
//Use Product here
}
}
Is this a valid scenario for initialising a variable inside OneTimeSetup i.e. because in this vase; Product is used by all of the Test methods?
回答1:
The fixture setup for Moq seems reasonable to me.
However, reusing the same Product
is suspect as it can be modified and accidentally reused which could lead to unstable tests. I would get a new Product
each test run, that is if its the system under test.
回答2:
Yes. No. :-)
Yes... [OneTimeSetUp]
works as you expect. It executes once, before all your tests and all your tests will use the same product.
No... You should probably not use it this way, assuming that the initialization of Product is not extremely costly - it likely is not since it's based on a mock.
OneTimeSetUp
is best used only for extremely costly steps like creating a data base. In fact, it tends to have very little use in purely unit testing.
You might think,"Well if it's even a little more efficient, why not use it all the time?" The answer is that you are writing tests, which implies you don't actually know what the code under test will do. One of your tests may change the common product possibly even put it in an invalid state. Your other tests will start to fail making it tricky to figure out the source of the problem. Almost invariably, you want to start out using SetUp
, which will create a separate product for each one of your tests.
回答3:
Yes, you use OneTimeSetUp
(and OneTimeTearDown
) to do all the setup that will be shared among tests in that fixture. It is run once per test run, so regardless of however many tests in the fixture the code will execute once. Typically you will initialize instances held in private fields etc for the other tests to use, so that you don't end up with lots of duplicate setup code.
来源:https://stackoverflow.com/questions/48388619/how-to-use-onetimesetup