tags:

views:

341

answers:

2

Basically I would like to tell MSTest to execute a bit of code before launching into a series of test runs, essentially what I would like to do is the same thing as sticking some code in Main().

The reason I would like to do this is that I would like to do some logging with log4net during my integration test runs. I cannot just use the log4net.Config.XmlConfigurator assembly attribute since by the time it reads it in my test assembly it has already called LoggerManager. The documentation recommends configuring log4net explicitly at the code entry point - but where is that in my tests?

Edit: I need to be able to run my tests in TestDriven.NET and MSTest runner

+1  A: 

I see this in the MS Test header.

// Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext) { }

This would run before the tests in one class.

Sounds like you want to run something before all of the tests.

There is also the setup script option in the test run config.

Maestro1024
Unless I'm missing something, setup scripts don't help with running code in the test AppDomain.
George Mauer
It's probably me. I am not sure I follow you when you say "running code in the test AppDomain. ". I read your question as wanting to execute code before a series of tests, if it was something else that is different."Setup scripts and cleanup scripts run before and after test runs, regardless of the types of tests that are contained in those test runs."http://msdn.microsoft.com/en-us/library/ms182480%28VS.80%29.aspx
Maestro1024
+3  A: 

FWIW, you can use the AssemblyInitialize attribute to run code before all unit tests in an assembly executes:

[AssemblyInitialize()]
public static void AssemblyInit(TestContext context)
{
    // Initalization code goes here
}

If you have more than one unit test assembly, I'm not aware of anything that encompasses more than one assembly.

As far as I'm aware, this is as close as you can get to a Main equivalent.

Mark Seemann
This should work for my purposes
George Mauer
Nevermind, this does not work for my purposes. Our tests have to be runnable from MSTest and TestDriven.NET and the latter doesn't seem to support AssemblyInitialize
George Mauer