8.4 Test Assert Methods

     

8.4 Test Assert Methods

NUnit provides a variety of test assert methods. Each one has a variant taking a message parameter, which appears as a descriptive message when the test fails.

The test assert methods referenced here are static methods of the class Assert . Additional test assert methods exist in the class Assertion , but they are obsolete and not recommended for use.


IsTrue(bool condition)

IsTrue(bool condition, string message)

Test passes if condition is true . This is the most generic type of assert.


IsFalse(bool condition)

IsFalse(bool condition, string message)

Test passes if condition is false .


AreEqual(decimal expected, decimal actual)

AreEqual(decimal expected, decimal actual, string message)

AreEqual(int expected, int actual)

AreEqual(int expected, int actual, string message)

Test passes if expected and actual are numerically equal.


AreEqual(double expected, double actual, double delta)

AreEqual(double expected, double actual, double delta, string message)

AreEqual(float expected, float actual, float delta)

AreEqual(float expected, float actual, float delta, string message)

Test passes if expected and actual are numerically equal within a tolerance of delta . If delta is 0, exact equality is necessary for test to pass.


AreEqual(Object expected, Object actual)

AreEqual(Object expected, Object actual, string message)

Test passes if expected and actual are equal. If both are numeric types, they are tested for numerical equality. Otherwise, the method Object.equals( ) is used to test equality.


AreSame(Object expected, Object actual)

AreSame(Object expected, Object actual, string message)

Test passes if expected and actual refer to the same Object .


IsNotNull(Object anObject)

IsNotNull(Object anObject, string message)

Test passes if anObject is not null .


IsNull(Object anObject)

IsNull(Object anObject, string message)

Test passes if anObject is null .


Fail( )

Fail(string message)

Test assert that always fails.

     

Chapter 9. PyUnit

Section 9.1.   Overview

Section 9.2.   Architecture

Section 9.3.   Usage

Section 9.4.   Test Assert Methods

     

9.1 Overview

PyUnit brings xUnit to Python. Python is an interpreted, interactive, object-oriented programming language, widely used for many different kinds of software development. Steve Purcell ported JUnit to Python to create PyUnit. It follows the generic xUnit model closely. The standard Python libraries have included PyUnit since Python 2.1.

Python is open source software that is copyrighted but freely usable and distributable. PyUnit has the same terms as Python itself, with a stipulation that Steve Purcell be credited as the author in the source code and any accompanying documentation.

For more information about Python, see http://www.python.org. PyUnit's home is http://pyunit. sourceforge .net. The information in this chapter is based on Python 2.3.3, which includes Version 1.4.6 of PyUnit.

     

9.2 Architecture

PyUnit's architecture is simple and effective. Tests are implemented by inheritance from a base class, TestCase , which supports test fixture behavior. Tests may be aggregated using a TestSuite . A number of test assert methods are provided. The PyUnit module, unittest.py , not only contains the foundation code for building unit tests, but also acts as a test runner to execute tests from the command line. A GUI test runner is also provided.