9.4 Test Assert Methods

     

9.4 Test Assert Methods

PyUnit provides 18 test assert methods. Many of them are aliases for the same methods. Most take an optional descriptive message argument, which is reported in case of test failure.

The test assert methods are defined within the class TestCase and are described in the following list. Their first argument, self , is a reference to the TestCase instance. When invoking these methods within a test method, this argument is implicit and does not need to be passed.


assert_(self, expr, msg=None)

failUnless(self, expr, msg=None)

Test passes if expr is true . This is the most generic assert method.


assertEqual(self, first, second, msg=None)

assertEquals(self, first, second, msg=None)

failUnlessEqual(self, first, second, msg=None)

Test passes if first and second are equal when compared using == .


assertNotEqual(self, first, second, msg=None)

assertNotEquals(self, first, second, msg=None)

failIfEqual(self, first, second, msg=None)

Test fails if first and second are equal when compared using == .


assertAlmostEqual(self, first, second, places=7, msg=None)

assertAlmostEquals(self, first, second, places=7, msg=None)

failUnlessAlmostEqual(self, first, second, places=7, msg=None)

Test passes if first and second are equal after being rounded to places decimal places.


assertNotAlmostEqual(self, first, second, places=7, msg=None)

assertNotAlmostEquals(self, first, second, places=7, msg=None)

failIfAlmostEqual(self, first, second, places=7, msg=None)

Test fails if first and second are equal after being rounded to places decimal places.


assertRaises(self, excClass, callableObj, *args, **kwargs)

failUnlessRaises(self, excClass, callableObj, *args, **kwargs)

Call callableObj with arguments args and keyword arguments kwargs . This assert passes if an exception of type excClass is thrown.


fail(self, msg=None)

Test assert that always fails.


failIf(self, expr, msg=None)

Test fails if expr is true .

     

Chapter 10. XMLUnit

Section 10.1.   Overview

Section 10.2.   Architecture

Section 10.3.   Usage

Section 10.4.   Test Assert Methods

     

10.1 Overview

XMLUnit provides useful support for unit testing of XML content. It is an extension to JUnit and NUnit, rather than a standalone framework.

Development and testing of software that generates XML content are common activities. Writing such tests is difficult with generic unit test frameworks. XML tests often compare expected XML content to actual XML content generated by the software that is being tested . Such tests can be written by doing a string comparison between the expected and actual XML content strings. However, what if the actual XML content contains a return character for readability, or puts XML attributes in opposite order than they appear in the expected XML string? Taken as XML documents, the two XML strings have the same contents, but an exact string comparison will fail.

To further illustrate the problem, the following pieces of XML represent an element named book containing the child elements title and author . Since the child elements are not in the same order, these XML elements have equivalent content, but different syntax:

 <book> <title>Dune</title> <author>Frank Herbert</author> </book>

<book> <author>Frank Herbert</author> <title>Dune</title> </book> 

The following representations of an empty XML element named library also have equal content but different syntax:

 <library> </library>

<library/> 

It is clearly useful to be able to test that two pieces of XML have the same content despite differences in format.

Other common test cases include verifying XML document validity and extracting the value of a particular node in a document, both of which involve writing a lot of code when building upon the generic xUnit test assert methods .

Thus, it is very useful to add knowledge of XML content, parsing, and validity to a unit test framework. XMLUnit addresses this need with a set of functional and powerful tests for XML content.

XMLUnit is open source software authored by Tim Bacon and Jeff Martin. It is released under a BSD license that freely allows redistribution and use of the source and binary code in both original and modified versions, as long as the original copyright notice is included. See the license file included with XMLUnit for full details.

For more information about XMLUnit, see http://xmlunit. sourceforge .net. The information in this chapter is based on XMLUnit for Java 1.0. The NUnit version, XMLUnit .Net 0.3, is very similar but is at an earlier stage of development.