Cppunit getting started-a series of conversations between Tony and Alex

Source: Internet
Author: User

TONY: Hi Alex! You just looks like drowing in your project. What is up?
ALEX: our project requires the introduction of unit tests, but I'm 've no experience in unit test.
TONY: I think cppunit is your best choice.
ALEX: Yes. I just pulled it down from the Internet and I'm going to study it.
TONY: Really? I have done some practice on unit test before. wocould you like me to join you?
ALEX: Oh Tony, I'm so gglad that you cocould HELP ME!
TONY: My pleasue!
ALEX: where do we start?
TONY: the simplest case! Let's take a simple example. Now we have a class with the name "simplecalculator" and it has four basic methods 'add', 'sub ', 'mul' and 'div ', all we shoshould do is to test whether these methods run as same as we have CT. first of all, complete the "simplecalculator" class, Alex.
ALEX: It is simple!

// Simplecalculator. h
Class simplecalculator {
Public:
Int add (int A, int B );
Int sub (int A, int B );
Int MUL (int A, int B );
Int Div (int A, int B );
};

// Simplecalculator. cpp
Int simplecalculator: add (int A, int B ){
Return A + B;
}

Int simplecalculator: Sub (int A, int B ){
Return A-B;
}

Int simplecalculator: MUL (int A, int B ){
Return a * B;
}

Int simplecalculator: div (int A, int B ){
Return A/B;
}

ALEX: This is simple. The DIV method does not consider the exception of Division 0.
Tony: Yes. Remember the test-driven development I mentioned last time, but today we are not using it. We just want to do something simple to familiarize ourselves with the use of tools.
ALEX: Should we also list a list of test cases?
TONY: That's right.
ALEX: Let me write a few. "Add (5, 6) = 11", "sub (5, 6) =-1", "MUL (5, 6) = 30", and "Div (12, 6) = 2 ".
TONY: Let's take a look at the cppunit help document.

(Tony and Alex are reading the doc of cppunit .)

TONY: what have you learned?
ALEX: It seems that the test tools of these xunit frameworks are almost identical in terms of concepts, such as testcase, testfixture, and testsuite.
TONY: Good. unit testing is about testing ideas. A tool is just a necessary condition. It cannot determine whether your testing is a good test. Next you should follow the cppunit you understand.

ALEX: OK. According to the book, we have to test multiple methods at a time. We 'd better use testfixture. This is what I wrote. Look at it.

# Include "simplecalculator. H"
# Include "cppunit/testcase. H"
# Include "cppunit/testresult. H"
# Include "cppunit/textoutputter. H"
# Include "cppunit/testresultcollector. H"
# Include "cppunit/testcaller. H"
# Include "cppunit/extensions/helpermacros. H"

Class simplecalctest: Public cppunit_ns: testfixture {
PRIVATE:
Simplecalculator * SC;

Public:
Virtual void setup (){
SC = new simplecalculator ();
}
Virtual void teardown (){
Delete SC;
}
 
Void testadd (){
Cppunit_assert_equal (SC-> Add (5, 6), 11 );
}

Void testsub (){
Cppunit_assert_equal (SC-> sub (5, 6),-1 );
}

Void testmul (){
Cppunit_assert_equal (SC-> MUL (5, 6), 30 );
}

Void testdiv (){
Cppunit_assert_equal (SC-> Div (12, 6), 2 );
}
};

Our main function is as follows:
Int main ()
{
Cppunit_ns: testresult R;
Cppunit_ns: testresultcollector result;
R. addlistener (& result );

Cppunit_ns: testcaller <simplecalctest> testcase1 ("testadd", & simplecalctest: testadd );
Cppunit_ns: testcaller <simplecalctest> testcase2 ("testsub", & simplecalctest: testsub );
Cppunit_ns: testcaller <simplecalctest> testcase3 ("testmul", & simplecalctest: testmul );
Cppunit_ns: testcaller <simplecalctest> testcase4 ("testdiv", & simplecalctest: testdiv );

Testcase1.run (& R );
Testcase2.run (& R );
Testcase3.run (& R );
Testcase4.run (& R );

Cppunit_ns: textoutputter out (& result, STD: cout );
Out. Write ();
Return 0;
}

TONY: I think it is feasible. Run it to see how it works.
ALEX: The output is as follows:
OK (4 tests)

TONY: This is indeed a feasible method, but let's look back at the content in the doc we learned together to see if there is room for improvement. Now, if you want to add a test case method in the simplecalctest class, not only does simplecalctest need to be modified, but our main function also needs to be modified. Do you still remember the concept in JUnit to support it?

ALEX: You don't mention it. I really can't remember it. There is testsuite in JUnit. I also saw the suite method in cppunit doc just now and may be helpful, wait a moment and I will flip the document ....

ALEX: I found it. There are indeed simpler methods. Modify the file. The referenced header file remains unchanged.

Class simplecalctest: Public cppunit_ns: testfixture {

Cppunit_test_suite (simplecalctest );
Cppunit_test (testadd );
Cppunit_test (testsub );
Cppunit_test (testmul );
Cppunit_test (testdiv );
Cppunit_test_suite_end ();

PRIVATE:
Simplecalculator * SC;

Public:
Virtual void setup (){
SC = new simplecalculator ();
}
Virtual void teardown (){
Delete SC;
}
 
Void testadd (){
Cppunit_assert_equal (SC-> Add (5, 6), 11 );
}

Void testsub (){
Cppunit_assert_equal (SC-> sub (5, 6),-1 );
}

Void testmul (){
Cppunit_assert_equal (SC-> MUL (5, 6), 30 );
}

Void testdiv (){
Cppunit_assert_equal (SC-> Div (12, 6), 2 );
}
};

Cppunit_test_suite_registration (simplecalctest );

The main function is modified as follows:
Int main ()
{
Cppunit_ns: testresult R;
Cppunit_ns: testresultcollector result;
R. addlistener (& result );

Cppunit_ns: testfactoryregistry: getregistry (). maketest ()-> Run (& R );
Cppunit_ns: textoutputter out (& result, STD: cout );
Out. Write ();
Return 0;
}

Cppunit uses macros to solve suite problems. Write the following code in your testcase definition:
Cppunit_test_suite (yourtestcase );
Cppunit_test (testxx );
...//
Cppunit_test_suite_end ();
This Code actually defines a function suite, which returns a test set that contains all the test cases defined by cppunit_test. Cppunit_test_suite_registration register the test set to the global test tree through static registration, and finally pass cppunit_ns: testfactoryregistry: getregistry (). maketest () generates and runs a test that contains all test cases. In this way, once a new test case function is added, we only need to modify the simplecalctest class.

TONY: Well done! Alex, you are more and more capable of solving problems independently. I believe that you have a deep understanding of this. In the future, we will explore cppunit usage experience in your actual project.

ALEX: Haha. Thank you!

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.