HOWTO for CPPUNIT TESTFRAMEWORK

xiaoxiao2021-03-06  112

LCG Application Area -

LCG INFRASTRUCTURE:

SW - Testing

HOWTO for CPPUNIT TESTFRAMEWORK

What is cppunit?

How to Start Testing with cppunit

How to use cppunit in lcg apparea

LCG CPPUnit FAQ

Related information

What is cppunit?

CppUnit is a framework for writing unit tests in C and running them automatically, giving a report about success or failing tests. It is the C port of the famous JUnit framework for unit testing. CppUnit is one of the members of the "XUnit family" (JUnit, PerlUnit, PyUnit, QtUnit, ...) test frameworks free available from XP software. The test output is in XML or in text format for automatic testing and GUI based for supervised tests.The first port of JUnit to C was done by Michael Feathers. His versions can be found on the XProgramming software page. They are os-specific, so Jerome Lacoste provided a port to Unix / Solaris. His version can be found on the same page. The CppUnit project has combined and built both ON this work. The main purpose of cppunit is to support developers in doing their unit Testing of C Program.

CPPUNIT WORKS IN Linux, Solaris and Windows Platforms. See The SPI Supported Platforms Compilers At SPI External Software Service.

Return to Top of Page

How to start testing with Cppunit Installation check To use CppUnit the CppUnit libraries and include files should be installed or accessible to your machine. To check this point in a linux / unix machine you can type at the prompt $ cppunit-config [--version ] [-CFLAGS] [--LIBS] [--PREFIX] [--help]

Using the different optional flags you can inquire the system about the CppUnit installed version, the cflags and library path you have to use, where CppUnit was installed and this help.If you are using one of the initial shell scripts provided bySPI in the general

HowTo for Test Execution Frameworks all the needed information is loaded in the environment variables ($ PATH, $ LD_LIBRARY_PATH, $ CPPUNIT_LDFLAGS, $ CPPUNIT_INCLUDES, $ ACLOCAL) that you can check at the prompt with the echo command (echo $ PATH, ... ).

IF you are working in a

Scram Project, CPPUNIT SHOULD APPEAR AS External Tool. Type "Scram Tool List" at the Prompt and Check It. You can also Use "Scram Tool Info CPPUnit" to know more about the cppunit installed release.

If your development platform is Windows and Visual C you should check that all the steps described in HowTo for Test Execution Frameworks for the integration of the CppUnit in a windows platform with / without DFS access were well done.

What to read about the usage of cppunit

- The CppUnit Cookbook written by Michael Feathers can give you a first idea of ​​what is a "Fixture", a "Test Case", a "Test Suite", the "TestRunner" and the "TestFactoryRegistry" For background information on the basic. Design of The Framework The Reader Is Referred To Kent's Original Paper, SIMPLE SMALLTALK TESTING: with PATTERNS.

- The section "Using CPPUnit" from Crash Course In Using CPPUNIT Document Can Also Help.

- But maybe the easy way to start the sw testing with CppUnit is following the examples provided by SPI These are done using CppUnit Helper-Macros and in this way the work of the developer is reduced A unique test driver main program (placed at.. The cppunit incrude path) Is Able to handle cppunit tests classes in the test directory.how to jump inte Directory.how to jump inte

If you are really impatient to start the testing job you can take a look at a set of basic examples which you can download from the SPI repository. Unzip and un-tar the "Testing_examples.tar.gz" file. Take a look of the Readme File. IT Explains Where Are The CPPUNIT EXAMPLES AND HOW TO Run The DiffERIS / Windows.

Return to Top of Page

How to use cppunit in lcg apparea

Check that "cppunit-config" is in your path or that Cppunit is configured as an external tool in your SCRAM project. (See installation check item in this document). Make sure that in your CppUnit include path you have the CppUnit test driver " CPPUNIT_TESTDRIVER.CPP ". It always looks like this one: #include

#include

#include

#include

#include

#include

/ ** Main class for all the cppunit test classes

*

* This Will Be The Driver Class of All Your CPPUnit Test Classes.

* - All Registered CPPUnit Test Classes Will Be Run.

