It is desired to have a possibility to expect Debug.Assert
() calling during test case execution. Now if I want to
test if Debug Assertion occured, I have to write my own
TraceListener which throw an exception in case of Fail()
calling, and substuite it in SetUp:
[TestFixture]
public class ReportTest {
[SetUp]
public void SetUp() {
m_defaultListener = Debug.Listeners[0];
Debug.Listeners[0] = new AssertSuppressor();
}
[TearDown]
public void TearDown() {
Debug.Listeners[0] = m_defaultListener;
}
// Some tests here with ExpectedException(typeof
(RealReportAssertionException))
private TraceListener m_defaultListener;
private class AssertSuppressor : TraceListener {
override public void Fail(string p_message, string
p_details) {
throw new RealReportAssertionException();
}
override public void Write(string p_message) {}
override public void WriteLine(string p_message) {}
}
private class RealReportAssertionException :
System.Exception {}
}
This implementation is quick and dirty - it doesn't take
into account that there can be several trace listeners,
and it doesn't depend on "DEBUG" - but it should not be
very difficult to implement.
Logged In: YES
user_id=586918
Is there a reason you don't just Add your special
tracelistener rather than replacing the one that's there?
Logged In: YES
user_id=824558
There are 2 ideas behind that piece of code:
1. If assertion is expected as a result of test case - I want
to make sure that it occurs
2. If assertion is NOT expected here - I want to make sure
that this test case will be marked as "failed" without any user
intervention (pressing "Ignore" button, etc.) Every night we
are building the whole product, and if during <nunit> task
there will be assertion, the whole nant process stops until
someone press "Ignore" button
So my idea was to replace default listener, which
shows "Abort/Retry/Ignore" dialog with my own which just
throws specific exception