[Cppunit-devel] cookbook docs
Brought to you by:
blep
|
From: Steve M. R. <ste...@vi...> - 2001-05-14 23:22:13
|
Hi,
CppUnit looks like a useful framework for testing, so I thought
I'd see if I can use it for my projects. I installed the debian
package and started with the "cookbook" page.
When I started compiling the running example, I ran into several
difficulties. I tried to update the file to reflect the
current state of the library; patch is attached.
Summary of changes:
* note the namespace
* type Complex needs a proper constructor
* SetUp() and friends need to be public
* note that calling run() member of a TestCaller is not useful
(this baffled me for a while --- I did "assert(false)", and
couldn't figure out why I got no diagnostics!)
I ran out of steam when I got to the "TestRunner" section. None of
that worked for me, since CppUnit has no TestRunner! Now I see that
1.5.5 has it, but only in MSWindows. I suppose the thing to do is
replace or supplement this section with a description of using
TextTestResults?
Regards,
-Steve
--- old/cppunit-1.5.4.orig/doc/cookbook.html Thu Oct 5 14:37:28 2000
+++ cppunit-1.5.4/doc/cookbook.html Mon May 14 17:34:12 2001
@@ -8,7 +8,12 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
</P>
<H1>CppUnit Cookbook</H1>
- <P>Here is a short cookbook to help you get started. </P>
+ <P>
+ Here is a short cookbook to help you get started.
+ Note that all CppUnit types are defined in the CppUnit
+ namespace. For brevity, the prefix <b>CppUnit::</b>
+ is omitted in the following examples.
+ </P>
<H2>Simple Test Case</H2>
<P>
You want to know whether your code is working.
@@ -90,9 +95,7 @@
friend bool operator== (const Complex& a, const Complex& b);
double real, imaginary;
public:
- Complex () {
- real = imaginary = 0.0;
- }
+ Complex ( double r, double i = 0 ) : real(r),imaginary(i) {}
};
bool operator== (const Complex& a, const Complex& b)
@@ -153,7 +156,7 @@
<TT><PRE>	class ComplexNumberTest : public TestCase {
	private:
Complex 	*m_10_1, *m_1_1; *m_11_2;
-	protected:
+	public:
	void		setUp () {
			 m_10_1 = new Complex (10, 1);
			 m_1_1 = new Complex (1, 1);
@@ -170,21 +173,22 @@
			 assert (*m_10_1 + *m_1_1 == *m_11_2);
	}
	};</PRE>
-</TT><P>Create and run instances for each test case like this:</P>
-<TT><PRE>	test = new TestCaller<ComplexNumberTest>("testEquality", ComplexNumberTest::testEquality);
- test->run (); </PRE>
-</TT><P>The second argument to the test caller constructor is the address of a method on ComplexNumberTest. When the test caller is run, that specific method will be run.</P>
-<P>Once you have several tests, organize them into a suite.</P>
+</TT><P>One may create and run instances for each test case like this:</P>
+<TT><PRE>	TestCaller<ComplexNumberTest> test("testEquality", &ComplexNumberTest::testEquality);
+ test.run (); </PRE>
+</TT><P>The second argument to the test caller constructor is the address of a method on ComplexNumberTest. When the test caller is run, that specific method will be run. This is not a useful thing to do, however, as no diagnostics will be
+ displayed. One will normally use a TestRunner (see below) to display
+ the results.
+ </P>
+
+ <P>Once you have several tests, organize them into a suite.</P>
<P> </P>
<H2>Suite</H2>
<P>How do you set up your tests so that you can run them all at once?<BR>
<BR>
-CppUnit provides a TestSuite class that runs any number of TestCases together. For example, to run a single test case, you execute:</P>
-<TT><PRE>	TestResult result;
-	TestCaller<ComplexNumberTest> test ("testAddition", ComplexNumberTest::testAddition);
-	Test.run (&result);</PRE>
-</TT><P> </P>
-<P>To create a suite of two or more tests, you do the following:</P>
+CppUnit provides a TestSuite class that runs any number of TestCases together.
+ We saw, above, how to run a single test case.
+To create a suite of two or more tests, you do the following:</P>
<TT><PRE>	TestSuite suite;
	TestResult result;
	suite.addTest (new TestCaller<ComplexNumberTest>("testEquality", ComplexNumberTest::testEquality));
@@ -193,6 +197,7 @@
</PRE>
</TT><P>TestSuites don't only have to contain callers for TestCases. They can contain any object that implements the Test interface. For example, you can create a TestSuite in your code and I can create one in mine, and we can run them together by creating a TestSuite that contains both: </P>
<TT><PRE>	TestSuite suite;
+	TestResult result;
	suite.addTest (ComplexNumberTest.suite ());
	suite.addTest (SurrealNumberTest.suite ());
	suite.run (&result);</PRE>
--
by Rocket to the Moon,
by Airplane to the Rocket,
by Taxi to the Airport,
by Frontdoor to the Taxi,
by throwing back the blanket and laying down the legs ...
- They Might Be Giants
|