⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 money_example.html

📁 c++开发的一个不错的工具
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<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&nbsp;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&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div><h1><a class="anchor" name="money_example">Money, a step by step example</a></h1><h2><a class="anchor" name="Table">of contents</a></h2><ul><li><a class="el" href="money_example.html#sec_setting_vc">Setting up your project (VC++)</a></li><li><a class="el" href="money_example.html#sec_setting_unix">Setting up your project (Unix)</a></li><li><a class="el" href="money_example.html#sec_running_test">Running our tests</a></li><li><a class="el" href="money_example.html#sec_adding_testfixture">Adding the TestFixture</a></li><li><a class="el" href="money_example.html#sec_first_tests">Our first tests</a></li><li><a class="el" href="money_example.html#sec_more_tests">Adding more tests</a></li><li><a class="el" href="money_example.html#sec_credits">Credits</a></li></ul><p>The example explored in this article can be found in <code>examples/Money/</code>.<h2><a class="anchor" name="sec_setting_vc">Setting up your project (VC++)</a></h2><h3><a class="anchor" name="sec_install">Compiling and installing CppUnit libaries</a></h3>In the following document, $CPPUNIT is the directory where you unpacked CppUnit: $CPPUNIT/: include/ lib/ src/ cppunit/<p>First, you need to compile CppUnit libraries:<ul><li>Open the $CPPUNIT/src/CppUnitLibraries.dsw workspace in VC++.</li><li>In the 'Build' menu, select 'Batch Build...'</li><li>In the batch build dialog, select all projects and press the build button.</li><li>The resulting libraries can be found in the $CPPUNIT/lib/ directory.</li></ul><p>Once it is done, you need to tell VC++ where are the includes and librairies to use them in other projects. Open the 'Tools/Options...' dialog, and in the 'Directories' tab, select 'include files' in the combo. Add a new entry that points to $CPPUNIT/include/. Change to 'libraries files' in the combo and add a new entry for $CPPUNIT/lib/. Repeat the process with 'source files' and add $CPPUNIT/src/cppunit/.<h3><a class="anchor" name="sec_getting_started">Getting started</a></h3>Creates a new console application ('a simple application' template will do). Let's link CppUnit library to our project. In the project settings:<ul><li>In tab 'C++', combo 'Code generation', set the combo to 'Multithreaded DLL' for the release configuration, and 'Debug Multithreaded DLL' for the debug configure,</li><li>In tab 'C++', combo 'C++ langage', for All Configurations, check 'enable Run-Time Type Information (RTTI)',</li><li>In tab 'Link', in the 'Object/library modules' field, add cppunitd.lib for the debug configuration, and cppunit.lib for the release configuration.</li></ul><p>We're done !<h2><a class="anchor" name="sec_setting_unix">Setting up your project (Unix)</a></h2>We'll use <code>autoconf</code> and <code>automake</code> to make it simple to create our build environment. Create a directory somewhere to hold the code we're going to build. Create <code>configure.in</code> and <code>Makefile.am</code> in that directory to get started.<p><code>configure.in</code> <pre><div class="fragment">dnl Process this file with autoconf to produce a configure script.AC_INIT(Makefile.am)AM_INIT_AUTOMAKE(money,0.1)AM_PATH_CPPUNIT(1.9.6)AC_PROG_CXXAC_PROG_CCAC_PROG_INSTALLAC_OUTPUT(Makefile)</div></pre><p><code>Makefile.am</code> <pre><div class="fragment"># Rules for the test code (use `make check` to execute)TESTS = MoneyAppcheck_PROGRAMS = $(TESTS)MoneyApp_SOURCES = Money.h MoneyTest.h MoneyTest.cpp MoneyApp.cppMoneyApp_CXXFLAGS = $(CPPUNIT_CFLAGS)MoneyApp_LDFLAGS = $(CPPUNIT_LIBS)MoneyApp_LDFLAGS = -ldl</div></pre><h2><a class="anchor" name="sec_running_test">Running our tests</a></h2>We have a main that doesn't do anything. Let's start by adding the mecanics to run our tests (remember, test before you code ;-) ). For this example, we will use a <a class="el" href="class_text_test_runner.html">TextTestRunner</a> with the <a class="el" href="class_compiler_outputter.html">CompilerOutputter</a> for post-build testing:<p><code>MoneyApp.cpp</code> <pre><div class="fragment"><span class="preprocessor">#include "stdafx.h"</span><span class="preprocessor">#include &lt;<a class="code" href="_compiler_outputter_8h.html">cppunit/CompilerOutputter.h</a>&gt;</span><span class="preprocessor">#include &lt;<a class="code" href="_test_factory_registry_8h.html">cppunit/extensions/TestFactoryRegistry.h</a>&gt;</span><span class="preprocessor">#include &lt;<a class="code" href="cppunit_2ui_2text_2_test_runner_8h.html">cppunit/ui/text/TestRunner.h</a>&gt;</span><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[]){  <span class="comment">// Get the top level suite from the registry</span>  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();  <span class="comment">// Adds the test to the list of test to run</span>  CppUnit::TextUi::TestRunner runner;  runner.addTest( suite );  <span class="comment">// Change the default outputter to a compiler error format outputter</span>  runner.setOutputter( <span class="keyword">new</span> CppUnit::CompilerOutputter( &amp;runner.result(),                                                       std::cerr ) );  <span class="comment">// Run the tests.</span>  <span class="keywordtype">bool</span> wasSucessful = runner.run();  <span class="comment">// Return error code 1 if the one of test failed.</span>  <span class="keywordflow">return</span> wasSucessful ? 0 : 1;}</div></pre><p>VC++: Compile and run (Ctrl+F5).<p>Unix: First build. Since we don't have all the file yet, let's create them and build our application for the first time: <pre><div class="fragment">touch Money.h MoneyTest.h MoneyTest.cppaclocal -I /usr/local/share/aclocalautoconfautomake -atouch NEWS README AUTHORS ChangeLog # To make automake happy./configuremake check</div></pre><p>Our application will report that everything is fine and no test were run. So let's add some tests...<h3><a class="anchor" name="sec_post_build">Setting up automated post-build testing (VC++)</a></h3>What does post-build testing means? It means that each time you compile, the test are automatically run when the build finish. This is very useful, if you compile often you can know that you just 'broke' something, or that everything is still working fine.<p>Let's adds that to our project, In the project settings, in the 'post-build step' tab:<ul><li>Select 'All configurations' (upper left combo)</li><li>In the 'Post-build description', enter 'Unit testing...'</li><li>In 'post-build command(s)', add a new line: <code>$(TargetPath)$</code></li></ul><p><code>$(TargetPath)</code> expands into the name of your application: Debug.exe in debug configuration and Release.exe in release configuration.<p>What we are doing is say to VC++ to run our application for each build. Notices the last line of <code>main()</code>, it returns a different error code, depending on weither or not a test failed. If the code returned by an application is not 0 in post-build step, it tell VC++ that the build step failed.<p>Compile. Notices that the application's output is now in the build window. How convenient!<p>(Unix: tips to integrate make check into various IDE?)<h2><a class="anchor" name="sec_adding_testfixture">Adding the TestFixture</a></h2>For this example, we are going to write a simple money class. Money has an amount and a currency. Let's begin by creating a fixture where we can put our tests, and add single test to test Money constructor:<p><code>MoneyTest.h:</code> <pre><div class="fragment"><span class="preprocessor">#ifndef MONEYTEST_H</span><span class="preprocessor"></span><span class="preprocessor">#define MONEYTEST_H</span><span class="preprocessor"></span><span class="preprocessor">#include &lt;<a class="code" href="_helper_macros_8h.html">cppunit/extensions/HelperMacros.h</a>&gt;</span><span class="keyword">class </span>MoneyTest : <span class="keyword">public</span> CppUnit::<a class="code" href="class_test_fixture.html">TestFixture</a>{  <a class="code" href="group___writing_test_fixture.html#ga0">CPPUNIT_TEST_SUITE</a>( MoneyTest );  <a class="code" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>( testConstructor );  <a class="code" href="group___writing_test_fixture.html#ga2">CPPUNIT_TEST_SUITE_END</a>();<span class="keyword">public</span>:  <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a1">setUp</a>();  <span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a2">tearDown</a>();  <span class="keywordtype">void</span> testConstructor();};<span class="preprocessor">#endif  // MONEYTEST_H</span></div></pre><p><ul><li>CPPUNIT_TEST_SUITE declares that our Fixture's test suite.</li><li>CPPUNIT_TEST adds a test to our test suite. The test is implemented by a method named testConstructor().</li><li>setUp() and tearDown() are use to setUp/tearDown some fixtures. We are not using any for now.</li></ul><p><code>MoneyTest.cpp</code> <pre><div class="fragment"><span class="preprocessor">#include "stdafx.h"</span><span class="preprocessor">#include "MoneyTest.h"</span><span class="comment">// Registers the fixture into the 'registry'</span><a class="code" href="group___creating_test_suite.html#ga0">CPPUNIT_TEST_SUITE_REGISTRATION</a>( MoneyTest );<span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a1">MoneyTest::setUp</a>(){}<span class="keywordtype">void</span> <a class="code" href="class_test_fixture.html#a2">MoneyTest::tearDown</a>(){}<span class="keywordtype">void</span> MoneyTest::testConstructor(){  <a class="code" href="group___assertions.html#ga2">CPPUNIT_FAIL</a>( <span class="stringliteral">"not implemented"</span> );}</div></pre><p>Compile. As expected, it reports that a test failed. Press the <code>F4</code> key (Go to next Error). VC++ jump right to our failed assertion CPPUNIT_FAIL. We can not ask better in term of integration! <pre><div class="fragment">Compiling...MoneyTest.cppLinking...Unit testing....FG:\prg\vc\Lib\cppunit\examples\money\MoneyTest.cpp(26):AssertionTest name: MoneyTest.testConstructornot implementedFailures !!!Run: 1   Failure total: 1   Failures: 1   Errors: 0Error executing d:\winnt\system32\cmd.exe.moneyappd.exe - 1 error(s), 0 warning(s)</div></pre><p>Well, we have everything set up, let's start doing some real testing.<h2><a class="anchor" name="sec_first_tests">Our first tests</a></h2>Let's write our first real test. A test is usually decomposed in three parts:<ul><li>setting up datas used by the test</li><li>doing some processing based on those datas</li><li>checking the result of the processing</li></ul><p><pre><div class="fragment"><span class="keywordtype">void</span> MoneyTest::testConstructor(){  <span class="comment">// Set up</span>  <span class="keyword">const</span> std::string currencyFF( <span class="stringliteral">"FF"</span> );  <span class="keyword">const</span> <span class="keywordtype">double</span> longNumber = 12345678.90123;  <span class="comment">// Process</span>  Money money( longNumber, currencyFF );  <span class="comment">// Check</span>  <a class="code" href="group___assertions.html#ga3">CPPUNIT_ASSERT_EQUAL</a>( longNumber, money.getAmount() );  <a class="code" href="group___assertions.html#ga3">CPPUNIT_ASSERT_EQUAL</a>( currencyFF, money.getCurrency() );}</div></pre><p>Well, we finally have a good start of what our Money class will look likes. Let's start implementing...<p><code>Money.h</code> <pre><div class="fragment"><span class="preprocessor">#ifndef MONEY_H</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -