MSTest:没有运行任何测试,因为没有加载测试或选定的测试被禁用

发布于 2024-08-23 06:51:47 字数 1346 浏览 2 评论 0原文

我有具有以下结构的 ac# 解决方案:

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);
    } 
}

当我尝试通过“测试”、“运行”、“解决方案中的所有测试”运行测试时,我在 VS2008 状态行上看到以下内容:

没有运行测试,因为没有加载测试或所选测试已禁用。

测试,Windows,测试视图显示没有测试。

注意:我手动创建了测试(适用于 xUnit.net),而不是使用 Microsoft 的向导。

我将我手工创建的 MSTest 设置与我使用向导生成的另一个测试设置进行了比较,它们似乎非常相似。

问题:出现上述错误消息的最可能原因是什么?

编辑于2010年2月25日:更多信息:
我右键单击解决方案项目文件夹,然后选择“添加”、“新建项目”,键入“测试项目”、“测试文档::Visual Studio 测试项目”模板。

新项目的默认不执行任何操作的测试“TestMethod1”被检测到并通过。
但是,我的测试没有显示出来......所以我将我的测试方法复制并粘贴到默认的测试测试项目“TestProject1”中。

我的测试在“TestProject”中检测到,但不在其原始位置。

我将“TestProject1”的文件、组织和设置与我手工创建的测试项目进行了仔细比较。

此时,我猜测 Visual Studio 测试项目模板进行了一些不易检测的设置。

在我看来,手动创建测试项目应该与使用 Visual Studio 测试项目模板创建测试项目一样简单。

请注意:我并不是说我反对使用 Visual Studio 测试项目模板;我只是说我反对使用 Visual Studio 测试项目模板。对我来说,我喜欢了解幕后的内容,因为这使我成为一个更好的程序员。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

笨笨の傻瓜 2024-08-30 06:51:48

可能有点晚了,但是这个问题在谷歌上搜索得很好,我想我应该为未来的谷歌用户提供一些面包屑。

Bryan Cook 建议在其关于手动创建的博客文章中检查 ProjectTypeGuids一个 MS 测试项目。显然,您需要的神奇 GUID 是 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 对于 c# 和 {3AC096D0-A1C2-E12C- 1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F} 用于 VB。有关更多详细信息,请参阅他的博客文章。

如果博客文章消失,您需要在 csproj 文件的主属性组中添加以下元素:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

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.

In case the blog post ever goes away you need to add the following element in the main property group in the csproj file:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
奢望 2024-08-30 06:51:48

谷歌员工的另一个想法。我的问题是试图让被忽略的测试再次运行。如果删除“忽略”标签,则会出现相同的 MS 错误消息。不会自动重新启用测试。本文将带您完成最后一步。 http://richallen.blogspot.com/2008 /05/ms-test-re-enabling-ignored-tests.html

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

半衬遮猫 2024-08-30 06:51:48

修复很简单,尽管如果 Visual Studio 正常工作,则不需要修复。

总结一下其他人的贡献,特别是在此文章,这就是最终对我有用的内容:

  • 配置管理器确保选择您的测试项目来构建您正在使用的任何配置和平台(例如:configuration=Debug 和 platform=x86)
  • 使用 方法属于 [TestClass] 并且它都标记为 [TestMethod],并且不使用属性 [Ignore]
  • 使用测试视图查找你的测试。
    测试视图
  • 打开“属性”窗口 (F4),并确保已启用测试
    启用

The fix is simple, even though it shouldn't be needed, if Visual Studio worked as it should.

To sum up what others have contributed, particularly in this article, here's what ultimately worked for me:

  • Use the Configuration Manager to make sure your test project is selected to build in whatever configuration and platform you're using (ex: configuration=Debug and platform=x86)
  • Make sure your method belongs to a [TestClass] and that it's both marked [TestMethod], and NOT using the attribute [Ignore]
  • Use Test View to find your test.
    Test View
  • Open your Properties window (F4), and make sure your test is enabled
    Enabled
小姐丶请自重 2024-08-30 06:51:48

原始海报确实这样做了,但我在没有这样做之后到达这里:

确保 [TestClass] 在顶部声明,在范围内为公共:

