我有一些测试是使用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");
        }
    }
}

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


当前回答

我有同样的症状,但情况不同。

我不得不在Peter Lamberg的解决方案中添加一个额外的步骤——清理你的解决方案/项目。

我的unittest项目的目标是x64。当我创建这个项目时,它最初是针对x86的。

切换到x64后,我所有的单元测试都消失了。

我必须去测试菜单->测试设置-默认处理器架构-> x64。

他们还是没有出现。

做了一个构建。

还是没出现。

终于做了一次清洁

然后他们出现了。

我发现清洁解决方案和清洁是相当有用的,让解决方案发挥球时,设置已经改变。有时我不得不走到极端,删除obj和bin目录,并进行重建。

其他回答

这里没有一个解决方案对我有帮助。一个解决方案的测试不会被发现,而另一个解决方案引用相同的项目工作得很好。我最终通过删除solutionname.v12解决了这个问题。缩文件。

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.

请将关键字public添加到类定义中。您的测试类目前在其自身程序集之外不可见。

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

测试不喜欢异步方法。例如:

    [TestMethod]
    public async void TestMethod1()
    {
        TestLib oLib = new TestLib();
        var bTest = await oLib.Authenticate();

    }

这样做之后:

    [TestMethod]
    public void TestAuth()
    {
        TestMethod1();
    }

    public async void TestMethod1()
    {
        TestLib oLib = new TestLib();
        var bTest = await oLib.Authenticate();

    }

它看到了测试。

我也有同样的问题。在我的例子中,它是由私有属性TestContext引起的。

将其更改为以下方式会有所帮助:

public TestContext TestContext
{
    get;
    set;
}

在清理和构建解决方案之后(如@Ourjamie的回答所述),受影响的测试类中的测试方法在测试资源管理器中是可用的。