我有一些测试是使用Microsoft.VisualStudio.TestTools内置的。单元测试,但不能让它们运行。

我使用visual studio 2012终极版。

我有两个项目的解决方案;一个有测试,使用Microsoft.VisualStudio.TestTools。UnitTesting, [TestClass]在类之前,[TestMethod]在测试方法之前,并参考Microsoft.VisualStudio.QualityTools.UnitTestFramework(版本10.0.0.0,运行时版本v2.0.50727)。我已经尝试过。net框架3.5,4和4.5其他人给出了一个重定向错误。

我已经尝试构建解决方案和项目。测试资源管理器显示“构建解决方案以发现所有可用的测试”。单击“全部运行”可在解决方案中构建、发现和运行所有测试。

问题是:如何让visual studio找到测试?


我也试过遵循这个:http://msdn.microsoft.com/en-US/library/ms379625%28v=VS.80%29.aspx但没有成功:当我被要求右键单击并选择创建测试时,我被困在了开始的部分。没有创建测试。


我有这个测试(它编译,但不显示在测试资源管理器):

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace tests {
    [TestClass]
    public class SimpleTest {
        [TestMethod]
        public void Test() {
            Assert.AreEqual("a","a", "same");
        }
    }
}

我现在发现(见下面删除的答案),这是因为它在一个共享驱动器上,但我还不知道如何绕过它。(可能是关于安全设置的问题)。


当前回答

我复制粘贴了方法声明,其中包括一个输入字符串参数。忘记删除输入参数。

  public void ExtractValueFromLineTest(string input) {}//test not discovered because of the string input param

其他回答

我们使用xunit,解决方案为我工作,团队正在删除文件夹%TEMP%\VisualStudioTestExplorerExtensions

阅读这篇文章。

为什么Visual Studio 2015/2017测试运行器没有发现我的xUnit v2测试

我有时也会有同样的症状。

我所做的是: 1. 关闭测试资源管理器窗口 2. 清洗溶液 3.重新构建解决方案 4. 从Test -> Windows ->测试资源管理器重新启动测试资源管理器窗口。

我在测试资源管理器窗口中得到了我的测试。

由于这些答案都没有涉及到我遇到的原因,对于这个问题,我也将在这里添加我的答案。

在我的例子中,这是一个复制粘贴代码的问题。我们有一个TestProjectA,其中有特定类型的测试,我们希望将它们拉出到它自己的测试项目testprojecb中。现在的问题是,当TestProjectA中的测试继续正常显示时,新的TestProjectB中的测试不会在VS测试资源管理器中显示——除非你特别构建了TestProjectB或手动右键单击那里的测试并运行它。

原因是在复制粘贴代码时,一些名称空间没有得到更新:

在命名空间TestProjectA中的TestClassA中有一个TestMethodA()。ModuleA的文件路径\TestProjectA\TestClassA.cs 以及命名空间TestProjectA中的TestClassB中的TestMethodA()。ModuleA的文件路径\ testprojecb \TestClassB.cs

因此,visual Studio在输出窗口中输出一条消息,表明这两个项目的testd是相同的,并且由于两个项目之间的testd冲突,在测试资源管理器中继续显示TestProjectA而不是testprojecb。将命名空间更正为TestProjectB。ModuleB for \ testprojecb \TestClassB.cs修复了这个问题。

我阅读了所有现有的答案,发现我的(xUnit)测试没有被发现,因为我删除了“未使用的”引用,更确切地说,删除了xUnit . execute .desktop。重新安装xUnit包解决了我的问题。

I found the best way to troubleshoot this issue is to create a .proj msbuild file and add your unit test projects which you hare having an issue into this file and execute the tests using the command line version of mstest. I found a small configuration issue in my app.config which only appeared when running the tests from mstest - otherwise the test project built just fine. Also you will find any indirect reference issues with this method as well. Once you can run the Unit test from the command line using mstest you can then do a clean solution, rebuild solution and your test should be discovered properly.