netbeans linux cunit cppunit 使用

來源:互聯網
上載者:User

源碼編譯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;}

運行測試,測試結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.