简体   繁体   English

如何编写调试和发布配置的测试

[英]How to write tests for debug and release configurations

I'm writing a library, and behaviour of it differs based on mode if it's debug or release. 我正在编写一个库,如果它是调试版或发布版,它的行为会因模式而异。 I want to write a unit-tests TestDebugBehaviour and TestReleaseBehaviour . 我想写一个单元测试TestDebugBehaviourTestReleaseBehaviour Is it possible to setup tests to run in debug/release mode? 是否可以设置测试以在调试/释放模式下运行?

I think you should be able to do this with preprocessor directives. 我认为您应该能够使用预处理程序指令执行此操作。 I am using xunit in my examples below; 我在下面的例子中使用xunit; you will probably have to decorate your test method with a different attribute. 您可能需要使用不同的属性来装饰您的测试方法。

This test should only execute in debug mode. 此测试应仅在调试模式下执行。

#if DEBUG

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

#endif

This test does not exactly run in release mode specifically, it just runs in any mode except debug mode but that is usually good enough. 此测试并不完全在发布模式下运行,它只是在除调试模式之外的任何模式下运行,但通常都足够好。

#if !DEBUG

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

#endif

No. That is not possible. 不,那是不可能的。 If you want to make such a comparison, just compile&run it in both release and compare the results. 如果你想进行这样的比较,只需在两个版本中编译并运行它并比较结果。 If a method behaves incorrectly in release mode only, use a logger to trace the behavior of that method and check out where things go wrong. 如果方法仅在发布模式下表现不正确,请使用记录器跟踪该方法的行为并检查出错的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM