標籤:
在極限編程中,測試程式本應該在編寫主程式之前就要寫好,然後將寫好的類程式放在測試程式中進行測試,但考慮到項目中需求文檔等並未將介面定義好,我無從開始,而且,自己對單元測試也是剛剛熟悉,需要一邊寫測試程式一邊解決遇到的問題,時間周期較長。在本次編程中,我是直接把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 };test_point.h
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 }test_point.cpp
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 }test_point_main.cpp
把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;
今天主要瞭解測試程式編寫的相關細節,明天繼續測試稍複雜一些的類。
第一個完整的cppunit單元測試程式