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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Overview 25<br />

assembly. Attributes can have parameters: the attribute [Category("Convert")] indicates<br />

that the following method belongs to the category named Convert; the test<br />

runner can be commanded to execute only the tests that belong to a particular<br />

category.<br />

Assertions check whether tests passed or failed. In NUnit, assertions are calls to<br />

methods in its Assert class, such as the calls to Assert.AreEqual that appear in our<br />

Test class. Each assertion checks a condition. If the condition is false, the assertion<br />

throws an exception that is caught by the test runner, which reports a test failure.<br />

To build the tests, compile your test class, referencing both the libraries you are<br />

testing (the IUT) and the NUnit library. This creates another library that contains<br />

your tests.<br />

csc /target:library /reference:Client.dll,Server.dll,Temperature.dll ˆ<br />

/reference:"%PROGRAMFILES%\NUnit-Net-2.0 2.2.8\bin\nunit.framework.dll" ˆ<br />

Tests.cs<br />

To execute the tests, run one of the NUnit test runner applications, identifying<br />

your test library on the command line or in the GUI. This command runs the command<br />

line test runner.<br />

nunit-console /include=Convert Tests.dll<br />

The test results appear on the console:<br />

Included categories: Convert<br />

..F<br />

Tests run: 2, Failures: 1, Not run: 0, Time: 0.501 seconds<br />

Failures:<br />

1) ClientServerImpl.Tests.ConvertMany :<br />

expected: <br />

but was: <br />

at ClientServerImpl.Tests.ConvertMany()<br />

This report says two tests were run and one failed, and provides some information<br />

about the failure. In the next section we will take a closer look at those tests.<br />

2.7 Some simple scenarios<br />

Our Tests class includes two test methods, Convert and ConvertMany, for testing<br />

the ConvertToCelsius method in our Temperature class (Figure 2.9). NUnit test<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!