所以事实证明我正在寻找 ITestClassCommand.EnumerateTestMethods() 方法。
- 默认的 xUnit.NET 测试运行程序将遍历测试程序集中的所有类。
- 对于每一个,它将检查一个 RunWithAttribute;这是您覆盖用于识别包含测试的方法的 ITestClassCommand 实现的机会。(RunWithNUnit 就是一个很好的例子)
- 调用 ITestClassCommand.EnumerateTestMethods() 来处理测试类并返回测试方法的 IEnumerable。
- 然后将每个测试 IMethodInfo 传递给 ITestClassCommand.EnumerateTestCommands(IMethodInfo testMethod) 以获取 ITestCommands 的 IEnumerable
- 然后执行每个 ITestCommand 并有机会返回结果。
在我上面的例子中,我需要类似的东西:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class RunWithMyTestClassCommandAttribute : RunWithAttribute
{
public RunWithMyTestClassCommandAttribute()
: base(typeof(MyTestClassCommand)) {}
}
然后我可以装饰我上面的例子:
[RunWithMyTestClassCommand]
public class AdditionSpecification
{
static int result;
public void Because()
{
result = 2 + 2;
}
public void Result_is_non_zero()
{
Assert.True(result <> 0);
}
public void Result_is_correct()
{
Assert.Equal(4, result);
}
}
最后,在 MyTestClassCommand 中,我有机会在 EnumerateTestMethods() 和 EnumerateTestCommands(IMethodInfo testMethod) 之间使用我想要定位的任何逻辑并构造 ITestCommand 实例,这些实例作为单独的测试执行。
顺便说一句,在研究这个问题的过程中,我在 xUnit.NET 框架中遇到了一个小错误,其中由 EnumerateTestMethods() 生成的自定义 IMethodInfo 从未出现在 EnumerateTestCommands(..) 中,因为它正在被测试解包和重新包装亚军或它的工厂之一。
我将此问题提交给 codeplex 上的 xUnit 项目,并于2009年 5 月 30 日更正了 xUnit.NET 1.5 CTP 2