MSTest: No tests are run because no tests are load

2019-02-01 15:42发布

I have a c# solution with the following structure:

mySolution
  myProject
  myProject.MSTests
    References
      Microsoft.VisualStudio.QualityTools.UnitTestFramework
    sutMSTests.cs

sutMSTests.cs:

[TestClass()] 
public class sutMSTests
{
    [TestMethod]
    public void MyTest0()
    {
        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(4, 2 + 2);
    } 
}

When I try to run the tests via Test, Run, All Tests In Solution, I get the following on the VS2008 status line:

No tests are run because no tests are loaded or the selected tests are disabled.

Test, Windows, Test View shows no tests.

Note: I created the tests manually (works for xUnit.net) instead of using Microsoft's wizards.

I've compared my hand created MSTest setup to the setup another test that I generated using the wizard and they appear to be sufficiently similar.

Question: What are the most likely causes of the error message above?

Edit 2010-02-25: More information:
I right clicked the Solution Items folder, and choose Add, New Project, type Test Projects,Test Documents::Visual Studio Test Project template.

The new project's default do nothing test "TestMethod1" was detected and passed.
However, my test did not show up ... so I copied and pasted my test method into the default test test project "TestProject1".

My test was detected in "TestProject" BUT not in its original location.

I closely compared the files, organization, and settings of "TestProject1" with my hand created test project.

At this point, I am guessing that some setting gets made by the Visual Studio Test Project template that is not easily detectable.

imo, it should be just as easy to create a test project by hand as it is to create one with the Visual Studio Test Project template.

please note: I'm not saying that I'm against using the Visual Studio Test Project template; for me, I like to understand what's behind the curtain since this makes me imho a much better programmer.

18条回答
我命由我不由天
2楼-- · 2019-02-01 16:19

Another idea for the Googlers out there. My problem was trying to get ignored tests running again. Same MS error message occurs if you remove the Ignore label. Does not automatically re-enable the test. This article takes you through the last step. http://richallen.blogspot.com/2008/05/ms-test-re-enabling-ignored-tests.html

查看更多
老娘就宠你
3楼-- · 2019-02-01 16:22

I was making use of a public TestContext TestContext method to write to the test output and changed the scope to private. This made every test not discoverable. Changing it back to public helped.

查看更多
仙女界的扛把子
4楼-- · 2019-02-01 16:24

Another one for the googlers - this one turned out to be my problem, and it's embarrassingly boneheaded of me. Make sure that your test project is set to build in whatever solution configuration you're using. If the test assembly isn't being built, VS won't be able to find any tests in the non-existent assembly, and you'll bang your head against the wall for a while :-)

查看更多
看我几分像从前
5楼-- · 2019-02-01 16:26

When you run into this issue, in Visual Studio, you have to create a Test Project. 1. Select Test in Tool bar and choose "New Test". Create your project and at this point create your test method. It should work after this point.

查看更多
太酷不给撩
6楼-- · 2019-02-01 16:27

I've just manually done this:

Created a new C# class library project with the following code:

namespace SO_Answer
{
    public class Class1
    {
        public void Test()
        {
            var k = "Hello";
        }
    }
}

Saved the project and then went to 'File->Add->New Project' and chose 'Test Project'. After VS created the unit test project, I added a reference to the class library project I created earlier.

In my test I have this code:

namespace Unit_Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        // You can use the following additional attributes as you write your tests:
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        #endregion

        /// <summary>
        /// The test method 1.
        /// </summary>
        [TestMethod]
        public void TestMethod1()
        {
            var f = new Class1();

        }
    }
}

The only code I added was the a using statement and the var f = new Class1(); statement. Looking at the MSTest runner, I can see TestMethod1 appear.

I can't think of a reason why your unit tests are not being picked up. The only time I've had this is because I was using the MSTest runner to try and view NUnit tests by mistake. Try starting from scratch.

查看更多
时光不老,我们不散
7楼-- · 2019-02-01 16:29

this is typical problem i have faced too. but the easiest solution I followed as my own is...just to build the project once and rebuild it again. so that you can resolve it.

查看更多
登录 后发表回答