I want to get the current NUnit executable test in a helper method that I use. In fact, we use NUnit for integration tests, not unit tests. When the test finishes, we would like the test to clear some log files when it was done. I currently hack this using the StackFrame class:
class TestHelper
{
string CurrentTestFixture;
string CurrentTest;
public TestHelper()
{
var callingFrame = new StackFrame(1);
var method = callingFrame.GetMethod();
CurrentTest = method.Name;
var type = method.DeclaringType;
CurrentTestFixture = type.Name;
}
public void HelperMethod()
{
var relativePath = Path.Combine(CurrentTestFixture, CurrentTest);
Directory.Delete(Path.Combine(Configurator.LogPath, relativePath));
}
}
[TestFixture]
class Fix
{
[Test]
public void MyTest()
{
var helper = new TestHelper();
helper.HelperMethod();
}
[Test]
public void MyTest2()
{
var helper = new TestHelper();
helper.HelperMethod();
}
}
This works just fine, unless I would like to make the TestHelper class part of my tool, for example:
[TestFixture]
class Fix
{
private TestHelper helper;
[Setup]
public void Setup()
{
helper = new TestHelper();
}
[TearDown]
public void TearDown()
{
helper.HelperMethod();
}
[Test]
public void MyTest()
{
}
[Test]
public void MyTest2()
{
}
}
, , . TestHelper... - .
- , .
?