简体   繁体   English

在异常单元测试中进行Debug.Assert

[英]Debug.Assert in exception unit test

I have some inner logic of my library. 我的图书馆有一些内在的逻辑。 Before I used expressions like this to check input data of my inner logic: 在使用这样的表达式检查内部逻辑的输入数据之前:

if (value < 0)
    throw new ArgumentOutOfRangeException("value");

But my library is very performance-sensetive, so now I use expressions like this only for my API to check user input. 但是我的库对性能非常敏感,因此现在我仅将这样的表达式用于我的API来检查用户输入。 Inner input for my inner code I check with Debug.Assert to save several percent of performace in release. 我使用Debug.Assert检查内部代码的内部输入,以节省发行版中百分之几的性能。

But now, my unit tests of my inner code which check invalid input doesn't work. 但是现在,我对内部代码进行检查无效输入的单元测试不起作用。 With Debug.Assert there is not any exception more. 有了Debug.Assert,没有更多例外了。 So how can I check Debug.Assert in my unit tests? 那么如何在单元测试中检查Debug.Assert?

Well, it can't because you don't actually have any kind of checks, your unit tests are likely working against a "Release" build of the program, when in release mode all functions in the System.Diagnostics.Debug class become NO-OPs and don't do anything. 好吧,这不可能,因为您实际上没有进行任何检查,您的单元测试很可能针对程序的“发布”版本,而在发布模式下, System.Diagnostics.Debug类中的所有功能均变为否。 -OP,什么也不做。

Have you considered using CodeContracts to make your code provably only take valid inputs? 您是否考虑过使用CodeContracts来确保您的代码仅接受有效输入? It should work just like Debug.Assert so the code won't exist in the release version but you can prove none of your code passes in a invalid value. 它应该像Debug.Assert一样工作,因此该代码在发行版中将不存在,但是您可以证明没有任何代码传递无效值。

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

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