Module Variables in MSTest

Posted by AgileCoder on July 1, 2010

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...;
     }


cialis generika kamagra australia cialis preise cialis kaufen cialis bestellen levitra generika viagra online kaufen kamagra bestellen cialis generika kamagra oral jelly kamagra 100mg prix cialis cialis generique kamagra gel viagra prix viagra pour homme acheter cialis kamagra prix viagra pas cher cialis 20mg levitra 20
Levitra Generika kaufen Kaufen Levitra Generika Online Cialis ohne Rezept kaufen Viagra online Viagra Generika kaufen Cialis Soft Tabs kaufen Sildenafil kaufen Apcalis Oral Jelly kaufen Viagra Soft Tabs bestellen Cialis Original kaufen Viagra bestellen Viagra bestellen Propecia Generika kaufen Viagra und seine Analoga Original Testpakete Levitra und Kamagra Erfahrungen Viagra Soft Tabs Kamagra kaufen
kamagra kopen in de winkel viagra werking cialis kopen in nederland cialis bijwerkingen kamagra bijsluiter levitra bijwerkingen viagra kopen apotheek cialis erfaring cialis i norge viagra effekt hva er kamagra viagra nettbutikk levitra eller cialis kamagra gel comprar cialis efeitos secundarios viagra farmacia cialis bula viagra infarmed levitra comprimidos cialis vs viagra cialis flashback kamagra tjejer kamagra effekt levitra fass kamagra oral jelly opiniones levitra generico precio cialis venta kamagra sobres cialis efectos secundarios