How to initialize test class resources in Visual Studio Unit Testing framework?
I'm using the unit testing framework in .NET in C++/CLI to test unmanaged C++ code.
I would like for example an instance of System::Random
to generate random values throughout the test methods.
Do I need to put this as a member variable in my test class?
If yes where can I put the initialization code, cause the ClassInitialize()
met开发者_开发百科hod that is generated is static for some reason and it only has access to a TestContext
which I read is only for using testing data from some external sources.
You can add static properties to your test class and initialize them in the ClassInitialize()
method if you need them to be available to all tests. If you want them initialized per test, then using the TestInitialize()
method is better.
Are you sure you want to use random values in your unit tests? Typically you'd want to use known values (good values, bad values, edge cases, etc) so that your tests are predictable. Using multiple tests with various values where you know the expected behavior (outcome) is more typical than using random values.
精彩评论