tags:

views:

1656

answers:

5

I searched SO with

no tests are run MSTest

but could not find relevant answer.

Scenario:

I have a c# solution with the following structure:

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

sutMSTests.cs:

using sut; // the System Under Test
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace sut.MSTests
{
[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
"No tests are run because no tests are loaded or the selected tests are disabled"
error message?

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.

Thank you.

Regards,
Gerry (Lowry)

A: 

Do you have a VSMDI file in your solution? I believe this file is required (NOT VERIFIED).

smaclell
The .vsmdi file appears to be not required. As described above in my edit, I created a test project with the vs template and saw the default test. After I deleted ALL .vsmdi files, the tests in the project created with the template still run. I'm trying to understand what special "tweak" the template does. It's frustrating because it's so easy with xUnit.net to manually create a test project. Thank you for your suggestion.
gerryLowry
No worries. I had to use MS Test my past few jobs and found its dependence on the VSMDI file a huge pain. It got even worst with certain source control providers. Good luck.
smaclell
A: 

Hi there.

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.

Cheers. Jas.

Jason Evans
+2  A: 

Possibly a bit late, but this question googles up well, I thought I'd throw some crumbs in for future googlers.

Bryan Cook suggests checking the ProjectTypeGuids in his blog post about Manually creating a MS Test Project. Apparently the magic GUIDs you need are {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} for c# and {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F} for VB. See his blog post for more details.

Dan F
+1  A: 

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 :-)

Dan F
@Dan F - you're right. You need to ensure the assembly is being built. In my case, this error was due to a combination of the following: (a) in the "Configuration Manager", the "build" button wasn't ticked (b) all other assembles were set to x32, this one was set to "Any CPU" in the "Configuration Manager" (c) it was complaining that it needed an assembly signed so I needed to set this in the project properties.
Gravitas
A: 

You code is trying to reference the attribtue like this:

[TestClass()]  
public class sutMSTests 
{ 

Try it without the ()'s like this:

[TestClass]  
public class sutMSTests 
{
MoMo