不能在 Visual Studio 代码中使用 DEBUG 标志进行测试



我在Visual Studio Code中有一个单元测试设置,我需要读取测试数据文件,将其加载到HtmlDocument(HtmlAgilityPack)中并测试解析器类。在班上名列前茅,我有

public class MegaParserTests{
private HtmlDocument _hd;
private MegaParser _parserUT;
public ParserTests() {
    _parserUI = new MegaParser();
    _hd = new HtmlDocument()
#if DEBUG
    filePath = "data/theDoc.html";
#else
    filePath = @"../../../data/theDoc.html";
#endif
    var docStr = File.ReadAllText(filePath);
    _hd.LoadHtml(doc);
}
}

我使用了 #if DEBUG,因为文件路径似乎会根据我是从命令行进行"调试测试"还是"dotnet test"而变化。但是,当我进行dotnet测试时,遇到了顶部的"#if DEBUG"条件,并且我得到了错误的文件路径。为什么当我运行dotnet测试时运行时认为我处于调试模式。我该如何改变这一点?

实际上dotnet test命令默认使用Release构建配置...尝试使用-configuration标志强制发布构建配置:

dotnet test -c Release
-

c|--配置

要在其下构建的配置。默认值为"发布"。

最新更新