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

📄 cppunit_cookbook中文版伟网动力.htm

📁 CppUnit Cookbook中文版 你希望知道你的代码是否正在工作。 你该怎么办? 有很多种方法。使用调试器直接调试或者在你的代码里乱丢一些流输出指令是两种简单的方法
💻 HTM
📖 第 1 页 / 共 2 页
字号:
              你会想运行它。CppUnit提供了定义这些suite并显示结果的工具。你可以通过在一个TestSuite中加入一个名为suite的静态的method使你的suite与TestRunner建立联系。</P>
              <P>例如,为了使TestRunner可以看到一个ComplexNumberTest 
              suite,在ComplexNumberTest中加入一下代码:<BR>public: <BR>static 
              CppUnit::Test *suite()<BR>{<BR>CppUnit::TestSuite *suiteOfTests = 
              new CppUnit::TestSuite( "ComplexNumberTest" 
              );<BR>suiteOfTests-&gt;addTest( new 
              CppUnit::TestCaller&lt;ComplexNumberTest&gt;( <BR>"testEquality", 
              <BR>&amp;ComplexNumberTest::testEquality ) 
              );<BR>suiteOfTests-&gt;addTest( new 
              CppUnit::TestCaller&lt;ComplexNumberTest&gt;(<BR>"testAddition",<BR>&amp;ComplexNumberTest::testAddition 
              ) );<BR>return suiteOfTests;<BR>}</P>
              <P>为了使用这个版本,在Main.cpp中包含以下头文件:<BR>#include 
              &lt;cppunit/ui/text/TestRunner.h&gt;<BR>#include 
              "ExampleTestCase.h"<BR>#include "ComplexNumberTest.h"</P>
              <P>然后在main()中加入addTest(CppUnit::Test *)的调用:<BR>int main( int argc, 
              char **argv)<BR>{<BR>CppUnit::TextUi::TestRunner 
              runner;<BR>runner.addTest( ExampleTestCase::suite() 
              );<BR>runner.addTest( ComplexNumberTest::suite() 
              );<BR>runner.run();<BR>return 0;<BR>}</P>
              <P>TestRunner会运行这些用例。如果所有的测试都顺利通过,你会得到一个反馈。如果任何一个测试没有通过,你会得到以下信息:<BR>*失败的测试用例的名字<BR>*包含这个测试源文件的名字<BR>*错误发生的行号<BR>*发现错误的CPPUNIT_ASSERT()调用中的所有文字。</P>
              <P>Helper Macros<BR>你可能已经注意到了,实现fixture中的static 
              suite()是一个要反复要做的,并且容易出错的任务。我们可以使用一组写 test fixture的宏来自动执行这些静态的suite 
              method.</P>
              <P>下面是使用这些宏重写了类ComplexNumberTest后的代码:<BR>#include 
              &lt;cppunit/extensions/HelperMacros.h&gt;</P>
              <P>class ComplexNumberTest : public CppUnit::TestFixture {</P>
              <P>首先我们声明这个suite,把这个类的名字传递给宏:<BR>CPPUNIT_TEST_SUITE( 
              ComplexNumberTest );</P>
              <P>这个使用静态的suite() 
              method建立的suite以类的名字来命名。然后,我们为每个测试用例声明:<BR>CPPUNIT_TEST( 
              testEquality );<BR>CPPUNIT_TEST( testAddition );</P>
              <P>最后,我们结束这个suite声明:<BR>CPPUNIT_TEST_SUITE_END();</P>
              <P>在这里下面这个method已经被实现了:<BR>static CppUnit::TestSuite *suite();</P>
              <P>剩下的fixture保持不动:<BR>private:<BR>Complex *m_10_1, *m_1_1, 
              *m_11_2;<BR>protected:<BR>void setUp()<BR>{<BR>m_10_1 = new 
              Complex( 10, 1 );<BR>m_1_1 = new Complex( 1, 1 );<BR>m_11_2 = new 
              Complex( 11, 2 ); <BR>}</P>
              <P>void tearDown() <BR>{<BR>delete m_10_1;<BR>delete 
              m_1_1;<BR>delete m_11_2;<BR>}</P>
              <P>void testEquality()<BR>{<BR>CPPUNIT_ASSERT( *m_10_1 == *m_10_1 
              );<BR>CPPUNIT_ASSERT( !(*m_10_1 == *m_11_2) );<BR>}</P>
              <P>void testAddition()<BR>{<BR>CPPUNIT_ASSERT( *m_10_1 + *m_1_1 == 
              *m_11_2 );<BR>}<BR>};</P>
              <P>加入这个suite的TestCaller的名字是这个fixture名字和method名字的组合。<BR>对目前这个用例来说,名字会是"ComplexNumberTest.testEquality" 
              和"ComplexNumberTest.testAddition". </P>
              <P>helper 
              macros帮你写些常用的断言。例如。检查当一个数被零除的时候ComplexNumber是否会抛出MathException异常:<BR>*把这个测试用例加入使用CPPUNIT_TEST_EXCEPTION的suite中,指定希望的异常的类型。<BR>*写这个测试用例的method</P>
              <P>CPPUNIT_TEST_SUITE( ComplexNumberTest );<BR>// 
              [...]<BR>CPPUNIT_TEST_EXCEPTION( testDivideByZeroThrows, 
              MathException );<BR>CPPUNIT_TEST_SUITE_END();</P>
              <P>// [...]</P>
              <P>void testDivideByZeroThrows()<BR>{<BR>// The following line 
              should throw a MathException.<BR>*m_10_1 / 
              ComplexNumber(0);<BR>}</P>
              <P>如果期望的异常没有被抛出,这个断言就会失败。</P>
              <P>TestFactoryRegistry</P>
              <P>TestFactoryRegistry是用来解决以下两个缺陷的:<BR>*忘了把你的fixture suite加入test 
              runner(因为它在另外一个文件中,很容易忘)<BR>*因为加入所有测试用例头文件造成的编译瓶颈。</P>
              <P>TestFactoryRegistry是在初始化的时候注册suite的地方。</P>
              <P>为了注册ComplexNumber suite,在.cpp中加入:<BR>#include 
              &lt;cppunit/extensions/HelperMacros.h&gt;</P>
              <P>CPPUNIT_TEST_SUITE_REGISTRATION( ComplexNumber );</P>
              <P>事实上,桌面下的动作是,一个静态的AutoRegisterSuite类型变量被声明。在构建的时候,它会注册一个TestSuiteFactory到TestFactoryRegistry。 
              TestSuiteFactory返回ComplexNumber::suite()返回的TestSuite。</P>
              <P>为了运行这些用例,使用文字的test runner,我们不必包含fixture了:<BR>#include 
              &lt;cppunit/extensions/TestFactoryRegistry.h&gt;<BR>#include 
              &lt;cppunit/ui/text/TestRunner.h&gt;</P>
              <P>int main( int argc, char 
              **argv)<BR>{<BR>CppUnit::TextUi::TestRunner runner;</P>
              <P>首先我们得到TestFactoryRegistry的实例:<BR>CppUnit::TestFactoryRegistry 
              &reg;istry = 
              CppUnit::TestFactoryRegistry::getRegistry();<BR>然后我们获得并添加一个由TestFactoryRegistry产生的新的TestSuite,它包含了使用CPPUNIT_TEST_SUITE_REGISTRATION()注册的所有的test 
              suite.<BR>runner.addTest( registry.makeTest() 
              );<BR>runner.run();<BR>return 0;<BR>}</P>
              <P>Post-build 
              check<BR>好了,现在我们已经可以使测试运行了,那么把它集成到编译过程中去怎么样?<BR>为了达到这个目的,应用程序必须返回一个非0值表明出现了错误。<BR>TestRunner::run()返回一个布尔值来表明run()是否成功。<BR>更新一下我们的main函数,我们得到:<BR>#include 
              &lt;cppunit/extensions/TestFactoryRegistry.h&gt;<BR>#include 
              &lt;cppunit/ui/text/TestRunner.h&gt;</P>
              <P>int main( int argc, char 
              **argv)<BR>{<BR>CppUnit::TextUi::TestRunner 
              runner;<BR>CppUnit::TestFactoryRegistry &reg;istry = 
              CppUnit::TestFactoryRegistry::getRegistry();<BR>runner.addTest( 
              registry.makeTest() );<BR>bool wasSucessful = runner.run( "", 
              false );<BR>return wasSucessful;<BR>}</P>
              <P>现在,你需要编译后运行你的应用程序。<BR>使用 Visual C++的话,可以在Project 
              Settings/Post-Build step中加入下面的命令。它被扩展到应用程序的执行路径。使用这个技术的时候看看project 
              examples/cppunittest/CppUnitTestMain.dsp 中是如何设置的。</P>
              <P>Original version by Michael Feathers. Doxygen conversion and 
              update by Baptiste Lepilleur. <BR></FONT><BR><BR>
              <P align=right>原作者:tonny<BR>来 源:转载<BR>共有682位读者阅读过此文 <BR><BR>【<A 
              href="http://www.weiw.com/article/sendmail.asp?id=458">告诉好友</A>】 
              </P></BLOCKQUOTE>
            <P>
            <LI>上篇文章:<A 
            href="http://www.weiw.com/article/list.asp?id=457">利用Ant和JUnit进行增量开发 
            使用单元测试来逐步改进代码</A> <BR>
            <LI>下篇文章:<A 
            href="http://www.weiw.com/article/list.asp?id=459">用Junit 
            Framework编写单元测试</A> </LI></TD></TR>
        <TR>
          <TD class=title3 width="50%">□- 本周热门文章 </TD>
          <TD class=title3 width="50%">□- 相关文章 </TD></TR>
        <TR class=tdbg>
          <TD vAlign=top width="50%">1.<A title=MSComm控件使用详解 
            href="http://www.weiw.com/article/list.asp?id=389" 
            target=_top>MSComm控件使用详解</A>[<FONT color=red>3942</FONT>]<BR>2.<A 
            title=VB串口编程的几个问题 href="http://www.weiw.com/article/list.asp?id=388" 
            target=_top>VB串口编程的几个问题</A>[<FONT color=red>3811</FONT>]<BR>3.<A 
            title=用VB进行串口实时数据采集 
            href="http://www.weiw.com/article/list.asp?id=383" 
            target=_top>用VB进行串口实时数据采集</A>[<FONT color=red>2582</FONT>]<BR>4.<A 
            title=用VB6实现仪器串口通信及检测功能 
            href="http://www.weiw.com/article/list.asp?id=384" 
            target=_top>用VB6实现仪器串口通信及检测功能</A>[<FONT 
            color=red>2135</FONT>]<BR>5.<A title="MSComm控件实现中文Win 9x下的通信" 
            href="http://www.weiw.com/article/list.asp?id=385" 
            target=_top>MSComm控件实现中文Win 9x...</A>[<FONT 
            color=red>1523</FONT>]<BR>6.<A title=用JAVASCRIPT打开EXCEL的模板文件(例) 
            href="http://www.weiw.com/article/list.asp?id=390" 
            target=_top>用JAVASCRIPT打开EXCEL...</A>[<FONT 
            color=red>990</FONT>]<BR>7.<A title="CppUnit Cookbook中文版" 
            href="http://www.weiw.com/article/list.asp?id=458" 
            target=_top>CppUnit Cookbook中文...</A>[<FONT 
          color=red>682</FONT>]<BR></TD>
          <TD vAlign=top width="50%"><A 
            href="http://www.weiw.com/article/list.asp?id=685">压力测试实例</A><BR><A 
            href="http://www.weiw.com/article/list.asp?id=543">对你的ASP程序作负载测试</A><BR><A 
            href="http://www.weiw.com/article/list.asp?id=459">用Junit 
            Framework编写单元测试</A><BR><A 
            href="http://www.weiw.com/article/list.asp?id=458">CppUnit 
            Cookbook中文版</A><BR><A 
            href="http://www.weiw.com/article/list.asp?id=457">利用Ant和JUnit进行增量开发 
            使用单元测试来逐步改进代码</A><BR><A 
            href="http://www.weiw.com/article/list.asp?id=456">NUnit 
            Cookbook(.net单元测试工具)</A><BR><A 
            href="http://www.weiw.com/article/list.asp?id=455">软件测试的组织与管理</A><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<DIV></DIV>
