首页 > 解决方案 > nunit 版本 3 无法测试涉及动态生成程序集的代码

问题描述

场景:作为逻辑的一部分,系统生成如下动态程序集。

CompilerParameters cp = new CompilerParameters();

cp.GenerateExecutable = false;
cp.GenerateInMemory = true;

cp.TreatWarningsAsErrors = false;
cp.ReferencedAssemblies.Add("Employee.dll");
cp.ReferencedAssemblies.Add("Person.dll");

CompilerResults cr = provider.CompileAssemblyFromSource(cp, "someCode");
if (cr.Errors.Count > 0)
{..}

我在 NUnit-Gui-0.6.0 中打开单元测试程序集并测试一个方法,该方法最终将上述代码作为业务逻辑的一部分调用。

问题:上述错误计数大于 0 并具有以下消息:

error CS0006: Metadata file 'Person.dll' could not be found
error CS0006: Metadata file 'Employee.dll' could not be found

我已经验证这些程序集与单元测试 DLL 一起存在于 bin 目录中。此代码适用于常规应用程序工作流程,但不适用于 NUnit 3.*。

环境细节:VS 2017. Unit Test DLL and Application DLLs are full .NET Framework 4.7.1.

不确定这是 NUnit 3.* 中的错误还是应用程序问题。

问候,

伊夫蒂哈尔。

标签: c#nunit

解决方案


编译单元测试项目时使用哪种配置模式?当您选择“调试”配置模式时,dll 文件默认输出到 bin\Debug 文件夹中。由于您说您的程序集在 bin 文件夹中,请检查您是否没有将程序集放在 bin\Debug 文件夹中。


推荐阅读