The below code works fine, the calc... generates an exception, comment it out or change calc... to not throw and exception and the test fails.
StartExpectingException(exception);
calcMembersPIPEndDate(EncodeDate(2005,01,01),true);
StopExpectingException('calcMembersPIPEndDate - 1st after aDay');
My problem is that any checks I put in this test method after this do not execute.
so
checkEquals(1,0);
StartExpectingException(exception);
calcMembersPIPEndDate(EncodeDate(2005,01,01),true);
StopExpectingException('calcMembersPIPEndDate - 1st after aDay');
fails on the 1st checkEquals
StartExpectingException(exception);
calcMembersPIPEndDate(EncodeDate(2005,01,01),true);
StopExpectingException('calcMembersPIPEndDate - 1st after aDay');
checkEquals(1,0);
passes - why?
I have tried to work out what version of Dunit I am using:
testframework.pas has the following - which didn't seem to
rcs_id: string = '#(@)$Id: TestFramework.pas,v 1.117 2006/07/19 02:45:55
rcs_version : string = '$Revision: 1.117 $';
versioninfo.inc
ReleaseNo : array[1..3] of Integer
= (9,2,1);
ReleaseStr = '9.2.1';
ReleaseWhen : array[1..6] of Integer
= (2005,09,25,17,30,00);
StopExpectingExceptioncannot work the way you expect. It's important to understand the flow of execution in an exception state to see why.Consider the following code:
When you call
OuterStepabove, line//2will raise an exception insideInnerStep. Now whenever an exception is raised:goto) to the first except or finally block found in the call-stack.Writeln('End');will not be called.//3will not be called.OuterStepis executed next.raise;is called, the exception is re-raised and the instruction pointer jumps to the next except or finally block.So when you write:
There are 2 possibilities:
DoSomethingraises an exception andStopExpectingExceptionis never called.DoSomethingdoesn't raise an exception and whenStopExpectingExceptionis called there is no exception.David has explained that the DUnit framework calls
StopExpectingExceptionfor you. But you may be wondering how to approach your test case checking multiple exception scenarios.Option 1
Write smaller tests.
You know that's what everyone says you're supposed to do in any case right? :)
E.g.
Option 2
As David has suggested, you can write your own exception handling inside your test. But you'll note that it can get a little messy, and you'll probably prefer option 1 in most cases. Especially when you have the added benefit that distinctly named tests make it easier to identify exactly what went wrong.
The subtle issues related exception testing make it important to: test first, ensure you have the correct failure, and only then implement the production code change to get a pass. The process will significantly reduce the chances of a dud test.