gpt4 book ai didi

c# - nunit TestContext 抛出 NullReferenceException

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:49 38 4
gpt4 key购买 nike

我有以下代码:

[TestFixture]
public class LexicalTests
{
[Test]
public void LexicalTest1()
{
TestContext.CurrentContext.TestDirectory;
}
}

CurrentContext 在尝试获取 TestDirectoryWorkingDirectory 属性时抛出异常。

我该如何解决这个问题?

P.S.:在我的家用 PC 上测试运行完美(没有奇怪的异常)。

最佳答案

似乎某些提供运行 NUnit 单元测试功能的应用程序的 TestContext 类存在问题。

下面类中的测试应该通过:

using NUnit.Framework;

namespace UnitTests
{
[TestFixture]
public class UnitTests
{
[Test]
public void CurrentContextTest()
{
Assert.IsNotNull(TestContext.CurrentContext);
Assert.IsNotNull(TestContext.CurrentContext.TestDirectory);
Assert.IsNotNull(TestContext.CurrentContext.WorkDirectory);
}
}
}

如果测试没有通过,那么正如 Dmitry 在他上面的评论中所写的那样,在 ReSharper 菜单中更改 NUnit 版本。在 Visual Studio 中,转到 ReSharper -> 选项 -> 工具 -> NUnit。单击指定的 NUnit 安装单选按钮并确保指定了包含 nunit.core.dll、nunit.core.interfaces.dll 和 nunit.util.dll 的文件夹。如果找不到列出的文件,将显示错误。

enter image description here

更改 NUnit 版本后,重新运行测试,它应该会通过。

关于c# - nunit TestContext 抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16014936/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com