<TABLE width="95%" align=center>
  <TBODY>
  <TR>
    <TD align=middle>
      <HR align=center width="90%" color=#00a0f0 SIZE=1>

      <DIV align=center><A href="http://www.weiw.com/about/" 
      target=_blank>关于本站</A> -+- <A href="http://www.weiw.com/me/" 
      target=_blank>关于本人</A><A href="http://www.weiw.com/job/" 
      target=_blank></A> -+- <A href="mailto:webmaster@weiw.com">与我联系</A><A 
      href="http://www.weiw.com/job/" target=_blank></A> -+- <A 
      href="http://www.weiw.com/feedback/" target=_blank>留言板</A> -+- <A 
      href="http://www.weiw.com/index.asp" target=_blank>返回首页</A><BR><FONT 
      color=#006699>【</FONT>伟网动力<FONT color=#006699>】</FONT>建议使用<FONT 
      color=red>800*600</FONT>分辩率浏览 <BR>网管信箱:<A 
      href="mailto:webmaster@weiw.com"><IMG 
      src="CppUnit_Cookbook中文版伟网动力.files/email.gif" border=0></A> <IMG height=16 
      src="CppUnit_Cookbook中文版伟网动力.files/oicq.gif" width=16>OICQ:<A 
      href="http://search.tencent.com/cgi-bin/friend/user_show_info?ln=18740065" 
      target=_blank>18740065</A><!-- Begin Nedstat Basic code --> <!-- Title: &#20013;&#22269;&#32593;&#32476;&#26102;&#31354; --><!-- URL: http://stady.51.net -->
      <SCRIPT language=JavaScript src="CppUnit_Cookbook中文版伟网动力.files/basic.js">
</SCRIPT>

      <SCRIPT language=JavaScript>
<!--
  nedstatbasic("ABdakgoCAkfii8iX7wLZdC1LAZ3A", 0);
// -->
</SCRIPT>
      <NOSCRIPT><A 
      href="http://v1.nedstatbasic.net/stats?ABdakgoCAkfii8iX7wLZdC1LAZ3A" 
      target=_blank><IMG height=18 src="CppUnit_Cookbook中文版伟网动力.files/n.gif" 
      width=18 border=0 nosave></A> 
      </NOSCRIPT><!-- End Nedstat Basic code --></DIV></TD></TR></TBODY></TABLE></BODY></HTML>

⌨️ 快捷键说明

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