简体   繁体   中英

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. Inner input for my inner code I check with Debug.Assert to save several percent of performace in release.

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. So how can I check Debug.Assert in my unit tests?

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.

Have you considered using CodeContracts to make your code provably only take valid inputs? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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