So I have a method i’m unit testing via MSTests to ensure it throws an exception properly on bad data coming in.
However that method has Debug.Assert so it’s caught while debugging if it happens during an actual test where I’m in debug mode trying to find a bug.
So the unit tests fail at being automatable when run in this way because the Assert statement comes up with Abort, Retry, Ignore. All 3 situations are important:
I’m debugging and looking for a problem, so I want the Debug.Assert to be available.
The code should have the proper guard clause so if it happens in production an exception is thrown.
My unit test should be completely automated and run without manual clicking.
What’s the work around?
So far when the unit tests are run locally Nicole’s solution didn’t do what I wanted, because I was intentionally passing in values that would trip the Asserts to make sure exceptions are thrown, interferes with running the unit tests automated locally. I suppose it would work if I was willing to accept the
[Conditional("DEBUG")]compilation attribute and run the unit tests in release mode. If I want that behavior I can provide a test level(or assembly level) wrapper with the[Conditional("DEBUG")]in the testing assembly, but this one I can utilize from a pre-compiled reusable class library.This gets pretty close to what I want, with the additional requirement of calling Trace.Listeners.Clear(); in my test suite.