1

I implement a test class for unit tests in VS2013

Inside that class I define the following struct and list:

private struct TestCase
   {
        public string Statement { set; get; }
        public string ExpectedStatement { set; get; }
        public MyClass[] ContainedEntities;
        public MyClass[] NonContainedEntities;
    }

private List<TestCase> m_TestCases;

I want to initialize the m_TestCases with 5 TestCase.

Ho do I do that? Implement Constructor?(once I read that implementing constructor for test class is bad idea).Use ClassInitialize? but it is static....

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
YAKOVM
  • 9,805
  • 31
  • 116
  • 217

1 Answers1

1

If the data is used in each test and could be mutable (changeable from test to test) then initialize the data in the method with ClassInitialize as the attribute for it is only loaded once.

If you want it to be loaded before each unit test use TestInitialize for it will be loaded a new for each test.

See

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • ClassInitialize is static....My member is private....Does it mean that I can`t define private members in the testclass? – YAKOVM Mar 13 '14 at 00:18
  • Should I always introduce TestCleanup if I have TestInitialize ? – YAKOVM Mar 13 '14 at 00:30
  • @Yakov Only if you need to reset state to an original condition due to tests which change information or need to *cleanup* say com references or what not. For example, Say one runs a test which accesses a database and writes information to tables. Using TestCleanup, one can undo what the test(s) wrote to the database, returning it to a *pristine* state. – ΩmegaMan Mar 13 '14 at 03:30
  • @Yakov One can have private members, but the question is why? The test class is not consumed by any other entity/class so whether the member is private or not is moot. – ΩmegaMan Mar 13 '14 at 04:39
  • @Yakov Have you found anything different? – ΩmegaMan Nov 17 '14 at 16:34