当前位置: 首页 > 编程语言 > C#

如何运行NUnit测试?分享

时间:2023-04-10 19:34:05 C#

如何运行NUnit测试?我想要一个独立的项目来测试通过USB连接的远程系统的某些功能。所以我想在我的应用程序中使用NUnit的所有功能。我目前写:使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用System.Web;使用NUnit.Framework;命名空间ConsoleApplication1{类程序{staticvoidMain(string[]args){Console.ReadLine();}}[TestFixture]publicclassMyTest{[Test]publicvoidMyTest(){inti=3;Assert.AreEqual(3,i);如何运行我的测试套件以及如何获得测试报告?我知道有两种可能的解决方案来实现你想要的。NUnit团队在nuget上发布了NUnitEngine和NUnitConsole。使用NUnit.Engine使用NUnit引擎;使用NUnit.Framework;使用System.Reflection;使用System.Xml;使用系统;publicclassProgram{staticvoidMain(string[]args){//设置选项stringpath=Assembly.GetExecutingAssembly().Location;测试包包=新测试包(路径);package.AddSetting("WorkDirectory",Environment.CurrentDirectory);//准备引擎ITestEngineengine=TestEngineActivator.CreateInstance();var_filterService=engine.Services。获取服务();ITestFilterBuilder构建器=_filterService.GetTestFilterBuilder();TestFilteremptyFilter=builder.GetFilter();using(ITestRunnerrunner=engine.GetRunner(package)){//执行测试XmlNoderesult=runner.Run(null,emptyFilter);}}[TestFixture]publicclassMyTests{//...}}从nuget安装NugetEngine包以运行此示例。结果将在结果变量中。警告每个想要使用这个包的人:它不适合只想运行测试的用户直接使用。使用使用NUnit.Framework的标准NUnit控制台应用程序;使用System.Reflection;类程序{staticvoidMain(string[]args){stringpath=Assembly.GetExecutingAssembly().Location;NUnit.ConsoleRunner.Program.Main(new[]{path});}[TestFixture]publicclassMyTests{//...}}从nuget安装NUnit引擎和NUnit控制台包。在您的项目中添加对nunit3-console.exe的引用。结果将保存在TestResult.xml文件中。我不喜欢这种方法,因为您可以使用简单的批处理文件来实现相同的目的。以上是C#学习教程:HowtorunNUnittests?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: