C# 为什么 Visual Studio 2012 找不到我的测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13533259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:55:48  来源:igfitidea点击:

Why does visual studio 2012 not find my tests?

c#.netvisual-studiounit-testing

提问by ctrl-alt-delor

I have some tests that use the built in Microsoft.VisualStudio.TestTools.UnitTesting, but can not get them to run.

我有一些使用内置的测试Microsoft.VisualStudio.TestTools.UnitTesting,但无法让它们运行。

I am using visual studio 2012 ultimate.

我正在使用 Visual Studio 2012 Ultimate。

I have a solution of two projects; One has tests, using Microsoft.VisualStudio.TestTools.UnitTesting, [TestClass]before the class, [TestMethod]before the test methods and reference Microsoft.VisualStudio.QualityTools.UnitTestFramework(version 10.0.0.0, runtime version v2.0.50727). I have tried dot-net framework 3.5, 4 and 4.5 others give a re-targeting error.

我有两个项目的解决方案;一个具有测试,using Microsoft.VisualStudio.TestTools.UnitTesting[TestClass]前级, [TestMethod]前测试方法与参考Microsoft.VisualStudio.QualityTools.UnitTestFramework(版本10.0.0.0,运行时版本V2.0.50727)。我已经尝试过 dot-net framework 3.5、4 和 4.5 其他人给出了重新定位错误。

