?? cppunit_cookbook.html
字號:
<html><head><title>CppUnit - The Unit Testing Library</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body bgcolor="#ffffff"> <table width="100%"> <tr> <td width="40%" align="left" valign="center"> <a href="http://sourceforge.net/projects/cppunit"> CppUnit project page </a> </td> <td> <a href="FAQ">FAQ</a> </td> <td width="40%" align="right" valign="center"> <a href="http://cppunit.sourceforge.net">CppUnit home page</a> </td> </tr></table><hr><!-- Generated by Doxygen 1.3.7 --><div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a> | <a class="qindex" href="pages.html">Related Pages</a></div><h1><a class="anchor" name="cppunit_cookbook">CppUnit Cookbook</a></h1>Here is a short cookbook to help you get started.<h2><a class="anchor" name="simple_test_case">Simple Test Case</a></h2>You want to know whether your code is working.<p>How do you do it?<p>There are many ways. Stepping through a debugger or littering your code with stream output calls are two of the simpler ways, but they both have drawbacks. Stepping through your code is a good idea, but it is not automatic. You have to do it every time you make changes. Streaming out text is also fine, but it makes code ugly and it generates far more information than you need most of the time.<p>Tests in CppUnit can be run automatically. They are easy to set up and once you have written them, they are always there to help you keep confidence in the quality of your code.<p>To make a simple test, here is what you do:<p>Subclass the <a class="el" href="">TestCase </a> class. Override the method <a class="el" href="">runTest()</a>. When you want to check a value, call <a class="el" href="group___assertions.html#ga0">CPPUNIT_ASSERT(bool) </a> and pass in an expression that is true if the test succeeds.<p>For example, to test the equality comparison for a Complex number class, write:<p><pre><div class="fragment"><span class="keyword">class </span>ComplexNumberTest : <span class="keyword">public</span> CppUnit::<a class="code" href="class_test_case.html">TestCase</a> { <span class="keyword">public</span>: ComplexNumberTest( std::string name ) : CppUnit::<a class="code" href="class_test_case.html">TestCase</a>( name ) {} <span class="keywordtype">void</span> <a class="code" href="class_test_case.html#a5">runTest</a>() { <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( Complex (10, 1) == Complex (10, 1) ); <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( !(Complex (1, 1) == Complex (2, 2)) ); }};</div></pre><p>That was a very simple test. Ordinarily, you'll have many little test cases that you'll want to run on the same set of objects. To do this, use a fixture.<h2><a class="anchor" name="fixture">Fixture</a></h2>A fixture is a known set of objects that serves as a base for a set of test cases. Fixtures come in very handy when you are testing as you develop.<p>Let's try out this style of development and learn about fixtures along the away. Suppose that we are really developing a complex number class. Let's start by defining a empty class named Complex.<p><pre><div class="fragment"><span class="keyword">class </span>Complex {};</div></pre><p>Now create an instance of ComplexNumberTest above, compile the code and see what happens. The first thing we notice is a few compiler errors. The test uses <code>operator ==</code>, but it is not defined. Let's fix that.<p><pre><div class="fragment"><span class="keywordtype">bool</span> operator==( <span class="keyword">const</span> Complex &a, <span class="keyword">const</span> Complex &b) { <span class="keywordflow">return</span> <span class="keyword">true</span>; }</div></pre><p>Now compile the test, and run it. This time it compiles but the test fails. We need a bit more to get an <code>operator ==</code>working correctly, so we revisit the code.<p><pre><div class="fragment"><span class="keyword">class </span>Complex { <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator ==(<span class="keyword">const</span> Complex& a, <span class="keyword">const</span> Complex& b); <span class="keywordtype">double</span> real, imaginary;<span class="keyword">public</span>: Complex( <span class="keywordtype">double</span> r, <span class="keywordtype">double</span> i = 0 ) : real(r) , imaginary(i) { }};<span class="keywordtype">bool</span> operator ==( <span class="keyword">const</span> Complex &a, <span class="keyword">const</span> Complex &b ){ <span class="keywordflow">return</span> a.real == b.real && a.imaginary == b.imaginary; }</div></pre><p>If we compile now and run our test it will pass.<p>Now we are ready to add new operations and new tests. At this point a fixture would be handy. We would probably be better off when doing our tests if we decided to instantiate three or four complex numbers and reuse them across our tests.<p>Here is how we do it:<ul><li>Add member variables for each part of the <a class="el" href="">fixture </a></li><li>Override <a class="el" href="">setUp() </a> to initialize the variables</li><li>Override <a class="el" href="">tearDown() </a> to release any permanent resources you allocated in <a class="el" href="">setUp() </a></li></ul><p><pre><div class="fragment"><span class="keyword">class </span>ComplexNumberTest : <span class="keyword">public</span> CppUnit::<a class="code" href="class_test_fixture.html">TestFixture</a> {<span class="keyword">private</span>: Complex *m_10_1, *m_1_1, *m_11_2;<span class="keyword">public</span>: <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a1">setUp</a>() { m_10_1 = <span class="keyword">new</span> Complex( 10, 1 ); m_1_1 = <span class="keyword">new</span> Complex( 1, 1 ); m_11_2 = <span class="keyword">new</span> Complex( 11, 2 ); } <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a2">tearDown</a>() { <span class="keyword">delete</span> m_10_1; <span class="keyword">delete</span> m_1_1; <span class="keyword">delete</span> m_11_2; }};</div></pre><p>Once we have this fixture, we can add the complex addition test case any any others that we need over the course of our development.<h2><a class="anchor" name="test_case">Test Case</a></h2>How do you write and invoke individual tests using a fixture?<p>There are two steps to this process:<ul><li>Write the test case as a method in the fixture class</li><li>Create a <a class="el" href="class_test_caller.html">TestCaller</a> which runs that particular method</li></ul><p>Here is our test case class with a few extra case methods:<p><pre><div class="fragment"><span class="keyword">class </span>ComplexNumberTest : <span class="keyword">public</span> CppUnit::<a class="code" href="class_test_fixture.html">TestFixture</a> {<span class="keyword">private</span>: Complex *m_10_1, *m_1_1, *m_11_2;<span class="keyword">public</span>: <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a1">setUp</a>() { m_10_1 = <span class="keyword">new</span> Complex( 10, 1 ); m_1_1 = <span class="keyword">new</span> Complex( 1, 1 ); m_11_2 = <span class="keyword">new</span> Complex( 11, 2 ); } <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a2">tearDown</a>() { <span class="keyword">delete</span> m_10_1; <span class="keyword">delete</span> m_1_1; <span class="keyword">delete</span> m_11_2; } <span class="keywordtype">void</span> testEquality() { <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( *m_10_1 == *m_10_1 ); <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( !(*m_10_1 == *m_11_2) ); } <span class="keywordtype">void</span> testAddition() { <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( *m_10_1 + *m_1_1 == *m_11_2 ); }};</div></pre><p>One may create and run instances for each test case like this:<p><pre><div class="fragment">CppUnit::TestCaller<ComplexNumberTest> test( <span class="stringliteral">"testEquality"</span>, &ComplexNumberTest::testEquality );CppUnit::TestResult result;test.run( &result );</div></pre><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 <a class="el" href="group___executing_test.html">TestRunner </a> (see below) to display the results.<p>Once you have several tests, organize them into a suite.<h2><a class="anchor" name="suite">Suite</a></h2>How do you set up your tests so that you can run them all at once?<p>CppUnit provides a <a class="el" href="">TestSuite </a> class that runs any number of TestCases together.<p>We saw, above, how to run a single test case.<p>To create a suite of two or more tests, you do the following:<p><pre><div class="fragment">CppUnit::TestSuite suite;CppUnit::TestResult result;suite.addTest( <span class="keyword">new</span> CppUnit::TestCaller<ComplexNumberTest>( <span class="stringliteral">"testEquality"</span>, &ComplexNumberTest::testEquality ) );suite.addTest( <span class="keyword">new</span> CppUnit::TestCaller<ComplexNumberTest>( <span class="stringliteral">"testAddition"</span>, &ComplexNumberTest::testAddition ) );suite.run( &result );</div></pre><p><a class="el" href="">TestSuites </a> don't only have to contain callers for TestCases. They can contain any object that implements the <a class="el" href="">Test </a> interface. For example, you can create a <a class="el" href="">TestSuite </a> in your code and I can create one in mine, and we can run them together by creating a <a class="el" href="">TestSuite </a> that contains both:<p><pre><div class="fragment">CppUnit::TestSuite suite;CppUnit::TestResult result;suite.addTest( ComplexNumberTest::suite() );suite.addTest( SurrealNumberTest::suite() );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -