📄 class_test_fixture.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>TestFixture Class Reference<br><small>[<a class="el" href="group___writing_test_fixture.html">Writing test fixture</a>]</small></h1>Wraps a test case with setUp and tearDown methods. <a href="#_details">More...</a><p><code>#include <<a class="el" href="_test_fixture_8h-source.html">TestFixture.h</a>></code><p><p>Inheritance diagram for TestFixture:<p><center><img src="class_test_fixture.png" usemap="#TestFixture_map" border="0" alt=""></center><map name="TestFixture_map"><area href="class_test_case.html" alt="TestCase" shape="rect" coords="317,56,624,80"><area href="class_orthodox.html" alt="Orthodox< ClassUnderTest >" shape="rect" coords="0,112,307,136"><area href="class_test_caller.html" alt="TestCaller< Fixture >" shape="rect" coords="317,112,624,136"><area href="class_test_case_decorator.html" alt="TestCaseDecorator" shape="rect" coords="634,112,941,136"><area href="class_exception_test_case_decorator.html" alt="ExceptionTestCaseDecorator< ExpectedException >" shape="rect" coords="634,168,941,192"></map><a href="class_test_fixture-members.html">List of all members.</a><table border=0 cellpadding=0 cellspacing=0><tr><td></td></tr><tr><td colspan=2><br><h2>Public Member Functions</h2></td></tr><tr><td class="memItemLeft" nowrap align=right valign=top>virtual </td><td class="memItemRight" valign=bottom><a class="el" href="class_test_fixture.html#a0">~TestFixture</a> ()</td></tr><tr><td class="memItemLeft" nowrap align=right valign=top>virtual void </td><td class="memItemRight" valign=bottom><a class="el" href="class_test_fixture.html#a1">setUp</a> ()</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Set up context before running a test. <a href="#a1"></a><br><br></td></tr><tr><td class="memItemLeft" nowrap align=right valign=top>virtual void </td><td class="memItemRight" valign=bottom><a class="el" href="class_test_fixture.html#a2">tearDown</a> ()</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Clean up after the test run. <a href="#a2"></a><br><br></td></tr></table><hr><a name="_details"></a><h2>Detailed Description</h2>Wraps a test case with setUp and tearDown methods. <p>A TestFixture is used to provide a common environment for a set of test cases.<p>To define a test fixture, do the following:<ul><li>implement a subclass of <a class="el" href="class_test_case.html">TestCase</a></li><li>the fixture is defined by instance variables</li><li>initialize the fixture state by overriding setUp (i.e. construct the instance variables of the fixture)</li><li>clean-up after a test by overriding tearDown.</li></ul><p>Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:<p><pre><div class="fragment"> <span class="keyword">class </span>MathTest : <span class="keyword">public</span> CppUnit::<a class="code" href="class_test_fixture.html">TestFixture</a> { <span class="keyword">protected</span>: <span class="keywordtype">int</span> m_value1, m_value2; <span class="keyword">public</span>: MathTest() {} <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a1">setUp</a> () { m_value1 = 2; m_value2 = 3; } }</div></pre><p>For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling CPPUNIT_ASSERT on the expression you want to test:<p><pre><div class="fragment"> <span class="keyword">public</span>: <span class="keywordtype">void</span> testAdd () { <span class="keywordtype">int</span> result = m_value1 + m_value2; <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( result == 5 ); }</div></pre><p>Once the methods are defined you can run them. To do this, use a <a class="el" href="class_test_caller.html">TestCaller</a>.<p><pre><div class="fragment"> CppUnit::Test *test = <span class="keyword">new</span> CppUnit::TestCaller<MathTest>( <span class="stringliteral">"testAdd"</span>, &MathTest::testAdd ); test->run();</div></pre><p>The tests to be run can be collected into a <a class="el" href="class_test_suite.html">TestSuite</a>.<p><pre><div class="fragment"> <span class="keyword">public</span>: <span class="keyword">static</span> CppUnit::TestSuite *MathTest::suite () { CppUnit::TestSuite *suiteOfTests = <span class="keyword">new</span> CppUnit::TestSuite; suiteOfTests->addTest(<span class="keyword">new</span> CppUnit::TestCaller<MathTest>( <span class="stringliteral">"testAdd"</span>, &MathTest::testAdd)); suiteOfTests->addTest(<span class="keyword">new</span> CppUnit::TestCaller<MathTest>( <span class="stringliteral">"testDivideByZero"</span>, &MathTest::testDivideByZero)); <span class="keywordflow">return</span> suiteOfTests; }</div></pre><p>A set of macros have been created for convenience. They are located in <a class="el" href="_helper_macros_8h.html">HelperMacros.h</a>.<p><dl compact><dt><b>See also:</b></dt><dd><a class="el" href="class_test_result.html">TestResult</a>, <a class="el" href="class_test_suite.html">TestSuite</a>, <a class="el" href="class_test_caller.html">TestCaller</a>, <p><a class="el" href="group___writing_test_fixture.html#ga1">CPPUNIT_TEST_SUB_SUITE</a>, <a class="el" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>, <a class="el" href="group___writing_test_fixture.html#ga2">CPPUNIT_TEST_SUITE_END</a>, <p><a class="el" href="group___creating_test_suite.html#ga0">CPPUNIT_TEST_SUITE_REGISTRATION</a>, <a class="el" href="group___writing_test_fixture.html#ga6">CPPUNIT_TEST_EXCEPTION</a>, <a class="el" href="group___writing_test_fixture.html#ga7">CPPUNIT_TEST_FAIL</a>. </dd></dl><p><hr><h2>Constructor & Destructor Documentation</h2><a class="anchor" name="a0" doxytag="TestFixture::~TestFixture" ></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> virtual TestFixture::~<a class="el" href="class_test_fixture.html">TestFixture</a> </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top"> ) </td> <td class="md" nowrap><code> [inline, virtual]</code></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p> </td> </tr></table><hr><h2>Member Function Documentation</h2><a class="anchor" name="a1" doxytag="TestFixture::setUp" ></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> virtual void TestFixture::setUp </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top"> ) </td> <td class="md" nowrap><code> [inline, virtual]</code></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Set up context before running a test. <p><p>Reimplemented in <a class="el" href="class_test_case_decorator.html#a3">TestCaseDecorator</a>, and <a class="el" href="class_test_caller.html#a5">TestCaller< Fixture ></a>. </td> </tr></table><a class="anchor" name="a2" doxytag="TestFixture::tearDown" ></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> virtual void TestFixture::tearDown </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top"> ) </td> <td class="md" nowrap><code> [inline, virtual]</code></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Clean up after the test run. <p><p>Reimplemented in <a class="el" href="class_test_case_decorator.html#a4">TestCaseDecorator</a>, and <a class="el" href="class_test_caller.html#a6">TestCaller< Fixture ></a>. </td> </tr></table><hr>The documentation for this class was generated from the following file:<ul><li><a class="el" href="_test_fixture_8h-source.html">TestFixture.h</a></ul><hr><table width="100%"> <tr> <td width="10%" align="left" valign="center"> <a href="http://sourceforge.net"> <img src="http://sourceforge.net/sflogo.php?group_id=11795" width="88" height="31" border="0" alt="SourceForge Logo"></a> </td> <td width="20%" align="left" valign="center"> hosts this site. </td> <td> </td> <td align="right" valign="center"> Send comments to:<br> <a href="mailto:cppunit-devel@lists.sourceforge.net">CppUnit Developers</a> </td> </tr></table></body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -