views:

44

answers:

1

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?

+2  A: 

Visual Studio does not run the tests in the bin\Debug output folder. Instead, it has a separate TestResults folder 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.

Wim Coenen
Thanks! Any idea why the files wouldn't get copied there or why the first TestMethod is able to load the assembly successfully?
JeffN825
@jeffn825: They would not be copied if the test assembly doesn't have a dependency on them, directly or indirectly. As for why the first time it would work, that doesn't make any sense to me. I missed that when I read your question the first time. Maybe there is some other problem after all.
Wim Coenen