You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core下NUnit Console无法正确使用指定配置文件

.NET6下NUnit测试项目无法加载指定配置文件问题

我有一个基于.NET6编译的C#测试项目,因遗留问题使用System.Configuration.ConfigurationManager 7.0.0读取配置文件而非JSON文件。执行命令nunit\console\nunit3-console.exe mytest.nunit --configfile=mytest.config后,发现实际加载的配置文件为D:\src2\csharp\coretest\nunit\console\agents\net6.0\nunit-agent.dll.config,并非指定的mytest.config。该问题疑似关联旧Issue,但当前使用最新版本NUnit Console 3.16.1。开启--trace=Verbose也未找到配置文件获取逻辑的日志。相同配置在.NET Framework 4.8下可正常运行,求解决方法。


测试代码

[Test]
public void TestConfiguration()
{
   var filePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
   Console.Error.WriteLine($"Configuration filepath is {filePath}");

   var testValue = ConfigurationManager.AppSettings["TestValue"];
   Assert.IsTrue(testValue == "testing");
}

mytest.nunit项目文件

<NUnitProject>
  <Config name="Debug" binpathtype="Auto" runtimeFramework="6.0" configfile="./mytest.config">
    <assembly path="PrimeService.Tests\bin\Debug\net6.0\PrimeService.Tests.dll" />
  </Config>
</NUnitProject>

mytest.config配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="testValue" value="testing"/>
    </appSettings>
</configuration>

目录结构

D:\src2\csharp\coretest\
  - mytest.nunit
  - mytest.config
D:\src2\csharp\coretest\nUnit
D:\src2\csharp\coretest\PrimeService.Tests

命令行执行结果片段

D:\src2\csharp\coretest>nunit\console\nunit3-console.exe mytest.nunit --configfile=mytest.config
NUnit Console 3.16.1 (Release)
Copyright (c) 2022 Charlie Poole, Rob Prouse
Wednesday, January 18, 2023 5:04:08 PM

Runtime Environment
   OS Version: Microsoft Windows NT 6.2.9200.0
   Runtime: .NET Framework CLR v4.0.30319.42000

Test Files
    mytest.nunit

Configuration filepath is D:\src2\csharp\coretest\nunit\console\agents\net6.0\nunit-agent.dll.config

内容的提问来源于stack exchange,提问作者Ketchup201

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:55:15