首页 文章

如何在Visual Studio 2017中运行NUnit测试?

提问于
浏览
125

我刚刚安装了VS 2017.我有一个使用NUnit进行测试用例的项目 . Ctrl R-T不再运行测试,并且测试资源管理器不再查找标有TestCase属性的任何测试用例 .

有没有办法让NUnit运行,或者我能找到更新?我从Nuget包管理器重新安装了NUnit到最新版本但没有任何改进 .

6 回答

  • 159

    要在Visual Studio 2017中运行或调试测试,我们需要安装“NUnit3TestAdapter” . 我们可以在任何VS中安装它,但它在VS“社区”版本中正常工作 . 要安装它,您可以通过Nuget Package添加 .

  • 3

    将NUnit测试适配器NuGet包添加到测试项目中

    或者安装Test Adapter visual studio扩展 . 有一个

    我更喜欢NuGet包,因为它将与您的项目使用的NUnit版本同步,因此将自动匹配任何构建服务器中使用的版本 .

  • 2

    您需要安装NUnitTestAdapter . NUnit的最新版本是3.x.y(3.6.1),您应该安装NUnit3TestAdapter以及NUnit 3.x.y

    要在Visual Studio 2017中安装NUnit3TestAdapter,请执行以下步骤:

    • 右键单击Project - >从上下文菜单中单击"Manage Nuget Packages.."

    • 转到“浏览”选项卡并搜索NUnit

    • 选择NUnit3TestAdapter - >单击右侧的安装 - >从预览弹出
      enter image description here
      单击确定

  • 14

    这一个帮助了我:https://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/07/27/getting-started-with-net-unit-testing-using-nunit.aspx

    基本上:

    • 在Nuget中添加NUnit 3库 .

    • 创建要测试的类 .

    • 创建一个单独的测试类,它应该在它上面有[TestFixture] .

    • 在Testing Class中创建一个函数,它应该在它上面有[Test] .

    • 然后进入TEST / WINDOW / TEST EXPLORER(顶部) .

    • 点击左侧的运行,它会告诉您已经通过的内容和失败的内容 .

    我的示例代码在这里:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using NUnit.Framework;
    
    namespace NUnitTesting
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
        }
    
        public class Maths
        {
            public int Add(int a, int b)
            {
                int x = a + b;
                return x;   
            }
        }
    
        [TestFixture]
        public class TestLogging
        {
         [Test]
         public void Add()
            {
                Maths add = new Maths();
                int expectedResult = add.Add(1, 2);
                Assert.That(expectedResult, Is.EqualTo(3));
            }
        }
    }
    

    这将返回true,如果您更改Is.Equal中的参数,它将失败,等等 .

  • 27
    • 您必须在VS中选择单元测试的处理器架构:
      Test > Test Settings > Default processor architecture

    必须打开

    • 测试适配器才能看到测试:(VisualStudio例如:
      Test->Windows->Test Explorer

    您可以在'VS-Output-Window'中选择下载'显示输出'并设置'测试'的其他信息

  • 0

    您需要安装3个NuGet包:

    • Nunit

    • NUnit3TestAdapter

    • Microsoft.NET.Test.Sdk

    玩写单元测试很有趣!

相关问题