c# - 如何为调试和发布配置编写测试

标签 c# .net visual-studio unit-testing testing

我正在编写一个库,它的行为因调试或 Release模式而异。我想编写单元测试 TestDebugBehaviourTestReleaseBehaviour。是否可以将测试设置为在调试/ Release模式下运行?

最佳答案

我认为您应该能够使用预处理器指令来执行此操作。我在下面的示例中使用了 xunit;您可能需要使用不同的属性来装饰您的测试方法。

此测试应仅在 Debug模式下执行。

#if DEBUG

[Fact]
public void ThisIsATestThatWillOnlyRunInDebugMode()
{
    throw new Exception("I am running in debug mode.");
}

#endif

这个测试并不完全在 Release模式下运行,它只是在除 Debug模式之外的任何模式下运行,但这通常已经足够好了。

#if !DEBUG

[Fact]
public void ThisIsATestThatWillNotRunInDebugMode()
{
    throw new Exception("I am running in in something other than debug mode.");
}

#endif

关于c# - 如何为调试和发布配置编写测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596005/

相关文章:

c# - UTF-8 是否可以用于读/写亚洲语言?

c# - 单元测试 Microsoft Band

.net - 带分组的 WPF DataGrid 虚拟化

c++ - 链接 LAPACK、64 位、Visual Studio 2013

c# - 如何从停靠面板中删除用户控件

c# - 读取和写入文本文件时出现意外输出

.net - 如何在Form中添加小窗口

c# - 监视器.TryEnter()

visual-studio - 更改网站项目的名称(命名空间)

c++ - Visual Studio发行模式exe文件是否独立?