Use netbeans Linux cunit cppunit

Source: Internet
Author: User

Compile cunit and cppunit in source code.

 

View files

 

The installation is successful.

 

I am using the netbeans development tool to remotely connect to centos for C/C ++ development.

Cunit project file

Configure the cunit static Connection Library and runtime environment

Compile the test file:

/* * 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();}

Run the test:

 

 

Cppunit project file

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 !";}

Configure the cppunit static library and runtime environment

 

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;}

Run the Test. Test results:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.