1

What is the best way of initiating the class and hold that initialization in a variable for other method to use instead of initiating every time.

here is my code:

private Employee employee;
public Employee SystemUnderTest
{
   get 
   {
       if (employee == null)
       {
          employee = new Employee();
       }
      return employee;
   }
}

//..method1 Test1
   public void TestMethod1()
   {
     Assert.IsTrue(SystemUnderTest.IsActive());
   }

   //..method2 Test   
   public void TestMethod2()
   {
     Assert.IsTrue(SystemUnderTest.IsEmployeeExists());
   }

PS: when debugging I noticed that it does initialize Employee object with every method.

using 3.5 framework.

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
  • 1
    Your test methods should be independent of each other. Object creation is very fast in C#/.NET. Do you see some performance issues ? – pero Oct 09 '12 at 22:54
  • not really but the only reason i am thinking not initialize is because i have base class that read some data from external source.. –  Oct 09 '12 at 22:59
  • What unit test framework you are using ? NUnit, XUnit, mstest, ... ? – pero Oct 09 '12 at 23:00
  • When running unit-tests there should not be any external IO. Otherwise they are not unit tests but integration test. I suggest you mock part of the code that reach for external data. – pero Oct 09 '12 at 23:01
  • Take a look at http://haacked.com/archive/2008/07/22/unit-test-boundaries.aspx for a discussion about unit testing with external data IO – pero Oct 10 '12 at 07:01

4 Answers4

0

Static initialization should work. This will run when you first access the type, and the shared variable(s) will be available to all instances.

private static bool m_HasInitialized = false;

private static MyClassName()
{
    // Do Stuff...
    m_HasInitialized = true;
}
Rob Hardy
  • 1,821
  • 15
  • 15
  • 1
    This way if tests are modifying the Employee instance the state of that instance while running particular test method will depend on previously run tests. – pero Oct 09 '12 at 22:56
  • 1
    @PetarRepac which is a bad thing, since tests can be run in any order. – dthorpe Oct 09 '12 at 22:59
0

If I'm understanding your question correctly, create a private field in your test class to hold a variable that contains the initialization information, then use a method decorated with a [TestInitialize] or [TestSetUp] attribute (depending on your testing framework) to initialize the information. This will give all of your test methods visibility to the private field, but you only have to define the initialization in one place. This also has the added benefit of reinitializing your variable for every test method so that if one test mutates the state of the variable unexpectedly, it should not cause other tests to break.

But if you really really want the initialization to only occur once (as opposed to just defining the code once), use the [TestFixtureSetUp] or [ClassInitialize] attributes.

  • i did try with `ClassInitialize` attribute but when i run my test it shows all test skipped... none of my test runs.. any idea? – –  Oct 10 '12 at 00:17
  • Are you familiar with the differences between running or debugging tests in the current context or in the solution? You'll want to be sure to be doing the right action. If you already know about all of that, sometimes you can create lists of tests which you manage through the Test menu in visual studio, and perhaps some inadequate set of tests has been defined, one which does not include any of the tests you're wanting to run. I'm getting pretty deep into guesswork now, though. –  Oct 10 '12 at 04:22
0

You can initialize a static field in the class static constructor, or you can use the [ClassInitialize] * unit test attribute to mark a method that you want executed before any test methods in that unit test class execute.

More info here: Do you use TestInitialize or the test class constructor to prepare each test? and why?

Community
  • 1
  • 1
dthorpe
  • 35,318
  • 5
  • 75
  • 119
  • Maybe the OP doesn't use mstest ? – pero Oct 09 '12 at 22:59
  • i did not thought about `ClassInitialize` i will give it a shot. –  Oct 09 '12 at 23:14
  • i did try with ClassInitialize attribute but when i run my test it shows all test skipped... none of my test runs.. any idea? –  Oct 09 '12 at 23:33
0

Singleton pattern (one of variations, lazy initialization):

public class Singleton
{
    private readonly static object _lockObject = new object();
    private static Singleton _instance;

    public static Singleton Instance {
        get
        {
            if (_instance == null)
            {
                lock (_lockObject)
                {
                    if (_instance == null)
                        _instance = new Singleton();
                }
            }
            return _instance;
        }
    }
}
Nenad
  • 24,809
  • 11
  • 75
  • 93
  • Why lazy ? It is not necessary. It complicates the code and the initialization code will eventually be called anyway. – pero Oct 09 '12 at 23:04
  • fair enough, then private static volatile Singleton _instance = new()...; – Nenad Oct 09 '12 at 23:06