我想在我正在使用的辅助方法中获取当前正在执行的 NUnit 测试。我们实际上在这里使用 NUnit 进行集成测试——而不是单元测试。当测试完成时,我们希望测试完成后清理一些日志文件。目前,我已经使用 StackFrame 类解决了这个问题:
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();
//Do other testing stuff
helper.HelperMethod();
}
[Test]
public void MyTest2()
{
var helper = new TestHelper();
//Do some more testing stuff
helper.HelperMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
这工作得很好,除了在某些情况下我想将 TestHelper 类作为我的装置的一部分,如下所示:
[TestFixture]
class Fix
{
private TestHelper helper;
[Setup]
public void Setup()
{
helper = new TestHelper();
}
[TearDown]
public void TearDown()
{
helper.HelperMethod();
}
[Test]
public void MyTest()
{
//Do other testing stuff
}
[Test]
public void MyTest2()
{
//Do some more testing stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我不能简单地使此类成为全局固定装置,因为有时单个测试会多次使用它,有时测试根本不需要使用它。有时测试需要将特定属性附加到 TestHelper...之类的东西。
因此,我希望能够以某种方式获取当前正在执行的测试,而不必在我正在查看的数千个测试用例中手动重复夹具和测试的名称。
有没有办法获得这样的信息?
NUnit 2.5.7添加了一个“实验性”TestContext 类。它包含的属性之一是 TestName。我没有尝试过,所以我不知道TearDown方法中是否有该信息。
| 归档时间: |
|
| 查看次数: |
4478 次 |
| 最近记录: |