需要帮助将 nUnit 测试转移到单独的项目中

发布于 2024-07-11 13:52:24 字数 1175 浏览 8 评论 0原文

当单元测试类是主项目(TestAccount)的一部分时,一切正常。

我读过的关于单元测试的每一篇文章都建议将测试放在一个单独的项目中,所以我......

  • 添加了另一个项目 (TestAccount.UnitTests) 解决方案
  • 将单元测试类 (AccountTests.vb) 移至 TestAccount.UnitTests
  • ,并将 TestAccount.UnitTests 中的引用添加到 TestAccount (copy local = true)

解决方案编译时不会出现任何警告。 但是,nUnit 无法访问主项目,并且每次测试都会出现以下错误:

System.IO.FileNotFoundException:无法加载文件或程序集“TestAccount,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”或其依赖项之一。 该系统找不到指定的文件。

我究竟做错了什么?

(在将 nunit.framework.dll 添加到 GAC 之前,我也遇到了类似的问题)

解决方案资源管理器的屏幕截图 http://img440.imageshack.us/img440/4862/nunitsolutionexploreree3.jpg

Visual Studio 2005
.NET 2.0
nUnit 2.4.8(.NET 2.0 版本)

[编辑] 我仅在从 Visual Studio 运行 nUnit(作为外部命令)时遇到此问题。 如果我独立加载 nUnit 控制台,它工作正常。

[编辑]奥马尔:是的,我有其他项目的参考。 这是我的解决方案资源管理器的屏幕截图

我想我可能只需要运行单独的 nUnit 控制台(而不是通过 VS 的外部工具)。

Everything works fine when the unit tests class is part of the main project (TestAccount).

Every article I've read about unit testing recommends putting the tests in a separate project, so I...

  • added another project
    (TestAccount.UnitTests) to the solution
  • moved the unit tests class (AccountTests.vb) to TestAccount.UnitTests
  • and added a reference in TestAccount.UnitTests to TestAccount (copy local = true)

The solution compiles without any warnings. However, nUnit can't access the main project and gives the following error for each test:

System.IO.FileNotFoundException : Could not load file or assembly 'TestAccount, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

What am I doing wrong?

(I was having similar trouble with nunit.framework.dll until I added it to the GAC)

screenshot of Solution Explorer http://img440.imageshack.us/img440/4862/nunitsolutionexploreree3.jpg

Visual Studio 2005
.NET 2.0
nUnit 2.4.8 (.NET 2.0 version)

[Edit] I only have this problem when running nUnit from Visual Studio (as external command). If I load the nUnit console independently, it works fine.

[Edit] Omar: yes, I have the reference to the other project. Here is a screenshot of my solution explorer

I think I might just have to run the nUnit console separately (instead of through VS's External Tools).

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

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

发布评论

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

评论(4

笑看君怀她人 2024-07-18 13:52:24

我发现了一个 错误报告说 Visual Studio 错误地扩展了 TargetPath 和 TargetDir 宏。 它们扩展到 obj\ 目录,而不是 bin\

[更新] 问题/解决方案实际上在 Visual Studio 支持 文档部分。 由于“目标”宏指向 obj\,因此您不能开箱即用地使用它们。 我最终在“参数”字段中使用了以下表达式:

$(ProjectDir)bin/Debug/$(TargetName)$(TargetExt)

I found a bug report that says Visual Studio incorrectly expands the TargetPath and TargetDir macros. They expand to the obj\ directory, not bin\

[Update] The problem/solution is actually discussed in the Visual Studio Support section of the documentation. Since the 'Target' macros point to obj\, you can't use them out-of-the-box. I ended up using the following expression in the Arguments field:

$(ProjectDir)bin/Debug/$(TargetName)$(TargetExt)
翻身的咸鱼 2024-07-18 13:52:24

这可能听起来很愚蠢,但我要说的是,您是否为您的测试项目提供了对其他项目的引用?

此类问题常见的其他问题是使用无法从其他项目/命名空间访问的内部类。

This might sound silly but I'm going to go for the obvious have you given your test project a reference to the other project?

Other issues that are common for this kind of issue is the use of internal classes which can't accessed from another project/namespace.

笑饮青盏花 2024-07-18 13:52:24

还要确保您的程序集输出路径与 NUnit 配置中设置的路径相对应。 我有相同的设置,但不必设置“copy local = true”

Also make sure your assembly output path corresponds to what is set up in your NUnit configuration. I have the same setup but I don't have to set 'copy local = true'

好菇凉咱不稀罕他 2024-07-18 13:52:24

问题是找不到应用程序本身,还是找不到应用程序的依赖项之一? 您可以尝试使用 FileMon 或 Fusion Log Viewer 来查看到底发生了什么故障。 问题可能不在于查找应用程序本身,而在于查找另一个依赖项。 确保所有依赖项已将 Copy Local 设置为 True。

您到底是如何在 VisualStudio 中启动 NUnit GUI 的? 如果您在项目属性下设置“启动外部程序”,它会为您提供指定工作目录的选项。 您可能需要将其更改为测试 dll 的构建位置。

Is the problem with it not finding the application itself, or one of the application's dependencies? You might try using either FileMon or the Fusion Log Viewer to see what exactly is failing. It's possible the issue isn't in finding the app itself, but in locating another dependency. Make sure any dependencies have Copy Local set to True.

How exactly are you starting the NUnit GUI within VisualStudio? If you are setting the "Start external program" under the project's properties, it gives you the option of specifying the working directory. You may need to change this to the build location of your test dll.

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