我正在编写一个库,它的行为因调试或 Release模式而异。我想编写单元测试 TestDebugBehaviour
和 TestReleaseBehaviour
。是否可以将测试设置为在调试/ 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/