现在的位置: 首页 > 综合 > 正文

netbeans linux cunit cppunit 使用

2013年08月05日 ⁄ 综合 ⁄ 共 2780字 ⁄ 字号 评论关闭

源码编译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
#define	HELLO_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
#define	HELLOTEST_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;
}

运行测试,测试结果:

抱歉!评论已关闭.