简体   繁体   English

NUnit运行多个程序集时Assembly.GetExecutingAssembly的意外位置

[英]Unexpected Location of Assembly.GetExecutingAssembly When NUnit Runs Multiple Assemblies

I recently encountered an odd issue when performing unit tests. 我最近在执行单元测试时遇到了一个奇怪的问题。 My solution contains a helper class with a property for getting the directory of the executing assembly. 我的解决方案包含一个帮助程序类,该类具有用于获取执行程序集目录的属性。 It looks like this: 看起来像这样:

public static class DirectoryHelper
{
    public static string ExecutingAssemblyDirectory
    {
        get
        {
            var codeBase = Assembly.GetExecutingAssembly().CodeBase;
            var uri = new UriBuilder(codeBase);
            var path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path);
        }
    }
}

This method is called through various test classes to get relative file paths to dependent resources. 通过各种测试类调用此方法,以获取到相关资源的相对文件路径。

Take the following contrived projects as examples: 以以下人为设计的项目为例:

TestProject1.dll - TestFixture1.cs TestProject1.dll-TestFixture1.cs

[TestFixture]
public class TestFixture1
{
    [Test]
    public void VerifyExecutingAssemblyDirectory1()
    {
        StringAssert.Contains(@"\TestProject1\bin\Debug", 
        DirectoryHelper.ExecutingAssemblyDirectory);
    }
}

TestProject2.dll - TestFixture2.cs TestProject2.dll-TestFixture2.cs

[TestFixture]
public class TestFixture2
{
    [Test]
    public void VerifyExecutingAssemblyDirectory1()
    {
        StringAssert.Contains(@"TestProject2\bin\Debug", 
        DirectoryHelper.ExecutingAssemblyDirectory);
    }
}

When these tests are ran individually they pass and the location of the returned assembly is the debug folder of the test class. 分别运行这些测试时,它们会通过,返回的程序集的位置就是测试类的调试文件夹。

However, when ran together, TestFixture2.VerifyExecutingAssemblyDirectory2() is actually returning the path to the bin folder of TestProject1 , rather than TestProject2 . 但是,当一起运行时,TestFixture2.VerifyExecutingAssemblyDirectory2()实际上将路径返回到TestProject1的bin文件夹,而不是TestProject2

I'm trying to determine why this behavior is happening and understand a better way of going about this. 我正在尝试确定为什么会发生这种行为,并了解解决此问题的更好方法。 I've found that using .GetCallingAssembly will resolve this problem, but it doesn't seem like I should have to do this. 我发现使用.GetCallingAssembly可以解决此问题,但是看来我不必这样做。

I've created an example to reproduce this issue and posted to GitHub. 我创建了一个示例来重现此问题并将其发布到GitHub。 TylerNielsen/NUnitExecutingAssemblyExample TylerNielsen / NUnitExecutingAssemblyExample

Note: I'm aware of the TestContext.TestDirectory in NUnit, however this library is currently not dependent on NUnit and I'd prefer to keep it that way. 注意:我知道NUnit中的TestContext.TestDirectory ,但是该库当前不依赖于NUnit,因此我希望保持这种方式。

UPDATE I'm running the NUnit tests through both Resharper in Visual Studio and via NUnit3-Console. 更新我正在通过Visual Studio中的Resharper和通过NUnit3-Console运行NUnit测试。 When I run using NUnit3-Console, I'm only specifying the two individual .dlls and not providing any other arguments. 当我使用NUnit3-Console运行时,我仅指定两个单独的.dll,而未提供任何其他参数。

Both TestProject1 and TestProject2 reference the assembly containing DirectoryHelper. TestProject1和TestProject2都引用包含DirectoryHelper的程序集。 I'm assuming that your references cause the assembly to be copied to the individual (separate) output directories. 我假设您的引用导致程序集被复制到单独的(单独的)输出目录。

When you run both test assemblies together, one of them causes it's "personal" copy of that assembly to be loaded. 当您同时运行两个测试程序集时,其中一个会导致加载该程序集的“个人”副本。 The second one finds that the assembly is already in memory. 第二个发现程序集已经在内存中。

Of course, this behavior will depend on how you run the assemblies, which you haven't said. 当然,这种行为将取决于您如何运行程序集,而您没有说过。 In the case where you use nunit3-console, it will also depend on your command-line arguments, especially whether you use a separate process for each assembly. 在使用nunit3-console的情况下,它也取决于命令行参数,尤其是对于每个程序集是否使用单独的进程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Fody 和 Assembly.GetExecutingAssembly().Location 返回空字符串 - Fody and Assembly.GetExecutingAssembly().Location returns empty string Windows phone 8.1 Assembly.GetExecutingAssembly不可用 - Windows phone 8.1 Assembly.GetExecutingAssembly not available 为什么 Assembly.GetExecutingAssembly() 会返回 null? - Why would Assembly.GetExecutingAssembly() return null? 使用Assembly.GetExecutingAssembly()从其他类库获取程序集 - Use Assembly.GetExecutingAssembly() to get assembly from other class library Assembly.GetExecutingAssembly()和typeof(程序)之间的区别.Assembly - Difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly Assembly.GetExecutingAssembly()。CreateInstance应该在这里抛出异常吗? - should Assembly.GetExecutingAssembly().CreateInstance throw an exception here? VS 2010构建输出路径是否覆盖Assembly.GetExecutingAssembly案例? - VS 2010 Build Output path overrides Assembly.GetExecutingAssembly case? 与 C# 中的 Assembly.GetExecutingAssembly().GetTypes() 相关的问题 - Issue Related to Assembly.GetExecutingAssembly().GetTypes() in C# 这个.GetType()。Assembly.GetName()。版本和Assembly.GetExecutingAssembly()。GetName()。版本有什么区别? - What's the difference between this.GetType().Assembly.GetName().Version and Assembly.GetExecutingAssembly().GetName().Version? Assembly.GetExecutingAssembly()。GetManifestResourceNames()是否从App_LocalResources返回资源? - Does Assembly.GetExecutingAssembly().GetManifestResourceNames() return resources from App_LocalResources?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM