为什么 xUnit Runner 找不到我的测试 [英] Why is the xUnit Runner not finding my tests
问题描述
我有一个 xUnit.net 测试如下:
静态类 MyTestClass{[事实]静态无效 MyTestMethod(){}}
VS 2012 的 xUnit 插件说:
<块引用>没有发现可以运行的测试.
TestDriven.net 运行良好 但提到了一些关于 Ad hoc 的内容:
<块引用>1 次通过,0 次失败,0 次跳过(参见任务列表"),耗时 0.47 秒(临时)
TeamCity、xunit.gui.exe
和 xunit.console.exe
和 Visual Studio 也找不到 TestMethod
(我已经安装了 xunit.runner.visualstudio
并且 VS 正在进行一些测试.)
是什么?
TL;DR your Test Classes must测试方法可以是 private
和/或 static
)
出于效率原因,xUnit 作者在运行程序中搜索测试类时选择不使用 BindingFlags.NonPublic
(MSIL 元数据表不索引 private
(/internal
) 类,因此反射可以实现的相对效率存在显着的性能差异).
由于上述原因,您的 class
是 private
的事实意味着它不会被接受.
测试方法是 private
和 static
的事实很好 - xUnit 从 1.0 开始就设计支持这两个方面.>
请注意,Visual Studio xUnit Runner 扩展、xunit.console.exe
(和 GUI)、xunit
MSBuild 任务、Resharper 和 CodeRush 都是一致的这(尽管可以说当测试类(即包含 Fact
派生注释的类)是 private
时,他们 [尤其是后两者] 可以做更多的事情来标记).
TestDriven.net 运行您的测试的原因是 TestDriven.net 的作者付出了巨大努力使其正常工作.它在内部使用一个特殊的 Test Runner 包装器/垫片(称为 Adhoc Runner)来运行您的测试.请注意,该方法实际上不是通过 xUnit.net 运行程序运行的,因此您在测试中放置的任何具有副作用的属性都不会被触发.
值得注意的是 NUnit(我很确定 MSTest)do 使用私有反射 [因此在 private
类中进行测试] 这可能是为什么它从来没有看起来像之前需要担心的重要事情.
注意:由此启用的副作用/技巧是,您可以将测试类设为 private
作为 Skip
ping 测试类中所有测试的快速方法 [和任何嵌套类].(可悲的是,这个星球上无意使用的案例远远超过了有意使用的案例!)
I have a xUnit.net Test as follows:
static class MyTestClass
{
[Fact]
static void MyTestMethod()
{
}
}
The xUnit plugin for VS 2012 says:
No tests found to run.
TestDriven.net runs it fine but mentions something about Ad hoc:
1 passed, 0 failed, 0 skipped (see 'Task List'), took 0.47 seconds (Ad hoc)
TeamCity, xunit.gui.exe
and xunit.console.exe
and Visual Studio also can't find TestMethod
(I've got xunit.runner.visualstudio
installed and VS is seeing some tests.)
What gives?
TL;DR your Test Classes must be public
(but your Test Methods can be private
and/or static
)
For reasons of efficiency, the xUnit authors have opted to not use BindingFlags.NonPublic
when searching for Test Classes in the runner (the MSIL metadata tables don't index private
(/internal
) classes to the same degree hence there is a significant performance difference in the relative efficiency that Reflection can thus achieve).
As a result of the above, the fact that your class
is private
means it doesn't get picked up.
The fact that the Test Method is private
and static
is fine - xUnit by design since 1.0 has supported both those aspects.
Note that the Visual Studio xUnit Runner extension, xunit.console.exe
(and the GUI), the xunit
MSBuild task, Resharper and CodeRush are all consistent in honouring this (although arguably they [especially the latter two] could do more to flag when a Test Class (i.e. class [potentially indirectly] containing Fact
-derived annoations) is private
).
The reason TestDriven.net runs your test is that the Author of TestDriven.net has put great effort into making it Just Work. It internally uses a special Test Runner wrapper/shim (termed the Adhoc Runner) to run your test. Be aware that the method is actually not being run via the xUnit.net runner and hence any attributes you put on your test that have side effects will not be triggered.
Notably NUnit (and I'm pretty sure MSTest) do use private reflection [and hence pick up tests in private
classes] which is probably why it never seemed an important thing for you to worry about before.
Note: A side effect / trick enabled by this is that you can make a Test Class private
as a quick way of Skip
ping all tests in a Test Class [and any nested classes]. (Sadly the cases on this planet of this being used unintentionally vastly outnumber the intentional cases of this though!)
这篇关于为什么 xUnit Runner 找不到我的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!