Integrando com uma função main

Para integrar com uma função main, você pode colocar o conteúdo dos testes junto com o código fonte do projeto. Por exemplo, em uma pasta "test", e utilizar os argumentos da linha de comandos para executar os testes caso, por exemplo, seja passado o argumento "test". Vamos ao exemplo. Você pode fazer o seguinte:

#include "test/calctests.h"

#include <xutest/xutest.h>
#include <cstring>

/* Código do main.cpp */

int main( int argc, char* argv[] ) {
    if ( argc == 2 ) {
        if ( strcmp( argv[ 1 ], "test" ) == 0 ) {
            RUN_ALL_TEST_CASES();
            return 0;
        }
    }

    /* Restante do código fonte do main.cpp */
    return 0;
}

Então vamos ao exemplo:

Na pasta do projeto, crie a pasta "libmath" e, dentro dela, crie o arquivo "libmath.h":

libmath/libmath.h
#ifndef LIBMATH_H
#define LIBMATH_H

int fatorial( int n );

#endif

Agora crie o arquivo "libmath.cpp", dentro da pasta "libmath":

libmath/libmath.cpp
#include "libmath.h"

int fatorial( int n ) {
    if ( n <= 1 )
        return 1;
    return n * fatorial( n-1 );
}

Agora crie a pasta "test" e, dentro dela, crie o arquivo "libmathtest.h" com o seguinte conteúdo:

test/libmathtest.h
#include <xutest/xutest.h>

#include "../libmath/libmath.h"

TEST_CASE( fatorialTest, LibMathTests ) {
    ASSERT_EQUALS( fatorial( 0 ), 1, )
    ASSERT_EQUALS( fatorial( 1 ), 1, )
    ASSERT_EQUALS( fatorial( 2 ), 2, )
    ASSERT_EQUALS( fatorial( 3 ), 6, )
    ASSERT_EQUALS( fatorial( 4 ), 24, )
    ASSERT_EQUALS( fatorial( 5 ), 120, )
}

Agora crie o arquivo "main.cpp" na raiz do projeto

#include "test/libmathtest.h"
#include "libmath/libmath.h"

#include <xutest/xutest.h>
#include <cstring>

#include <iostream>

using namespace std;

int main( int argc, char* argv[] ) {
    if ( argc == 2 ) {
        if ( strcmp( argv[ 1 ], "test" ) == 0 ) {
            RUN_ALL_TEST_CASES();
            return 0;
        }
    }

    int n;
    cout << "Informe um número para calcular o fatorial: ";
    cin >> n;

    int fat = fatorial( n );
    cout << "Fatorial de " << n << " é: " << fat << endl;

    return 0;
}

Feito isso, seu projeto deve ter a seguinte estrutura:

            
main-integration
├── libmath
│   ├── libmath.cpp
│   └── libmath.h
├── main.cpp
└── test
    └── libmathtest.h

Agora vamos compilar tudo:

g++ -o libmath.o -c libmath/libmath.cpp
g++ -o main.o -c main.cpp

Agora vamos linkar:

Se estiver no linux, faça:
g++ -o main main.o libmath.o -lxutest -ldl -rdynamic
Se estiver no windows, faça:
g++ -o main.exe main.o libmath.o -lxutest

Agora, após gerado o executável de nome "main", você pode executar os testes ou executar o programa fatorial que criou.

Para executar os testes faça:

Se estiver no windows, faça:
.\main.exe test
Se estiver no linux, faça:
./main test

Para executar o programa fatorial faça:

Se estiver no windows, faça:
.\main.exe
Se estiver no linux, faça:
./main

Faça o download do projeto logo abaixo:

Download: main-integration.zip

Próxima aula

O próxima aula ensina como utilizar o xutest com o foxmake.