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

2
  • Which test framework do you use? Commented Mar 12, 2014 at 23:01
  • @HamletHakobyan - VS2013,C#,UnitTest Commented Mar 12, 2014 at 23:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

ClassInitialize is static....My member is private....Does it mean that I can`t define private members in the testclass?
Should I always introduce TestCleanup if I have TestInitialize ?
@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.
@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.
@Yakov Have you found anything different?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.