在 C# 控制台应用程序中运行 NUnit 测试

发布于 2024-08-14 01:32:06 字数 429 浏览 2 评论 0原文

我需要在控制台应用程序中以编程方式运行 NUnit 测试。不能使用 NUnit 的 nunit-console.exe。我当前的代码是:

var testRunner = new SimpleTestRunner();
var package = new TestPackage("MyTests.dll", new List<string> { ("c:\MyTests\MyTests.dll" });
testRunner.Load(package);

当我调用 Load 时,NUnit 在当前进程的目录中查找 dll。因此,我收到类似“c:\MyTestRunner\bin\debug\MyTests.dll”之类的 FileNotFoundException。

如何强制它在不同的目录中查找 dll?

I need to run NUnit tests programmatically in a console app. Using NUnit's nunit-console.exe is not an option. My current code is:

var testRunner = new SimpleTestRunner();
var package = new TestPackage("MyTests.dll", new List<string> { ("c:\MyTests\MyTests.dll" });
testRunner.Load(package);

When I call Load, NUnit looks for the dll in the current process's directory. So I get a FileNotFoundException for something like "c:\MyTestRunner\bin\debug\MyTests.dll".

How can I force it to look for the dll in a different directory?

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

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

发布评论

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

评论(3

冷弦 2024-08-21 01:32:06

我不确定它如何与 NUnit 的 SimpleTestRunner() 一起使用,但使用 RemoteTestRunner() 对我来说效果很好:

TestPackage testPackage = new TestPackage(@"C:\YourProject.Tests.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestResult result = remoteTestRunner.Run(new NullListener());

确保 nunit.framework .dll 库位于测试程序集所在的文件夹中。

尝试一下 RemoteTestRunner() ;)

I'm not sure how it would work with NUnit's SimpleTestRunner() but using the RemoteTestRunner() instead works fine for me:

TestPackage testPackage = new TestPackage(@"C:\YourProject.Tests.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestResult result = remoteTestRunner.Run(new NullListener());

Make sure, the nunit.framework.dll library is in the the folder with your test assembly.

Give the RemoteTestRunner() a try ;)

挽袖吟 2024-08-21 01:32:06

你可以改变当前目录

Directory.SetCurrentDirectory(@"c:\MyTestRunner\bin\debug\");

更新:

看来这不是那么简单。
我环顾四周,有一篇 文章 和 < a href="https://stackoverflow.com/questions/1412235/how-do-you-instruct-nunit-to-load-an-assembles-dll-config-file-from-a-specific-d">堆栈关于这个问题的问题。

希望有帮助

You can change the current directory

Directory.SetCurrentDirectory(@"c:\MyTestRunner\bin\debug\");

Update:

It seems this is not so simple.
I`v looked around, there is an article and stack question on this issue.

Hope it helps

小耗子 2024-08-21 01:32:06

我的理解是标准 nunit-console.exe 在第二个应用程序域中运行其测试。这样它就可以将 AppDomainSetup.ApplicationBase 属性设置为所有 DLL 所在的目录。它还允许测试套件选择拥有自己的 MyTests.dll.config 副本。

我不熟悉 NUnit 源代码,快速浏览并没有发现任何可以设置第二个应用程序域的明显帮助程序类。然而,不应该有任何异常:创建一个带有更改的 ApplicationBase 属性的 AppDomainSetup ;创建一个新的AppDomain;在该应用程序域内创建您的 SimpleTestRunner ;并像您已经做的那样调用 Load

My understanding is that the standard nunit-console.exe runs its tests in a second app domain. This way it can set the AppDomainSetup.ApplicationBase property to the directory in which all the DLLs are located. It also allows the test suite to optionally have its own copy of MyTests.dll.config.

I'm not familiar with the NUnit source code, and a quick browse doesn't reveal any obvious helper classes that can set up a second app domain. However there shouldn't be anything out of the ordinary: create an AppDomainSetup with an altered ApplicationBase property; create a new AppDomain; create your SimpleTestRunner inside that app domain; and call Load like you're doing already.

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