In extreme Programming, the test program should be written before writing the main program, and then put the written class program in the test program to test, but considering the project requirements of the document and so does not define the interface, I can not start, and, I was just familiar with unit testing, need to write a test program while solving problems encountered , the time period is longer. In this programming, I am directly to the GitHub on the group's program to download, see what interface, and then write the test program.
Test object: Solver Class (Https://github.com/TeamWork-Robot/Team1/tree/master/Robot) in the Robot class The main reason for choosing the Solver class is that a function that is easy to test is not found in the 1.robot class, and the return value is generally void;2. Since the point class, frame class is relatively simple, there is nothing to test, and the Solver class itself has a certain degree of complexity, and the function return type is generally point;
After writing the test program of the Solver class, I found that there were too many errors, and it took a long time to solve the problem (mainly to find that the Solver class itself still has a lot of improvement); and then retreat to the second, write a program that can test the point class, solve the problems encountered.
1#include <cppunit/extensions/HelperMacros.h>2#include <Point.h>3 4 classTestPoint: Publiccppunit::testfixture5 {6 Cppunit_test_suite (testpoint);7 cppunit_test (testget);8 //cppunit_test (testrotate);9 cppunit_test_suite_end ();Ten Public: One voidsetUp (); A voidTearDown (); - testpoint (); - //~testsolver (); the voidtestget (); - //void Testrotate (); -};
test_point.h
1#include"test_point.h"2#include"Point.h"3#include <string>4#include <iostream>5#include <cppunit/TestCase.h>6#include"cppunit/testassert.h"7 8Cppunit_test_suite_named_registration (TestPoint,"alltest");9 Ten Testpoint::testpoint () One { A }; - - voidTestpoint::setup () the { -Std::cout <<"Test begin"<<Std::endl; - }; - + voidTestpoint::teardown () - { +Std::cout <<"Test End"<<Std::endl; A }; at - voidTestpoint::testget () - { -Point Point1 (3,4); - intResultx =point1.getx (); - intResulty =point1.gety (); inCppunit_assert (Resultx = =3); -Cppunit_assert (resulty = =4); to}
Test_point.cpp
1#include <cppunit/TestResultCollector.h>2#include <cppunit/BriefTestProgressListener.h>3#include <cppunit/TextOutputter.h>4 5 intMain ()6 {7 Cppunit::testresult R;8 Cppunit::testresultcollector RC;9R.addlistener (&RC);//Prepare the result collector.Ten OneCppunit::testrunner runner;//defining an execution entity ARunner.addtest (Cppunit::testfactoryregistry::getregistry ("alltest"). Maketest ()); -Runner.run (R);//Run Tests - theCppunit::textoutputter O (&RC, std::cout); -O.write ();//Output The result - -System"Pause"); + - returnRc.wassuccessful ()?0: -1; +}
Test_point_main.cpp
After adding the header file and the source program to the point class, the result is as follows: (Add system ("pause");)
After adding the return value of the Get function in Point.cpp to the human, the result is as follows:
Here are some of the problems encountered and solutions, for those who need to solve similar problems for students to provide reference:
1.fatal error C1083: Could not open include file: "Point.h": No such files or directory: The solution to this problem is as follows, first find the location of the error file, right-click Project Properties->c/c++, General- > Attach the Include directory and put the error file location in.
2. Error C2011: "Point": "Class" type redefined, because the Point.h file does not use a macro definition at first, resulting in Point.h recompilation when running the test class; resolution to add a macro definition to the Point.h file #ifndef point _h #define POINT_H #endif即可.
Program-related statement explanation:
1.class testpoint:p ublic cppunit::testfixture inherit from Testfixture class TestPoint;
2.cppunit_test_suite (testpoint); add TestPoint to a test package
Cppunit_test (testget); Testget as a test function
3.cppunit_test_suite_named_registration (TestPoint, "alltest"); Register test package TestPoint as AllTest
The 4.main function basically does not change when the test package changes;
Cppunit::testresult R;
Cppunit::testresultcollector RC;
R.addlistener (&RC); Three sentences together to complete the preparation of the result collector
Cppunit::testrunner runner; Defining an execution entity
Runner.addtest (Cppunit::testfactoryregistry::getregistry ("AllTest"). Maketest ());
Runner.run (R); Run Tests
Cppunit::textoutputter O (&RC, std::cout);
O.write (); Output the result
System ("pause");//mainly in order to avoid EXE program to quit immediately after running;
Return rc.wassuccessful ()? 0: -1;//If the test passes, the return value is 0;
Today, we mainly understand the details of the test program writing and continue to test the slightly more complex classes tomorrow.
The first complete Cppunit unit test program