tags:

views:

1293

answers:

4

Is it possible to run specific method before each test?

I know about TestInitialize attribute but this attribute has "class scope". If it's defined in Test class it will be execute before each test from this class.

I want to define method that will be executed before each test defined in assembly.

A: 

I think you are looking for the ClassInitialize attribute.

Andrew Hare
ClassInitialize is executed once for all class. So if we have 5 tests in class, method with ClassInitialize is executed once.
brzozow
Yeah, he probably meant TestInitialize.
David Pokluda
A: 

Well isn't MSTest instantiating the class for each test? That was my understanding of it. In such a case whatever you call from your constructor is the initialization code (per test by definition).

EDIT: If it doesn't work (which I still think it should because MSTest needs to make sure that individual test method runs are isolated) then TestInitialize is your attribute. By the way the best unit-test comparison is available at http://xunit.codeplex.com/Wiki/View.aspx?title=Comparisons&referringTitle=WhyDidWeBuildXunit.

David Pokluda
I want to write code that will be execute before all test in all test classes. So your solution will work only on one test class
brzozow
No that is not true. If it is true that MSTest harness will instantiate new test class instance per test (which I believe is true for test isolation purpose) then the code in the constructor runs before each test method.
David Pokluda
It's true what you writing but it's not solving my issue. I want to reset some static properties before each test (I know it's bad design). I have 400 classes with Unit Tests. I don't want to write that code in all classes.
brzozow
Well, you can still do that in the constructor. But again TestInitialize would probably do the trick. The constructor approach (similar to xUnit) is more clear/obvious for people not familiar with the framework. That's why I would recommend it.
David Pokluda
The constructor way is actually the default option for test setup in XUnit.Net
robi
+1  A: 

I am not sure that this feature is possible in MsTest out of box like in other test frameworks (e.g. MbUnit).

If I have to use MsTest, then I am solving this by defining abstract class TestBase with [TestInitialize] attribute and every test which needs this behaviour derives from this base class. In your case, every test class in your assembly must inherit from this base...

And there is probably another solution, you can make your custom test attribute - but I have not tried this yet... :)

nihique
+3  A: 

[TestInitialize()] is what you need.

private string dir;

[TestInitialize()]
public void Startup()
{
    dir = Path.GetTempFileName();
    MakeDirectory(ssDir);
}

[TestCleanup()]
public void Cleanup()
{
    ss = null;
    Directory.SetCurrentDirectory(Path.GetTempPath());

    setAttributesNormal(new DirectoryInfo(ssDir));
    Directory.Delete(ssDir, true);
}


[TestMethod]
public void TestAddFile()
{
    File.WriteAllText(dir + "a", "This is a file");
    ss.AddFile("a");
    ...
}

[TestMethod]
public void TestAddFolder()
{
    ss.CreateFolder("a/");
    ...
}

This gives a new random temporary path for each test, and deletes it when it's done. You can verify this by running it in debug and looking at the dir variable for each test case.

FryGuy
StartUp method will be invoked only before executing test from this one class. I want to execute method before all tests from all classes in assembly
brzozow
FTQ "Is it possible to run specific method before each test?". I answered that. If what you mean is "a method run once, before all tests", then your answer is here: http://stackoverflow.com/questions/639326/mstest-executing-method-before-each-test/639339#639339
FryGuy
Thx for answer. But I want to execute method before each test in assembly - not before each in one specific class. Maybe my question was not clear enough.
brzozow