【问题标题】:How to run a NUnit test?如何运行 NUnit 测试?
【发布时间】:2016-04-26 07:53:40
【问题描述】:

我想要一个独立的项目来测试通过 USB 连接的远程系统的一些功能。

所以我想在我的应用程序中使用 NUnit 的所有功能。

我目前是这样写的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NUnit.Framework;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
        }
    }

    [TestFixture]
    public class MyTest
    {
        [Test]
        public void MyTest()
        {
            int i = 3;
            Assert.AreEqual(3, i);
        }
    }
}

如何运行我的测试套件以及如何获得测试报告?

【问题讨论】:

    标签: c# nunit


    【解决方案1】:

    我知道实现您想要的两种可能的解决方案。 NUnit 团队在 nuget 上发布了 NUnit EngineNUnit Console

    使用 NUnit 引擎

    using NUnit.Engine;
    using NUnit.Framework;
    using System.Reflection;
    using System.Xml;
    using System;
    
    public class Program
    {
        static void Main(string[] args)
        {
            // set up the options
            string path = Assembly.GetExecutingAssembly().Location;
            TestPackage package = new TestPackage(path);
            package.AddSetting("WorkDirectory", Environment.CurrentDirectory);
    
            // prepare the engine
            ITestEngine engine = TestEngineActivator.CreateInstance();
            var _filterService = engine.Services.GetService<ITestFilterService>();
            ITestFilterBuilder builder = _filterService.GetTestFilterBuilder();
            TestFilter emptyFilter = builder.GetFilter();
    
            using (ITestRunner runner = engine.GetRunner(package))
            {
                // execute the tests            
                XmlNode result = runner.Run(null, emptyFilter);
            }
        }
    
        [TestFixture]
        public class MyTests
        {
            // ...
        }
    }
    

    从 nuget 安装 Nuget Engine package 以运行此示例。结果将在result 变量中。对于想要使用这个包的每个人都有一个警告:

    它不适合只想运行的用户直接使用 测试。

    使用标准的 NUnit 控制台应用程序

    using NUnit.Framework;
    using System.Reflection;
    
    class Program
    {
        static void Main(string[] args)
        {
            string path = Assembly.GetExecutingAssembly().Location;
            NUnit.ConsoleRunner.Program.Main(new[] { path });
        }
    
        [TestFixture]
        public class MyTests
        {
            // ...
        }
    }
    

    从 nuget 安装 NUnit EngineNUnit Console 软件包。在您的项目中添加对nunit3-console.exe 的引用。结果将保存在TestResult.xml 文件中。我不喜欢这种方法,因为你可以使用一个简单的批处理文件来达到同样的效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2011-01-24
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多