27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

26 <strong>Why</strong> <strong>We</strong> <strong>Need</strong> <strong>Model</strong>-<strong>Based</strong> <strong>Testing</strong><br />

methods must be public void methods with no parameters, and must be labeled<br />

with the [Test] attribute. <strong>We</strong> have also labeled these with the optional [Category("Convert")]<br />

attribute so we can command the test runner to run just these<br />

tests.<br />

Each test method tests a run, a sample of program behavior consisting of a<br />

sequence of method calls and other statements. Convert32 tests a very brief run<br />

comprising a single method call. Its body is a single line:<br />

Assert.AreEqual(Temperature.ConvertToCelsius(32), 0);<br />

<strong>We</strong> happen to know that 32 ◦ F (the freezing point of water) corresponds to 0 ◦ C;<br />

this test method checks that the ConvertToCelsius method computes the conversion<br />

correctly for this case. The assertion here acts as the oracle, the authority that<br />

provides the correct result to determine whether the test passed or failed.<br />

A test method can make several method calls and check several assertions, so<br />

it can test a run that is longer than a single method call. Our ConvertMany method<br />

checks a few other well-known conversions. Its body is several lines:<br />

Assert.AreEqual(Temperature.ConvertToCelsius(32), 0);<br />

Assert.AreEqual(Temperature.ConvertToCelsius(98.6), 37);<br />

Assert.AreEqual(Temperature.ConvertToCelsius(212), 100);<br />

Assert.AreEqual(Temperature.ConvertToCelsius(212), 101); // Fail!<br />

If any assertion in a test method fails, the test runner considers that test a failure.<br />

Here we have deliberately put an incorrect assertion at the end of the test, in order to<br />

demonstrate how NUnit indicates test failures. This is the cause of the test failure we<br />

observed in the previous section. It also makes the point that a test failure does not<br />

necessarily indicate that the IUT is defective. It might be the test that is defective!<br />

The ConvertToCelsius method is an ideal subject for unit testing because it<br />

gets all the information it needs from its argument (the temperature in Fahrenheit)<br />

and provides all of the information it computes in its return value (the temperature<br />

in Celsius). It uses no stored information, and computes no new information that<br />

must be stored from one method call to the next. The same input always produces<br />

the same output, so each method call is independent of all the others; the history<br />

of previous calls doesn’t matter. Stored information is called state, so a method<br />

with these properties is said to be state-independent. It behaves like a mathematical<br />

function, so it is also said to be functional.<br />

State-independent methods are ideal subjects for unit testing. Their behavior<br />

does not depend on context, so the tests can be very short. Moreover, each test is<br />

conclusive in this sense: in any context, when invoked with the same arguments, the<br />

method will always behave exactly as it did in the test. The only problem in testing a<br />

state-independent method is choosing arguments that achieve good coverage of the<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!