CppUnit

De µRV - La réalité virtuelle au département informatique
Aller à : navigation, rechercher

Ubuntu

Installation

sudo apt-get install libcppunit-1.12-1 libcppunit-dev libcppunit-doc

Compilation

La bibliothèque cppunit est ajoutée lors de la compilation avec l'option -lcppunit

g++ TestSample.h main.cpp -lcppunit -o tests

Windows

Installation

Télécharger les sources sur le site suivant : http://sourceforge.net/apps/mediawiki/cppunit/index.php?title=Main_Page Version 1.2.1

Compilation

  1. Lire le fichier INSTALL_WIN32.txt
  2. Lancer le fichier examples/examples avec Visual Studio 2010
  3. Convertir tous les projets
  4. Lancer la compilation sous le mode Release DLL
  5. Sur le projet de test, ajouter dans "Propriété > Propriétés de configuration > Éditeur de liens > Répertoires de liens supplémentaires" le dossier où sont situé les DLL de cppunit (cppunit_dll.dll)

Exécution

  • . --> Run : Ok
  • .F --> Failure : Le résultat n'est pas celui attendu
  • .E --> Error : Le code plante
./tests
...F

!!!FAILURES!!!
Test Results:
Run:  3   Failures: 1   Errors: 0


1) test: testFail (F) line: 43 TestSample.h
forced failure
- TODO

Fichiers d'exemples

Syntaxe 1

Classe de test

#include <vector>
#include <stdexcept>

#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestCase.h>
#include <cppunit/TestCaller.h>

/**
 * Test Sample class.
 *
 * @see http://cppunit.sourceforge.net/doc/cvs/group___assertions.html
 * @see Sample
 */
class TestSample : public CppUnit::TestCase {

    private:
        /** Tested object. */
        //Sample sample;
	
    public:
      
        /** Test Sample#assert method. */
        void testAssert()
        {
	        //CPPUNIT_ASSERT (condition)
	        CPPUNIT_ASSERT(true);  
	  
	        //CPPUNIT_ASSERT_EQUAL(expected, actual)
	        int i = 5 + 1;
	        CPPUNIT_ASSERT_EQUAL(6, i);
	    
	        //CPPUNIT_ASSERT_DOUBLES_EQUAL (expected, actual, delta)
	        double d = 5.0 + 1.;
	        CPPUNIT_ASSERT_DOUBLES_EQUAL(6., d, 0.001);
	    }

        /** Test Sample#throw method. */
        void testThrow()
        {
	        //CPPUNIT_ASSERT_THROW (expression, ExceptionType)
	        std::vector<int> v;
	        CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range );
        }
	
        /** Test Sample#fail method. */
        void testFail()
        {
	        //CPPUNIT_FAIL (message)  	
	        CPPUNIT_FAIL("TODO");
        }


       /** Return suite of test methods for runner.
         * <ul> You should change :
         *  <li>Name of the class</li>
         *  <li>Method name</li>
         * </ul>
         */ 
        static CppUnit::Test *suite(){
            CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite("TestSample");
            suiteOfTests->addTest(new CppUnit::TestCaller<TestSample>("testAssert", &TestSample::testAssert));
            suiteOfTests->addTest(new CppUnit::TestCaller<TestSample>("testThrow", &TestSample::testThrow));
            suiteOfTests->addTest(new CppUnit::TestCaller<TestSample>("testFail", &TestSample::testFail));
            return suiteOfTests;
        }
};

Runner

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

#include "TestSample.h"

using namespace std;

int main()
{

    CppUnit::TextUi::TestRunner runner;

    runner.addTest( TestSample::suite() );

    runner.run();

    return 0;
}

Syntaxe 2

Classe de test

#include <vector>
#include <stdexcept>

#include <cppunit/extensions/HelperMacros.h>

/**
 * Test Sample class.
 *
 * @see http://cppunit.sourceforge.net/doc/cvs/group___assertions.html
 * @see Sample
 */
class TestSample : public CppUnit::TestCase {

    CPPUNIT_TEST_SUITE( TestSample );
    CPPUNIT_TEST( testAssert );
    CPPUNIT_TEST( testThrow );
    CPPUNIT_TEST( testFail );
    CPPUNIT_TEST_SUITE_END();

    private:
        /** Tested object. */
        //Sample sample;
	
    public:
      
        /** Test Sample#assert method. */
        void testAssert()
        {
	        //CPPUNIT_ASSERT (condition)
	        CPPUNIT_ASSERT(true);  
	  
	        //CPPUNIT_ASSERT_EQUAL(expected, actual)
	        int i = 5 + 1;
	        CPPUNIT_ASSERT_EQUAL(6, i);
	    
	        //CPPUNIT_ASSERT_DOUBLES_EQUAL (expected, actual, delta)
	        double d = 5.0 + 1.;
	        CPPUNIT_ASSERT_DOUBLES_EQUAL(6., d, 0.001);
	    }

        /** Test Sample#throw method. */
        void testThrow()
        {
	        //CPPUNIT_ASSERT_THROW (expression, ExceptionType)
	        std::vector<int> v;
	        CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range );
        }
	
        /** Test Sample#fail method. */
        void testFail()
        {
	        //CPPUNIT_FAIL (message)  	
	        CPPUNIT_FAIL("TODO");
        }
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestSample);

Runner

#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include "TestSample.h"

using namespace CppUnit;

int main (int argc, char* argv[]) {
    TextUi::TestRunner runner;
    TestFactoryRegistry& registry = TestFactoryRegistry::getRegistry();

    // run all tests if none specified on command line
    Test* test_to_run = registry.makeTest();
    if (argc == 2)
        test_to_run = test_to_run->findTest(argv[1]);

    runner.addTest( test_to_run );
    bool failed = runner.run("", false);
    return !failed;
}

Makefile

CXX = g++
LIBS = -lcppunit
CXXFLAGS = -O2
TARGET = tests
SRC = $(wildcard *.h)

compile:
	${CXX} ${SRC} main.cpp ${CXXFLAGS}  ${LIBS} -o ${TARGET}

run:
	./${TARGET}

clean:
	rm ${TARGET}

help:
	@echo "Accepted commandes:"
	@echo "... compile"
	@echo "... run"
	@echo "... clean"
	@echo "... help"