* - You Can Also Modify The Output (Text, Compiler, XML).

* - This class willover also integrate cppunit test with oval

* /

INT main (int Argc, char ** argv)

{

/// Get the top level suite from the registry

CPPUnit :: test * suite = cppunit :: testfactoryregistry :: getRegistry (). Maketest (); /// ads the test to the list of test to run

CPPUnit :: Textui :: Testrunner Runner;

Runner.addtest (Suite);

// Change the default outputter to a compiler error Format Outputter

// uncomment the folowing line if you need a compiler outputter.

Runner.setputPutter (New CPPUnit :: CompilerOutputter (& runner.result (),

Std :: cout);

// Change The Default Outputter to a XML Error Format Outputter

// UNComment the folowing line if you need a xml outputter.

//runner.setOutputTer (New CPPUnit :: Xmloutputter (& runner.result (),

// std :: cerr));

/// Run the Tests.

Bool Wassuccessful = Runner.Run ();

// if you want to avoid the cppunit Typical Output Change The Line Above

// by the folowing one:

// Bool Wassucessful = runner.run (", false, false, false);

// Return Error Code 1 if the one of test failed.

// uncomment the next line if you want to integrate cppunit with oval

IF (! Wassuccessful! = 0) std :: cerr << "Error: cppunit failures" << std :: end1

Std :: cout << "[oval] cppunit-result =" <

}

This test driver will look for all your registered CppUnit test classes and will run all of them providing and text / compiler / XML output. If all the tests pass, you will get an informative message. If any fail, you will get the following information :

The name of the test case that failed. The name of the source file that contains the test. The line number where the failure occurred All of the text inside the call to CPPUNIT_ASSERT () which detected the failure The last line of this example is used to integrate the CppUnit tests in the Oval Test FrameWork When you call Oval ( "oval run test_Package_X_test1" and "oval diff test_Package_X_test1") if any of the CppUnit tests fails Oval will receive an alert Note:.. Here we are following the naming conventions Define in The HOWTO Make SW-TESTS Document. Edit Your First CPPUnit Test Class, You Can Start with One Like this: / ** CPPUnit Test Class: Test_package_x_test1.cpp

*

* Use the helpermacros.h Header File to Establish in A Easy

* Way the test suite in which you can place all the tests

* Concerning the tsted class or component.

* /

#include

Class myclass_test: public cppunit :: testfixture {

/// brief definition of the unit test suite "myclass_test",

/// Don't forget to add all those tests ("Test1",

/// "TEST2" ...) You would like to run.

CPPUNIT_TEST_SUITE (myclass_test);

CPPUNIT_TEST (TEST1);

CPPUNIT_TEST (TEST2);

CPPUNIT_TEST_SUITE_END ();

Private:

Int alpha;

Int Beta;

PUBLIC:

/// brief override setup () to initialize the

/// Variables You Will Use for Test

Void setup () {

}

/// Write Now Your Own Tests, this is the first one

Void test1 () {

INT A = 10;

INT B (10);

CPPUNIT_ASSERT (a == b);

}

/// Write Now Your Own Tests, this is the first one

Void test2 () {

Float tol = 0.05;

Float a = 10 .;

Float B = a Tol / 10 .;

CPPUNIT_ASSERT_DOUBLES_EQUAL (a, b, tol);

// final clean up

//

Void teardown () {

}

}

// Class Registration On CPPUnit Framework

/// brief you have to register the test suite "complexnumbertest". in this

/// it will be recognized by The May Test Program Which Drives

/// DiffERENT TESTS IN A Specific Package Test Directory.

CPPUNIT_TEST_SUITE_REGISTRATION (myclass_test);

// CPPUNIT TEST-Driver Common for all the cppunit test classes

#include

Note: If you do not like to type you can use the script lcg-distributeTest.py and you will receive all the stuff for testing with CppUnit and other test-frameworks At this point you should be able to compile "test_Package_X_test.cpp. "with your own makefile or through scram (scram b), product the Executable" Test_package_x_test "and Run It with the folload result: $> test_package_x_test

.

OK (2 tests)

[Oval] cppunit-result = 0

The output says that there are 2 cppunit tests and no failures. The "[OVAL] Cppunit-result = 0" tag is intended for the integration of CppUnit and Oval. Now you can write your new test methods or if you prefer you can package them into another CppUnit test class like this we discussed but without the last line including the Cppunit test driver The new test suite you defined is added to the test suite defined in the previous step in any case remember what is required by CppUnit..:

Include the CppUnit Helper Macros: #include Your test class must be derived publically from CppUnit :: TestFixture Define your test suite and give a name using CPPUNIT_TEST_SUITE (MyTestSuiteName); Add as many tests as you want to this test-suite using CPPUNIT_TEST (myfirsttest); and end the suite declaration with CPPUNIT_TEST_SUITE_END (); Use the methods setUp () and tearDown () to set and to release any permanent resources you allocate for the test-suite Write your tests using the CppUnit macros for making assertions:.. CPPUNIT_ASSERT (condition) Assertions that a condition is true CPPUNIT_ASSERT_MESSAGE (message, condition) Assertion with a user specified message CPPUNIT_FAIL (message) Fails with the specified message CPPUNIT_ASSERT_EQUAL (expected, actual) Asserts that two values Are Equals. CPPUNIT_ASSERT_EQUAL_MESSAGE (MESSAGE, EXPECTED, ACTUAL) Asserts That Two Values ​​Are Equals, Provides Additional Message on Failure CPPUNIT_ASSERT_DOUBLES_EQUAL (Expected , actual, delta) Macro for Primitive Value Comparisons. Don't forget to register your test suite with cppunit_test_suite_registration (mytestsuitename).

ONCE more Remember,

Why do you need unit testing ?:

Before writing code, it forces you to detail your requirements in a useful way While writing code, it keeps you from over-coding. When all the test cases pass, the function is complete. When refactoring code, it assures you that the new version BEHAVES The Same Way As The Old Version .hen Maintaining Code, IT Helps You Cover Yourself When Someone Comes Screaming That Your Latest Change Broke Their Old Code.

Return to Top of Page

LCG CppUnit FAQ 1.- How can I check if my test throws an exception The CppUnit helper macros also help you to check the exceptions For this:?. Add the test of your exception to the test suite using CPPUNIT_TEST_EXCEPTION (testMethod, ExceptionType) and Specifying the expected exception type. for example: cppunit_test_exception (TestVectorAtthrow, std :: invalid_argument);

Write the Test Case Method TestMethod #include

#include

#include

........

Void testoratthrow ()

{

Std :: Vector

v (2);

v.at (1); // this will not throw exception std :: invalid_argument

}

CPPUNIT WILL ANSWER with the FOLLOWING FAILURE:

1) TEST: EXAMPLETESTCASE.TESTVECTORATTHROW (F)

"EXPECTED EXCEPTION OF TYPE STD_INVALID_ARGUMENT, But Got None"

2.- How can I do test for failure It is not enough to test that our functions succeed when given good input;? We must also test that they fail when given bad input And not just any sort of failure;. They must fail in To do this:

Add The Test Which Will Fail To The Test Suite Using CPPUnit_Test_Fail (TestMethod). For example: cppUnit_test_fail (Testassertfalsefail);

Write the Test Case That Should Fail: void testassertfalsefail ()

{

CPPUNIT_ASSERT (1 == 0);

}

Return to Top of Page

Related Information Useful Links:

CppUnit web site http://CppUnit.sourceforge.net/cgi-bin/moin.cgi CppUnit project in sourceForge http://sourceforge.net/projects/cppunit CppUnit mailing list. Check it to know if your problem was already solved. CPPUNIT forums. CHECK THEM for Help. CPPUnit CVS Repository Organized View of CPPUnit Classes CPPUnit Releases Available At SPI External Software Service

Return to Top of Page

LCG Apparea SPI Support

Last Modified: Tue Sep 16 12:26:57 CEST 2003 $ Revision: 1.9 $ $ Author: Mgallas $

转载请注明原文地址:https://www.9cbs.com/read-102754.html

New Post(0)