views:

71

answers:

3

In my MSTest UnitTest project, before running any tests, I need to execute some commands. Is there a feature, kind of like Global.asax is for web based projects, that will let me kick off something before any tests run?

I should make it clear that when I say "execute some commands", I don't mean DOS commands, but execute some code.

+1  A: 

properties of you project and then debug field there you can specify arguments

EDIT When you see the debug menu in the properties you can start an external program to do certain things for you when you start debugging. This will trigger when you launch an instance of your test project. You can also specify command line arguments in the command line arguments box.

For example I use NUnit I specify NUnit as the external program and specify the location of the .dll in the command line arguments

Chino
I would like to execute some code rather than run external commands.
AngryHacker
And what prevents you from running an .exe file that you created? You can compile your code that needs to be executed into an exe that you will start on a debug session or am I missing something because I think that's a good solution to your problem.
Chino
@Chino. I need to run some code before any of the tests kick off. Code being some static methods on some classes that will get called later by most of the tests. That's why I need to run some code before any of the tests execute.
AngryHacker
+4  A: 

If I understand correctly, you need to have some initialization code run before you start your tests. If that is indeed the case you should declare a method inside your unit-test class with the ClassInitializeAttribute like this:

[ClassInitialize]
public void ClassSetUp()
{
     //initialization code goes here...
}

Edit: there is also the AssemblyInitializeAttribute that will run before any other tests in assembly

bottlenecked
+1, wow, had no idea about the `AssemblyInitializeAttribute`
tster
`AssemblyInitializeAttribute` is exactly what I need. Thanks a bunch, have your self a green check mark.
AngryHacker
+1  A: 

Unit test frameworks usually support set up and "tear down" methods for both the entire test fixture and individual tests. MSTest lets you specify which methods to run when with these attributes:

[ClassIntialize()]
public void ClassInitialize() {
    // MSTest runs this code once before any of your tests
}

[ClassCleanup()]
public void ClassCleanUp() {
    // Runs this code once after all your tests are finished.
}

[TestIntialize()]
public void TestInitialize() {
    // Runs this code before every test
}

[TestCleanup()]
public void TestCleanUp() {
    // Runs this code after every test
}

Having said that, be careful with the class initialize and cleanup methods if you're running ASP.NET unit tests. As it says in the ClassInitializeAttribute documentation:

This attribute should not be used on ASP.NET unit tests, that is, any test with [HostType("ASP.NET")] attribute. Because of the stateless nature of IIS and ASP.NET, a method decorated with this attribute may be called more than once per test run.

Jeff Sternal