namespace XYZ.API.Repository.Tests
{
    [TestClass()]
    public class ClientTests
    {

The original poster did do this, but I arrived here after not having done this:

Be sure that [TestClass] is declared at the top, public in scope:

namespace XYZ.API.Repository.Tests
{
    [TestClass()]
    public class ClientTests
    {
丑丑阿 2024-08-30 06:51:48

我收到了同样的消息,结果是我的单元测试项目位于网络驱动器上。一旦我将其移至本地,它就运行良好。如果您遇到此错误,只需尝试一下即可。
约翰

I was receiving the same message and it turned out to be that I had my unit test project on a network drive. Once I moved it local it ran fine. Just something to try if you get this error.
John

初懵 2024-08-30 06:51:48

我刚刚手动完成此操作:

使用以下代码创建了一个新的 C# 类库项目:

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

保存项目,然后转到“文件->添加->新项目”并选择“测试项目”。 VS 创建单元测试项目后,我添加了对之前创建的类库项目的引用。

在我的测试中,我有以下代码:

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();

        }
    }
}

我添加的唯一代码是 using 语句和 var f = new Class1(); 语句。查看 MSTest 运行程序,我可以看到出现 TestMethod1

我想不出你的单元测试没有被接受的原因。我唯一一次遇到这种情况是因为我错误地使用 MSTest 运行程序尝试查看 NUnit 测试。尝试从头开始。

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.

弱骨蛰伏 2024-08-30 06:51:48

这可能是另一个原因。检查解决方案是否在 64 位上运行。如果是这样,请将其更改为 x86。

This could be another reason. Check whether the solution is running on 64bit. If so change it to x86.

水波映月 2024-08-30 06:51:48

这肯定是一个错误,并且绝对是一个痛苦,特别是当您必须单独重新启用每个测试方法时。然而,一点横向思考产生了更好的解决方案 - 重命名测试类并重建。然后将其重命名回来。似乎有效。
哎呀 - 不,不是。重命名该类是可行的,但是当它被重命名回来时,它会恢复到原始设置。
诀窍是关闭 Visual Studio 并删除 .vsmdi(Visual Studio 测试元数据)文件。这将被重新生成。

This must be a bug and is an absolute pain especially as you have to reenable every single test method individually. However a bit iof lateral thinking produced a better solution - rename the test class and rebuild. Then rename it back. Seems to work.
Ooops - no it doesn't. Renaming the class works but when it's renamed back it reverts to the original settings.
The trick is to close down Visual Studio and delete the .vsmdi (visual studio test meta data) file. This will be regenrated.

抱猫软卧 2024-08-30 06:51:48

当您遇到此问题时,您必须在 Visual Studio 中创建一个测试项目。
1. 在工具栏中选择“测试”,然后选择“新建测试”。创建您的项目并在此时创建您的测试方法。在此之后它应该起作用。

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.

梦冥 2024-08-30 06:51:48

对于后代:我刚刚发现将测试标记为静态会使它们默默地无法出现在测试列表中。显然这是不允许的。

For posterity: I just found that marking tests as static made them silently fail to appear in the test list. Apparently that isn't allowed.

-黛色若梦 2024-08-30 06:51:48

其他答案都不适合我。我不断在输出窗口中收到以下消息:

------ Discover test started ------
========== Discover test finished: 2 found (0:00:00.1310428) ==========
No tests found to run.

就我而言,问题仅在我创建名为 0-Local 的新配置后发生。我必须将 true 添加到我的 csproj 文件的相关部分,所以它看起来像这样:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == '0-Local|AnyCPU'">
  <DebugSymbols>true</DebugSymbols>
  <OutputPath>bin\0-Local\</OutputPath>
</PropertyGroup>

None of the other answers worked for me. I kept getting the following message in the output window:

------ Discover test started ------
========== Discover test finished: 2 found (0:00:00.1310428) ==========
No tests found to run.

In my case, the problem only occurred after I created a new configuration called 0-Local. I had to add <DebugSymbols>true</DebugSymbols to the relevant section of my csproj file, so it looks like this:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == '0-Local|AnyCPU'">
  <DebugSymbols>true</DebugSymbols>
  <OutputPath>bin\0-Local\</OutputPath>
</PropertyGroup>
因为看清所以看轻 2024-08-30 06:51:48

您的解决方案中有 VSMDI 文件吗?我相信这个文件是必需的(未验证)。

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

苏辞 2024-08-30 06:51:48

这也是我遇到过的典型问题。但我自己遵循的最简单的解决方案是......只需构建一次项目并再次重建它。以便您可以解决它。

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.

半暖夏伤 2024-08-30 06:51:48

遇到了同样的问题,但阅读了之前的答案,一切看起来都很好。

就我而言,我刚刚运行测试套件,做了一个小更改,构建了解决方案并尝试运行测试。不去。我尝试了几次构建并寻找其他人尝试过的问题。还是不行。

我在其中一种测试方法中按 Enter 键添加新方法,然后按 F6 构建解决方案,然后单击“运行单元测试”。

宾果!一切都很顺利。

Had this same issue but reading over the previous answers, everything looked good.

In my case, I had just run the test suite made a small change, built the solution and tried to run the test. No go. I tried building a couple more times and looking for problems other people had tried. Still no go.

I hit enter in one of my test methods to add a new and hit F6 to build the solution and clicked run Unit Tests.

Bingo! Everything ran smoothly.

剪不断理还乱 2024-08-30 06:51:48

我使用 public TestContext TestContext 方法写入测试输出,并将范围更改为 private。这使得每个测试都无法被发现。将其改回 public 有帮助。

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.

弄潮 2024-08-30 06:51:48

另一种适用于使用 NUnit 的 google 用户,尤其是那些从 MS Unit 测试迁移到 NUnit 的用户。请从项目文件中删除将项目标识为 MS 测试项目的项目类型指南。

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Another one for googlers using NUnit, especially those who have migrated from MS Unit test to NUnit. Please remove the project type Guids that are identifying the project as MS Test project from the project file.

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
完美的未来在梦里 2024-08-30 06:51:48

如果您的代码是 CLI(托管 C++)并且您的测试类继承自抽象基类,请确保您的测试类实现该基类的纯虚方法。
如果您没有实现它,您可能会看到“没有发现可以运行的测试”消息。

If your code is CLI (managed c++) and your test class inherits from an abstract base class, make sure that your test class implements the base's pure virtual method.
if you didn't implement it, you may see the "no tests found to run" message.

沉鱼一梦 2024-08-30 06:51:47

谷歌用户的另一个问题——这个问题原来是我的问题,而且我真是愚蠢得令人尴尬。确保您的测试项目设置为在您使用的任何解决方案配置中构建。如果没有构建测试程序集,VS 将无法在不存在的程序集中找到任何测试,并且您将头撞在墙上一段时间:-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文