Why does visual studio 2012 not find my tests?

2019-01-16 03:04发布

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

I am using 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.

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?


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

30条回答
够拽才男人
2楼-- · 2019-01-16 03:29

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.

查看更多
爷、活的狠高调
3楼-- · 2019-01-16 03:29

For Me the solution was just a little bit less complicated.

I had just brought an existing solution on to my machine (cloned from gitHub) and we do not track the auto-generated .cs files that Visual Studio created. (For each feature file there is a .cs file with the same name)

Opening the solution without having the associated .cs files actually allow me to navigate to the bound methods, so it appeared as if specflow was wired up properly, but I was not able to view the test names in the Test Explorer.

For this problem simply excluding the feature files from the project and then re-including them, forced VS to regenerate these auto generated codebehind files.

After that, I was able to view the tests in the test explorer.

查看更多
时光不老,我们不散
4楼-- · 2019-01-16 03:30

Please add the keyword public to your class definition. Your test class is currently not visible outside its own assembly.

namespace tests {
    [TestClass]
    public class SimpleTest {
        [TestMethod]
        public void Test() {
            Assert.AreEqual("a","a", "same");
        }
    }
}
查看更多
仙女界的扛把子
5楼-- · 2019-01-16 03:31

I had the same problem.. In my case it was caused by a private property 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.

查看更多
三岁会撩人
6楼-- · 2019-01-16 03:34

This sometimes works.

Check that the processor architecture under Test menu matches the one you use to build the solution.

Test -> Test Settings -> Default Processor Architecture -> x86 / x64

As mentioned in other posts, make sure you have the Test Explorer window open. Test -> Windows -> Test Explorer

Then rebuilding the project with the tests should make the tests appear in Test Explorer.

Edit: As Ourjamie pointed out below, doing a clean build may also help. In addition to that, here is one more thing I encountered:

The "Build" checkbox was unticked in Configuration Manager for a new test project I had created under the solution.

Go to Build -> Configuration Manager. Make sure your test project has build checkbox checked for all solution configurations and solution platforms.

查看更多
太酷不给撩
7楼-- · 2019-01-16 03:34

I have Visual Studio 2012 and i couldn't see the Tests in Test Explorer,

So I installed the following: NUnit Test Adapter

That fixed the issue for me !

查看更多
登录 后发表回答