How do I load data into a unit test using the [AssemblyInitialize] or [ClassInitialize] attributes and still have the data persist
I know I'm missing one important rule about static methods but would would be the point of initializing something if you couldn't' use it later on for different purposes?
I have 开发者_JS百科a method called LoadValidConfig and a private member called configSetup
[TestClass]
public class ConfigControllerTest
{
private ConfigSetup configSetup;
private TestContext testContextInstance;
[ClassInitialize]
public static void LoadValidConfig(TestContext context)
{
ConfigSetup setup;
ConfigController.LoadConfig(out setup);
}
[TestMethod]
public void ConfigTest1()
{
//example test
}
}
I can't get the static method to access and initialize the configSetup class with the ConfigSetup reference populated by the ConfigController.LoadConfig() method.
I could really use some direction here. I've used N Unit in the past to do [setup] and [teardown] initialization and cleanup respectively before but this isn't working like those.
[ClassInitialize]
runs in a static context and is run before any of the test methods run. You are probably looking for [TestInitialize]
which is for an instance initialization method, and is similar to NUnit's [Setup]
.
Then try assigning your field after the LoadConfig method:
[TestInitialize]
public void LoadValidConfig()
{
ConfigSetup setup;
ConfigController.LoadConfig(out setup);
configSetup = value;
}
(Or, you could keep ClassInitialize if that suits you better, and make configSetup static).
精彩评论