I have tried to build the solution and project. Test explorer has the message `Build your solution to discover all available tests. Click "run all" to build, discover, and run all tests in your solution.

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

So the question is: How to I get visual studio to find the tests?

所以问题是:如何让 Visual Studio 找到测试?



Have also tried to follow this: http://msdn.microsoft.com/en-US/library/ms379625%28v=VS.80%29.aspxbut with no success: I get stuck in section getting started, when asked to right click and select create tests. There is no create tests.

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



I have this test(it compiles, but does not show up in test explorer):

我有这个测试(它编译,但没有出现在测试资源管理器中):

using Microsoft.VisualStudio.TestTools.UnitTesting;

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


I have now discovered (see deleted answer below) that it is because it is on a shared drive, but I don't as yet know how to get around it. (something about the security setting maybe).

我现在发现(请参阅下面已删除的答案)这是因为它位于共享驱动器上,但我还不知道如何解决它。(可能与安全设置有关)。

采纳答案by ctrl-alt-delor

The problem was that the test runner, is configured to not run tests from a remote drive. When I copied the project to a local drive it worked fine, but I needed it on a shared drive (can not remember why).

问题是测试运行器被配置为不从远程驱动器运行测试。当我将项目复制到本地驱动器时,它运行良好,但我需要在共享驱动器上使用它(不记得为什么)。

I read that it can be configured to work from a shared drive, but never got to do it as by the time I discovered the solution I had switched to MonoDevelop on Debian.

我读到它可以配置为从共享驱动器工作,但从来没有这样做,因为当我发现解决方案时,我已经在 Debian 上切换到 MonoDevelop。

回答by Todd Carter

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.

我发现解决此问题的最佳方法是创建一个 .proj msbuild 文件并将您遇到问题的单元测试项目添加到此文件中,然后使用命令行版本的 mstest 执行测试。我在我的 app.config 中发现了一个小的配置问题,它只在从 mstest 运行测试时出现 - 否则测试项目构建得很好。此外,您还会发现此方法的任何间接参考问题。一旦你可以使用 mstest 从命令行运行单元测试,你就可以做一个干净的解决方案,重建解决方案,你的测试应该被正确发现。

回答by CSharp

I sometimes get the same symptoms.

我有时会出现相同的症状。

What I did is:
1. Closed the Test Explorer window
2. Cleaned the solution
3. Rebuild the solution
4. Relaunched Test Explorer window from Test -> Windows -> Test Explorer.

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

And I got my test in Test Explorer window.

我在“测试资源管理器”窗口中进行了测试。

回答by DaBlue

Tests do not like async methods. Eg:

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

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

    }

After doing this:

这样做之后:

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

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

    }

It saw the test.

它看到了测试。

回答by Teja

The simplest thing you can do is, just drag and drop those files from the File Explorer to the Test project. Build the project again, and you are good to go!

您可以做的最简单的事情是,只需将这些文件从文件资源管理器拖放到测试项目中即可。再次构建项目,一切顺利!

OR

或者

Open the .csprojfile of your Test project in a text editor and manually include the file name to compile. Search for the Compiletag in the file and add your file which you want to run.

.csproj在文本编辑器中打开您的测试项目的文件并手动包含要编译的文件名。Compile在文件中搜索标签并添加要运行的文件。

<ItemGroup>
    <Compile Include="TestSettings.cs" />
    <Compile Include="TestLibrary.cs" />
    <Compile Include="UnitTest1.cs" />
</ItemGroup>

Add the Compile tag for your file and save it. Now run the tests. Your file with unit test will be detected and run.

为您的文件添加 Compile 标记并保存它。现在运行测试。将检测并运行带有单元测试的文件。

回答by Mike Perrenoud

These are all great answers, but there is one more reason that I know of; I just ran into it. In one of my tests I had a ReSharper message indicating that I had an unused private class. It was a class I'm going to use in an upcoming test. This actually caused allof my tests to disappear.

这些都是很好的答案,但我知道还有一个原因;我刚碰到它。在我的一次测试中,我收到一条 ReSharper 消息,表明我有一个未使用的私有类。这是我将在即将进行的测试中使用的课程。这实际上导致我所有的测试都消失了。

回答by Stephan Bauer

I had the same problem.. In my case it was caused by a private propertyTestContext.

我有同样的问题.. 在我的情况下,它是由私有财产引起的TestContext

Changing it to the following helped:

将其更改为以下内容有帮助:

public TestContext TestContext
{
    get;
    set;
}

After cleaning and building the solution (as described in @Ourjamie 's answer), the test methods in the affected test class were available in the Test Explorer.

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

回答by Ourjamie

I had same symptoms, but under different circumstances.

我有相同的症状,但在不同的情况下。

I had to add one additional step to Peter Lamberg's solution — Clean your solution/project.

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

My unittest project targets x64. When I created the project it was originally targeting x86.

我的单元测试项目针对 x64。当我创建该项目时,它最初是针对 x86 的。

After switching to x64 all my unit tests disappeared.

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

I had to go to the Test Menu -> Test Setting -Default Processor Architecture -> x64.

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

They still didn't show up.

他们还是没有出现。

Did a build.

做了一个构建。

Still didn't show up.

还是没有出现。

Finally did a Clean

最后做了一个清洁

Then they showed up.

然后他们出现了。

I find Clean Solution and Clean to be quite useful at getting the solutions to play ball when setting have changed. Sometimes I have to go to the extreme and delete the objand bindirectories and do a rebuild.

我发现 Clean Solution 和 Clean 在设置更改时获得解决方案来打球非常有用。有时我不得不走极端,删除objbin目录并进行重建。

回答by Si Zi

We were using xunit and solution worked for me and the team was deleting the folder %TEMP%\VisualStudioTestExplorerExtensions

我们正在使用 xunit 并且解决方案对我有用,团队正在删除文件夹 %TEMP%\VisualStudioTestExplorerExtensions

Read this thread.

阅读此线程。

Why is the Visual Studio 2015/2017 Test Runner not discovering my xUnit v2 tests

为什么 Visual Studio 2015/2017 Test Runner 没有发现我的 xUnit v2 测试

回答by mortey

I know this is an older question but with Visual Studio 2015 I was having issues where my newly created test class was not being recognized. Tried everything. What ended up being the issue was that the class was not "included in the project". I only found this on restarting Visual Studio and noticing that my test class was not there. Upon showing hidden files, I saw it, as well as other classes I had written, were not included. Hope that helps

我知道这是一个较旧的问题,但是在 Visual Studio 2015 中,我遇到了无法识别新创建的测试类的问题。尝试了一切。最终的问题是该课程没有“包含在项目中”。我只是在重新启动 Visual Studio 并注意到我的测试类不存在时才发现这一点。在显示隐藏文件时,我看到它以及我编写的其他类都没有包含在内。希望有帮助

回答by Aggromonster

One issue I've run into repetitively is not running Visual Studio (explicitly) as Administrator on Windows 8+. I tried several of the suggestions listed, but they did not get the unit tests to appear until I ran VS 2013 as Administrator (even though my shortcut is set to open VS that way, either opening the solution file or sometimes clicking the shortcut it doesn't actually open as admin).

我反复遇到的一个问题是没有在 Windows 8+ 上以管理员身份运行 Visual Studio(明确地)。我尝试了列出的几个建议,但是直到我以管理员身份运行 VS 2013 之后,它们才显示单元测试(即使我的快捷方式设置为以这种方式打开 VS,无论是打开解决方案文件还是有时单击快捷方式它都没有'实际上不是以管理员身份打开)。