C ++ Unit testing tool CppUnit instance learning

Source: Internet
Author: User
Tags error code

During the test, teachers always use JUnit as a demonstration. Although java is also required to write web pages, they prefer to use c/c ++ to write programs. So I found a unit test framework of c ++-CppUnit.
CppUnit is very similar to Junit, so it is more convenient to get started, but some macros in it are still a bit difficult to remember.

First, write a class for testing:

The code is as follows: Copy code
// Add. h

ClassAdd

{

Public:

Add ();

Virtual ~ Add ();

Voidadd (inta );

Voidset (int );

IntgetResult ();

Private:

Intnumber;

};



This class first sets the size of the original value, then you can call the add () function to increase the size of the number, call the getRestlt () function to obtain the current value of the number.

Write a Test class based on this class:

The code is as follows: Copy code
// Testadd. h

# Include <cppunit/extensions/HelperMacros. h>

Class TestAdd: public CppUnit: TestFixture

{

CPPUNIT_TEST_SUITE (TestAdd );

CPPUNIT_TEST (testAdd );

CPPUNIT_TEST_SUITE_END ();

Public:

Void setUp ();

Void tearDown ();

Void testAdd ();

};



Add the test class through the macro CPPUNIT_TEST_SUITE, and then Add the function used for testing in this class. Here there is only one function used to test the Add class, that is, the testAdd () function, therefore, use CPPUNIT_TEST (testAdd); to set this function as a Test function (similar to the @ Test annotation in JUnit ). If you want to add other functions, add them here, and then end the Test definition CPPUNIT_TEST_SUITE_END ();

This class can have two functions to set the environment before running the test and clean up the test. This is similar to JUnit.

Implementation of the Test class:

The code is as follows: Copy code
# Include "TestAdd. h"

# Include "../src/Add. h"

CPPUNIT_TEST_SUITE_REGISTRATION (TestAdd );

VoidTestAdd: setUp ()

{

}

VoidTestAdd: tearDown ()

{

}

VoidTestAdd: testAdd ()

{

Addadd;

Add. set (5 );

Add. add (10 );

CPPUNIT_ASSERT_EQUAL (16, add. getResult ());

}



At the beginning of the implementation file, you need to use CPPUNIT_TEST_SUITE_REGISTRATION to register this class into the Test component, so that CppUnit can find the test class by itself.

In the test function testAdd, the asserted CPPUNIT_ASSERT_EQUAL is used to test whether the values of the Add class operation and the expected values are equal (the intentionally written values are different here)

The code is as follows: Copy code
Main. cpp:

# Include <cppunit/CompilerOutputter. h>

# Include <cppunit/extensions/TestFactoryRegistry. h>

# Include <cppunit/ui/text/TestRunner. h>

Intmain (intargc, char * argv [])

{

// Get the top level suite from the registry

CppUnit: Test * suite = CppUnit: TestFactoryRegistry: getRegistry (). makeTest ();

// Adds the test to the list of test to run

CppUnit: TextUi: TestRunnerrunner;

Runner. addTest (suite );

// Change the default outputter to a compiler error format outputter

Runner. setOutputter (newCppUnit: CompilerOutputter (& runner. result (),

Std: cerr ));

// Run the tests.

BoolwasSucessful = runner. run ();

// Return error code 1 if the one of test failed.

ReturnwasSucessful? 0: 1;

}



The code here is copied directly from the example. The test component gets the code directly from TestFactoryRegistry and sets the test error to be output from the standard error stream.

Final running output:

The code is as follows: Copy code
. F

TestAdd. cpp: 29: Assertion

Test name: TestAdd: testAdd

Equality assertion failed

-Expected: 16

-Actual: 15

Failures !!!

Run: 1 Failure total: 1 Failures: 1 Errors: 0
Related Article

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.