在極限編程中,測試程序本應該在編寫主程序之前就要寫好,然后將寫好的類程序放在測試程序中進行測試,但考慮到項目中需求文檔等並未將接口定義好,我無從開始,而且,自己對單元測試也是剛剛熟悉,需要一邊寫測試程序一邊解決遇到的問題,時間周期較長。在本次編程中,我是直接把github上小組的程序下載下來,看有哪些接口,再來編寫的測試程序。
測試對象:robot類中的solver類(https://github.com/TeamWork-Robot/Team1/tree/master/Robot);選擇solver類的理由主要原因:1.robot類中未找到易於測試的函數,返回值一般為void;2.自以為point類,frame類比較簡單,沒什么好測試的,而solver類本身具有一定復雜度,而且函數返回類型一般為point;
編寫好solver類的測試程序后,發現錯誤太多,花了好長時間也沒有解決(主要是發現solver類本身還有不少需要改進的地方);於是退而求其次,先寫一個能測試point類的程序,解決遇到的問題。
 
          
         1 #include <cppunit/extensions/HelperMacros.h> 2 #include <Point.h> 3 4 class testpoint :public CppUnit::TestFixture 5 { 6 CPPUNIT_TEST_SUITE(testpoint); 7 CPPUNIT_TEST(testget); 8 //CPPUNIT_TEST(testrotate); 9 CPPUNIT_TEST_SUITE_END(); 10 public: 11 void setUp(); 12 void tearDown(); 13 testpoint(); 14 //~testsolver(); 15 void testget(); 16 //void testrotate(); 17 };
 
          
         1 #include "test_point.h" 2 #include "Point.h" 3 #include <string> 4 #include<iostream> 5 #include <cppunit/TestCase.h> 6 #include "cppunit/TestAssert.h" 7 8 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(testpoint, "alltest"); 9 10 testpoint::testpoint() 11 { 12 }; 13 14 void testpoint::setUp() 15 { 16 std::cout << "test begin" << std::endl; 17 }; 18 19 void testpoint::tearDown() 20 { 21 std::cout << "test end" << std::endl; 22 }; 23 24 void testpoint::testget() 25 { 26 Point point1(3, 4); 27 int resultx = point1.getX(); 28 int resulty = point1.getY(); 29 CPPUNIT_ASSERT(resultx == 3); 30 CPPUNIT_ASSERT(resulty == 4); 31 }
 
          
         1 #include <cppunit/TestResultCollector.h> 2 #include <cppunit/BriefTestProgressListener.h> 3 #include <cppunit/TextOutputter.h> 4 5 int main() 6 { 7 CppUnit::TestResult r; 8 CppUnit::TestResultCollector rc; 9 r.addListener(&rc); // 准備好結果收集器 10 11 CppUnit::TestRunner runner; // 定義執行實體 12 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("alltest").makeTest()); 13 runner.run(r); // 運行測試 14 15 CppUnit::TextOutputter o(&rc, std::cout); 16 o.write(); // 將結果輸出 17 18 system("pause"); 19 20 return rc.wasSuccessful() ? 0 : -1; 21 }
把point類的頭文件和源程序加進去后,結果如下:(加了system("pause");)

將Point.cpp中的get函數的返回值人為的加一之后,結果如下:

下面介紹一下這其中遇到的問題及解決辦法,為需要解決類似問題的同學提供參考:
1.fatal error C1083: 無法打開包括文件:“Point.h”: No such file or directory:這個問題的解決辦法如下,首先找到出錯文件的位置,右鍵項目屬性 ->C/C++ ->常規->附加包含目錄,將出錯文件位置放進去。

2. error C2011: “Point”:“class”類型重定義;由於Point.h文件在一開始沒有使用宏定義,導致運行測試類時顯示Point.h重編譯;解決辦法將Point.h文件中加上宏定義#ifndef POINT_H #define POINT_H #endif即可。
程序相關語句解釋:
1.class testpoint :public CppUnit::TestFixture 繼承自TestFixture的類testpoint;
2.CPPUNIT_TEST_SUITE(testpoint);將testpoint添加到測試包
CPPUNIT_TEST(testget);testget為測試函數
3.CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(testpoint, "alltest");將測試包testpoint注冊為alltest
4.main函數在測試包改變時基本不變;
  CppUnit::TestResult r;
   CppUnit::TestResultCollector rc;
   r.addListener(&rc); // 三句共同完成結果收集器的准備工作
  CppUnit::TestRunner runner; // 定義執行實體
   runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("alltest").makeTest());
   runner.run(r); // 運行測試
  CppUnit::TextOutputter o(&rc, std::cout);
   o.write(); // 將結果輸出
system("pause");//主要為了避免exe程序運行后立即退出;
return rc.wasSuccessful() ? 0 : -1;//若測試通過,則返回值為0;
今天主要了解測試程序編寫的相關細節,明天繼續測試稍復雜一些的類。
