Negative Testing

Negative testing is testing in which test methods are designed to throw exceptions. Such methods are only considered successfully executed when the anticipated exception type is thrown.

Silk Performer offers an attribute that can be applied to test methods to indicate that a specific exception type is expected. If the specified exception is not thrown during execution, then the test method fails.

The Handle Exception dialog box opens whenever a method throws an exception. The dialog box enables you to specify what to do with the method:

Click Details to view detailed exception information. The Explorer Exception dialog box displays the thrown exception and all inner exceptions. The exception detail grid shows all properties of the exception object. Place your cursor over the value column to display a tool-tip that includes complete content. This is especially useful for the StackTrace property as it is a multi-line property.

Expected Exceptions

Each method call that is added to a test case has a property called Expected Exceptions. This property holds a list of exception types that are expected to be thrown during an animated run. If you selected Add to test case and expect this exception during test run on the Handle Exception dialog box, the thrown exception type will automatically be added to this list.

Click (...) in the Output Data Properties pane to display the Expected Exceptions dialog box. This dialog box enables you to add/remove expected exception types.

The Possible Exceptions list is populated with the system exceptions. You can enter an exception type that is not of the system types, though to proceed you must click OK on a confirmation dialog box informing you of potential impact.

The following code is generated for method calls when the exception SoapException is expected:

public void TestWebProxies_Service1__GetRandomNumber1() {
try {
Bdl.MeasureStart("Service1::GetRandomNumber");
              mWebProxy1.GetRandomNumber(5, 1);
              Bdl.MeasureStop("Service1::GetRandomNumber");
              Bdl.LogVerification(
               "Expected exception of the type(s): System.Web.Services.Protocols.SoapException "+
               "has not been thrown!", Bdl.Severity.SEVERITY_ERROR);
} catch (System.Web.Services.Protocols.SoapException expected_0) {
       		Bdl.MeasureStop("Service1::GetRandomNumber");
              Bdl.LogException(expected_0, Bdl.Severity.SEVERITY_SUCCESS);
} catch (SilkPerformer.StopException stopExcp) {
              Bdl.MeasureStop("Service1::GetRandomNumber");
              Bdl.LogException(stopExcp);
              throw stopExcp;
} catch (System.Exception e) {
              Bdl.MeasureStop("Service1::GetRandomNumber");
              Bdl.LogException(e);
}
}

If an exception is not thrown, a verification of severity Error will be logged. The log will contain the exception types that were expected but not thrown.