Nunit: 控制台 NUnit 3 中测试的工作目录

创建于 2015-11-26  ·  21评论  ·  资料来源: nunit/nunit

在 NUnit 2.6.4 中,我的测试的工作目录被设置为测试 DLL 的对应bin/Debug目录。 这允许我通过相对路径在测试中加载外部文件。

似乎现在工作目录设置为控制台运行程序的工作目录。 这是故意的吗?

如何将测试结果(*.xml 文件)保留在控制台运行程序的工作目录中,同时将我的测试的工作目录设置为bin/Debug

notabug

最有用的评论

@imakowski您是否尝试过将当前工作目录设置为程序集设置中的已知目录? 未经测试的代码,但类似于;

``` C#
[设置夹具]
公共类 MySetUpClass
{
[一次性设置]
RunBeforeAnyTests()
{
var dir = Path.GetDirectoryName(typeof(MySetUpClass).Assembly.Location);
Environment.CurrentDirectory = dir;

    // or
    Directory.SetCurrentDirectory(dir);
}

}
``

所有21条评论

这是设计使然,如下所示: https :

在早期版本中,NUnit 更改了工作目录。 它不再这样做了。 您可以使用 TestContext.TestDirectory 获取包含测试程序集的目录。

新网址: https :

这打破了很多测试! 你为什么这样改? 一些测试无法修复,因为被测试应用程序的运行时代码依赖于工作目录是测试程序集的位置。

@imakowski您是否尝试过将当前工作目录设置为程序集设置中的已知目录? 未经测试的代码,但类似于;

``` C#
[设置夹具]
公共类 MySetUpClass
{
[一次性设置]
RunBeforeAnyTests()
{
var dir = Path.GetDirectoryName(typeof(MySetUpClass).Assembly.Location);
Environment.CurrentDirectory = dir;

    // or
    Directory.SetCurrentDirectory(dir);
}

}
``

@imakowski您可能无法控制它,但代码/应用程序不应依赖于当前设置的工作目录。 应用程序应该始终使用我上面介绍的代码来确定它们的 bin 目录。 Windows 中有几个 API 调用会在您不知道的情况下更改当前工作目录。 您的应用程序大部分时间都可以正常运行,但在用户访问应用程序中很少使用的部分后就会失败。

@rprouse此代码在卷影复制模式下不起作用
var dir = Path.GetDirectoryName(typeof(MySetUpClass).Assembly.Location);

在这种情况下,您可以使用它:
var dir = Path.GetDirectoryName(new Uri(typeof(MySetUpClass).Assembly.CodeBase).LocalPath);

或者,我们将其作为 TestContext.CurrentContext.TestDirectory 提供给您。 :-)

我们按照建议使用 Uri,并针对特殊情况进行了一些调整。

或者,我们将其作为 TestContext.CurrentContext.TestDirectory 提供给您。 :-)

CurrentContext 中不再有 TestDirectory 属性。 我使用 nunit 3.6.1。 在我的情况下,当我从 resharper 开始测试时,只有 WorkDirectory 指向错误的地方:(

有 TestContext.TestDirectory。 它是一个静态属性。 我相信 WorkDirectory 也是?

好吧,我无法使用 Visual Studio 浏览器或编译器或 Teleric 反编译器找到它。
但我在 nunit 3.5 中看到它。 不知道出了什么问题

我错了。

它是 TestContext.CurrentContext.TestDirectory。

但是,它不存在于 PORTABLE 版本中。

#if !PORTABLE
        /// <summary>
        /// Gets the directory containing the current test assembly.
        /// </summary>
        public string TestDirectory
        {
            get
            {
                Assembly assembly = _testExecutionContext?.CurrentTest?.TypeInfo?.Assembly;

                if (assembly != null)
                    return AssemblyHelper.GetDirectoryName(assembly);

#if NETSTANDARD1_6
                // Test is null, we may be loading tests rather than executing.
                // Assume that the NUnit framework is in the same directory as the tests
                return AssemblyHelper.GetDirectoryName(typeof(TestContext).GetTypeInfo().Assembly);
#else
                // Test is null, we may be loading tests rather than executing.
                // Assume that calling assembly is the test assembly.
                return AssemblyHelper.GetDirectoryName(Assembly.GetCallingAssembly());
#endif
            }
        }
#endif

它在 3.6.0 中有但不在 3.6.1 中

我正在查看当前的 github 存储库,它与我上面复制和粘贴的完全一样。

您确定您没有以某种方式使用 PORTABLE 版本吗?

是的,我正在使用它

嗯,这就是它不存在的原因。 据我所知,PORTABLE 版本不支持它,而且从来没有。 但是,我从未使用过 PORTABLE 构建,只是针对它运行 CI 测试。

谢谢你

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

对于那些关注实际代码的人来说,把@CharliePoole@rprouse所说的放在一起,就是

[SetUpFixture]
public class MySetUpClass
{
    [OneTimeSetUp]
    public void RunBeforeAnyTests()
    {
        Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
        // or identically under the hoods
        Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory);
    }
}

用于全局初始化

只是不要将上述逻辑包含在任何命名空间中。 IE

using NUnit.Framework;
using System;
using System.IO;

[SetUpFixture]
public class GlobalSetup
{
    [OneTimeSetUp]
    public void RunBeforeAnyTests()
    {
        Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
        // or identically under the hoods
        Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory);
    }
}

此外,似乎TestContext.CurrentContext.TestDirectory有更多的智能来解决极端情况,所以我会选择typeof(MySetUpClass).Assembly.Location

所以我会选择 typeof(MySetUpClass).Assembly.Location

如果程序集是阴影复制的,则Location返回阴影位置,通常您不希望那样。 改用CodeBase ,它会在任何卷影复制之前返回它最初运行的实际位置的Uri

显然最好使用TestDirectory ,但该属性似乎存在问题,请参阅#2872,导致无法捕获的异常。 我不知道如果您使用OneTimeSetup (我认为不会),它们是否也会出现,但在解决这个问题之前应该小心。

此页面是否有帮助?
0 / 5 - 0 等级