NUnit global initialization - bad idea?
We need some global one time setup code in our test suite. We can do it more than once but it takes quite some time.
It's required by all fixtures so
[TestFixtureSetUp]
does not work. It has to run before all[TestFixtureSetUp]
code.Put it in
Main()
s开发者_C百科ince we keep test assemblies as executables. HoweverMain
doesn't get executed under GUI client.Creating a separate class with a static constructor for initialization only works when you reference the class which we do not favor doing in each and every class.
Inheriting all test fixtures from a base class and adding a static constructor to it causes multiple calls to the init code.
Now given the circumstances, I have two questions:
1) Is "global setup" a very bad idea that it's not supported by NUnit?
2) What's the least painful, most common way to achieve this?
[SetUpFixture]
This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace.
The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.
Assembly wide initialization. If you don't put the class in any namespace, it will apply to all tests in the assembly.
eg.
// using statements
[SetUpFixture]
public class GlobalSetup {
[SetUp]
public void ShowSomeTrace() {
Trace.WriteLine("It works..."); // won't actually trace
}
}
http://www.nunit.org/index.php?p=setupFixture&r=2.4
Starting from NUnit 3.0, the Setup
attribute is no longer supported inside classes marked with the SetUpFixture
attribute. The current valid syntax is:
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
// ...
}
[OneTimeTearDown]
public void RunAfterAnyTests()
{
// ...
}
}
The
OneTimeSetUp
method in aSetUpFixture
is executed once before any of the fixtures contained in its namespace. TheOneTimeTearDown
method is executed once after all the fixtures have completed execution.
Current SetUpFixture documentation page
As stated in my comment, you can achieve assembly wide init by using a SetUpFixture located on the assembly level. I needed this to turn off the UI on the default trace listener:
[SetUpFixture]
public class AssemblySetup
{
[SetUp]
public void Setup()
{
var traceListener = Debug.Listeners.Cast<TraceListener>().FirstOrDefault(listener => listener is DefaultTraceListener) as DefaultTraceListener;
if (traceListener != null)
traceListener.AssertUiEnabled = false;
}
}
More about assembly or namespace setup: http://www.nunit.org/index.php?p=setupFixture&r=2.4
Note: As pointed out by others, don't use this to corrupt isolation between your tests.
I don't think there is a nice, built in way to achieve it unfortunately - probably because NUnit is meant to be used for unit tests mainly, and you shouldn't need any global setup for unit tests (everything should be locally mocked in every test fixture).
It is however quite common to use NUnit for integration tests, and there it is very common to have a global setup - as in your case. There are a few reasonable options here:
On my current project we usually do in in the msbuild script that is running the tests. The benefits are that you don't need to remember about any special setup when you write new tests. The downside - you have to make sure you have everything set up when you run the tests from IDE.
If the above is not an option you can use your last idea - to inherit the tests from a common base class. The base class could then reference a singleton class (you can find i.e. Jon Skeets article on how to implement a singleton), which would do the setup. This way it will be run only once.
1) I guess it depends on the context. I've never needed a global setup on any project, but I can imagine scenarios, e.g. an app that only reads data, and a common, global data setup.
2) You could make your global setup, e.g. in the fixture base you mention, stateful. I.e. have a HasRun property or the like that is checked before execution.
精彩评论