Unit testing 测试项目未找到测试项目dll

Unit testing 测试项目未找到测试项目dll,unit-testing,c#-4.0,Unit Testing,C# 4.0,我想看看我是否能实施这里列出的建议: 我将我的主要项目设置为命令行应用程序。 我的测试项目引用了copylocal=false的CmdlApp。 调试时,我有要输出到..\build\debug的主项目设置 我有要输出到..\build\tests的测试项目设置\ 我的测试项目中有一个app.config,具有以下设置: <configuration> <runtime> <assemblyBinding xmlns="urn:sch

我想看看我是否能实施这里列出的建议:

我将我的主要项目设置为命令行应用程序。 我的测试项目引用了copylocal=false的CmdlApp。 调试时,我有要输出到..\build\debug的主项目设置 我有要输出到..\build\tests的测试项目设置\ 我的测试项目中有一个app.config,具有以下设置:

<configuration>
    <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <probing privatePath="debug;debug\lib;tests;" />
                <dependentAssembly>
                        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
                </dependentAssembly>
                <dependentAssembly>
                    <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
                </dependentAssembly>
                <dependentAssembly>
                    <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
                </dependentAssembly>
                <dependentAssembly>
                    <assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-2.6.4.14350" newVersion="2.6.4.14350" />
                </dependentAssembly>
            </assemblyBinding>
    </runtime>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    </startup>
</configuration>
当我尝试运行单元测试时,我收到一个错误: System.IO.FileNotFoundException:无法加载文件或程序集“CmdlApp,版本=1.0.0.0,区域性=中性,PublicKeyToken=null”或其依赖项之一

我已检查并正在创建生成文件夹,调试文件夹包含CmdLnApp中的所有文件,生成文件夹还包含一个包含所有测试库的测试文件夹


有人能看到我遗漏了什么吗?

经验法则:所有代码都必须在外部DLL中。这使它更易于测试。exe中的任何内容都可以由用户、UI自动测试或使用命令行参数调用exe进行测试。它们都很笨重,而且容易失败。更正您的设置,您将不会遇到任何问题。

您的单元测试是否使用了CmdLnApp?它找不到主控制台的可执行文件,但是您没有提到您在输出文件夹中实际看到了它。CmdLnApp可执行文件确实显示在调试文件夹中,并且测试确实有在可执行文件中使用虚拟类的测试。我对assemblybinding中“Probling”指令的理解是,它应该指示CLR搜索中列出的目录找到依赖项的privatePath。由于debug文件夹是tests文件夹的同级,因此我认为应该找到CmdLnApp可执行文件。但是app.config属于CmdLnApp。如果由于任何原因无法执行CmdLnApp,那么就不会读取.config文件的内容,因此根本不会进行依赖关系探测。我明白了,在使用nunit进行测试时,有没有办法让类似依赖关系探测的事情发生?即使代码位于单独的dll中,除非我使用“copy local”(复制本地),否则测试运行程序如何知道探测它将在其中的子文件夹?这正是我试图避免的。然后,您需要指示测试运行程序使用您的.config文件,在该文件中提供探测路径。如果您使用NUnit,那么下面的链接将解释如何执行此操作。如果没有,那么你可以谷歌如果它是你的特定选择可能。因为测试运行实际上等同于引导AppDomain,只足以使用反射和发现测试属性。为了做到这一点,测试运行程序需要首先将程序集加载到域中。