源碼編譯CUnit 以及CppUnit。
查看檔案
安裝成功。
我使用的是netbeans 開發工具,遠端連線centos進行C/C++的開發。
Cunit 專案檔
配置cunit靜態串連庫及運行環境
編寫測試檔案:
/* * File: CUnitTest.c * Author: Vicky.H * * Created on 2012-3-31, 14:22:29 */#include <stdio.h>#include <stdlib.h>#include "CUnit/Basic.h"/* * CUnit Test Suite */int init_suite(void) { return 0;}int clean_suite(void) { return 0;}void test1() { CU_ASSERT(2 * 2 == 4);}void test2() { CU_ASSERT(2 * 2 == 5);}int main() { CU_pSuite pSuite = NULL; /* Initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); /* Add a suite to the registry */ pSuite = CU_add_suite("CUnitTest", init_suite, clean_suite); if (NULL == pSuite) { CU_cleanup_registry(); return CU_get_error(); } /* Add the tests to the suite */ if ((NULL == CU_add_test(pSuite, "test1", test1)) || (NULL == CU_add_test(pSuite, "test2", test2))) { CU_cleanup_registry(); return CU_get_error(); } /* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); CU_cleanup_registry(); return CU_get_error();}
運行測試:
CppUnit專案檔
Hello.h
#ifndef HELLO_H#defineHELLO_H#include <string>class Hello {public: Hello(); Hello(const Hello& orig); virtual ~Hello(); std::string sayHello();private:};#endif/* HELLO_H */
Hello.cpp
#include "Hello.h"Hello::Hello() {}Hello::Hello(const Hello& orig) {}Hello::~Hello() {}std::string Hello::sayHello() { return "Hello World !";}
配置CppUnit靜態庫和運行環境
HelloTest.h
#ifndef HELLOTEST_H#defineHELLOTEST_H#include "Hello.h"#include <cppunit/extensions/HelperMacros.h>class HelloTest : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(HelloTest); CPPUNIT_TEST(testSayHelloMethod); CPPUNIT_TEST_SUITE_END();public: HelloTest(); virtual ~HelloTest(); void setUp(); void tearDown();private: void testSayHelloMethod(); Hello hello2;};#endif/* HELLOTEST_H */
HelloTest.cpp
#include "HelloTest.h"CPPUNIT_TEST_SUITE_REGISTRATION(HelloTest);HelloTest::HelloTest() {}HelloTest::~HelloTest() {}void HelloTest::setUp() {}void HelloTest::tearDown() {}void HelloTest::testSayHelloMethod() { CPPUNIT_ASSERT(hello2.sayHello() == "Hello World !");}
#include <cppunit/BriefTestProgressListener.h>#include <cppunit/CompilerOutputter.h>#include <cppunit/extensions/TestFactoryRegistry.h>#include <cppunit/TestResult.h>#include <cppunit/TestResultCollector.h>#include <cppunit/TestRunner.h>int main() { // Create the event manager and test controller CPPUNIT_NS::TestResult controller; // Add a listener that colllects test result CPPUNIT_NS::TestResultCollector result; controller.addListener(&result); // Add a listener that print dots as test run. CPPUNIT_NS::BriefTestProgressListener progress; controller.addListener(&progress); // Add the top suite to the test runner CPPUNIT_NS::TestRunner runner; runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest()); runner.run(controller); // Print test in a compiler compatible format. CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut()); outputter.write(); return result.wasSuccessful() ? 0 : 1;}
運行測試,測試結果: