The title says it – What happens between TestMethods in MS Visual Studio Unit Tests?
I have a bunch of TestMethods in a TestClass that has a TestInitialize method.
The TestInitialize method internally loads a Type via Reflection (eg. Type.GetType("MyContainer, MyContainerAssembly") ). MyContainer is a class that inherits from WindsorContainer (from Castle Windsor).
When I choose to run all unit tests in the solution, the first time TestInitialize is called (for the first TestMethod), this works all well and good. When the second TestMethod executes and TestInitialize is called, my Type.GetType call returns null.
I put a breakpoint inside the TestInitialize method and verified this. To debug the issue, I tried in the Immediate Window:
Assembly.Load("MyContainerAssembly")
which worked…then:
Assembly.Load("MyContainerAssembly").GetTypes()
and what do you know? It threw a TypeLoaderException saying that it couldn’t find the assembly Castle.Windsor. Checked the bin\debug directory for the Unit Test project. It’s there.
So then I tried:
Assembly.Load(“Castle.Windsor”)
which worked…then:
Assembly.Load("Castle.Windsor").GetAssemblies()
…could not load Castle.Core…so then
Assembly.Load("Castle.Core")
then
Type.GetType("MyContainer, MyContainerAssembly")
again…and it returned the Type instance, not null.
Thoughts?
Visual Studio does not run the tests in the
bin\Debugoutput folder. Instead, it has a separateTestResultsfolder where it makes a new subfolder and copies assemblies for each test run. (This folder appears in the same folder as the solution file.) The assemblies you mention are probably not being copied.You can add files via test run configurations: Open the Test menu, “Edit Test Run Configurations”, choose a test configuration to edit, and select the “Deployment” view. Here you can add any extra files that need to be deployed.
Alternatively, you can use the DeploymentItem attribute on your tests.