Label
them with the NUnit [TestFixture] and [Test] attributes, respectively. Each test
method encodes a single test that can pass or fail; it calls a method (or methods) in
the IUT (the library or libraries under test) and checks the results. Call an NUnit
assertion, such as Assert.AreEqual, to check the results of each test. Our Tests
class is an NUnit test fixture for testing our remote instrument libraries (Figure 2.9).
Attributes supply programmer-defined information about a type or member that
is stored in the assembly, where it can be retrieved by another program. The
[TestFixture] and [Test] attributes defined in the NUnit library are used by the
NUnit test runner applications to identify the test classes and test methods in an
Overview 25
assembly. Attributes can have parameters: the attribute [Category("Convert")] indicates
that the following method belongs to the category named Convert; the test
runner can be commanded to execute only the tests that belong to a particular
category.
Assertions check whether tests passed or failed. In NUnit, assertions are calls to
methods in its Assert class, such as the calls to Assert.AreEqual that appear in our
Test class. Each assertion checks a condition. If the condition is false, the assertion
throws an exception that is caught by the test runner, which reports a test failure.
To build the tests, compile your test class, referencing both the libraries you are
testing (the IUT) and the NUnit library.
Pages:
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57