在 NUnit [TearDown] 中,如何确定测试是否是 运行 solo?
In NUnit [TearDown], how to find out whether the test was run solo?
因此,在 TearDown, I have got the info about the test outcome 和测试结果消息中,但我想具体处理测试是 运行 单独(测试会话中的单个测试)还是已开始的事情在一整套测试中(例如 "Run all tests/All tests from Solution")。
目标是检测开发人员是单独启动测试(从 Visual Studio 内部手动启动)还是使用持续集成系统启动测试。
这是我目前拥有的:
/// <summary>
/// A helper function for resolving problems when string comparison fails.
/// </summary>
/// <remarks>
/// Intended to be used to analyze the detected differences.
/// </remarks>
[TearDown]
public void CompareNonMatchingStringsOnFailure() {
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed) {
string outputMessage = TestContext.CurrentContext.Result.Message;
if (outputMessage.StartsWith("Expected string to be ")) {
// do extended comparison
// This should only run on single runs, which were initiated manually from visual studio
//...
}
}
}
如何在 TearDown 方法中获取有关测试 run/session 的信息?
您不能在拆解代码中执行此操作,因为 (1) 拆解仍然是测试的一部分,并且 (2) 测试不应该知道 运行 他们是谁,为什么他们是运行ning等。执行环境知道测试,但测试不知道执行环境。事实上,NUnit 为了确保在每个环境中工作相同,NUnit 付出了很多努力。虽然有一些方法可以欺骗 NUnit,但它们通常都是糟糕的想法并且依赖于版本。
这是您可以做的...
- 创建一个从您的夹具继承的夹具。
- 将您想要的逻辑放入新的夹具 TearDown 方法中。
- 将新灯具标记为
[Explicit]
。
- 不要向新夹具添加任何类别。
因为 (3) 当您 运行 所有测试。
只能运行明确。由于它没有类别,这意味着它只能 运行 按名称...即通过选择整个夹具或单个测试。
这完全不是您要求的。如果您 运行 整个夹具,您将获得所有继承测试方法的完整比较。但是,它可能足以满足您要完成的任务。
因此,在 TearDown, I have got the info about the test outcome 和测试结果消息中,但我想具体处理测试是 运行 单独(测试会话中的单个测试)还是已开始的事情在一整套测试中(例如 "Run all tests/All tests from Solution")。
目标是检测开发人员是单独启动测试(从 Visual Studio 内部手动启动)还是使用持续集成系统启动测试。
这是我目前拥有的:
/// <summary>
/// A helper function for resolving problems when string comparison fails.
/// </summary>
/// <remarks>
/// Intended to be used to analyze the detected differences.
/// </remarks>
[TearDown]
public void CompareNonMatchingStringsOnFailure() {
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed) {
string outputMessage = TestContext.CurrentContext.Result.Message;
if (outputMessage.StartsWith("Expected string to be ")) {
// do extended comparison
// This should only run on single runs, which were initiated manually from visual studio
//...
}
}
}
如何在 TearDown 方法中获取有关测试 run/session 的信息?
您不能在拆解代码中执行此操作,因为 (1) 拆解仍然是测试的一部分,并且 (2) 测试不应该知道 运行 他们是谁,为什么他们是运行ning等。执行环境知道测试,但测试不知道执行环境。事实上,NUnit 为了确保在每个环境中工作相同,NUnit 付出了很多努力。虽然有一些方法可以欺骗 NUnit,但它们通常都是糟糕的想法并且依赖于版本。
这是您可以做的...
- 创建一个从您的夹具继承的夹具。
- 将您想要的逻辑放入新的夹具 TearDown 方法中。
- 将新灯具标记为
[Explicit]
。 - 不要向新夹具添加任何类别。
因为 (3) 当您 运行 所有测试。
只能运行明确。由于它没有类别,这意味着它只能 运行 按名称...即通过选择整个夹具或单个测试。
这完全不是您要求的。如果您 运行 整个夹具,您将获得所有继承测试方法的完整比较。但是,它可能足以满足您要完成的任务。