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.

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

using System;<br />

using NUnit.Framework;<br />

namespace ClientServerImpl<br />

{<br />

// Test fixture for remote instrument Server, Client, Temperature classes<br />

[TestFixture]<br />

public class Tests<br />

{<br />

#region [SetUp], [TearDown] methods (code not shown)<br />

// Check Fahrenheit to Celsius conversion, one data point<br />

[Test]<br />

[Category("Convert")]<br />

public void Convert32()<br />

{<br />

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

}<br />

// Check Fahrenheit to Celsius conversion, several data points<br />

[Test]<br />

[Category("Convert")]<br />

public void ConvertMany()<br />

{<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 />

}<br />

#region Other tests (code not shown)<br />

}<br />

}<br />

Figure 2.9. Test fixture with test methods.<br />

To write unit tests for NUnit, code a test fixture class that has test methods. Label<br />

them with the NUnit [TestFixture] and [Test] attributes, respectively. Each test<br />

method encodes a single test that can pass or fail; it calls a method (or methods) in<br />

the IUT (the library or libraries under test) and checks the results. Call an NUnit<br />

assertion, such as Assert.AreEqual, to check the results of each test. Our Tests<br />

class is an NUnit test fixture for testing our remote instrument libraries (Figure 2.9).<br />

Attributes supply programmer-defined information about a type or member that<br />

is stored in the assembly, where it can be retrieved by another program. The<br />

[TestFixture] and [Test] attributes defined in the NUnit library are used by the<br />

NUnit test runner applications to identify the test classes and test methods in an<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!