I have been converting a number of test classes from NUnit to the Microsoft Test Framework (MSTest). There are a number of quirky differences between the two, but the one that got me last night was how to use module-level variables.
Frequently in NUnit you (or at least I) use [TestFixtureSetup] to set module level variable (like connection strings) eg.
[TestFixture]
public class AreaDAOTest
{
private string _myVar;
[TestFixtureSetUp]
public void Init()
{
_myVar = //whatever here...;
}
This doesn't work in MSUnit because [TestClassInitialize] is required to
be a static and can't set reference variable.
The two options I
tried were to use [TestInitialze] or a test class constructor.
[TestInitialze] is run every test as expected, but what we didn't expect
was that the constructor seemed to be called for each test as well.
The
better pattern, discovered by a co-worker (thanks, Paul) for setting module level variables
is:
[TestClass]
public class AreaDAOTest
{
static string _myStaticVar;
[ClassInitialize]
public static void TestSetup(TestContext MSTestContext)
{
_myStaticVar = //whatever here...;
}