I would like to use Catch unit test framework for testing my projects. I read tutorial how to write tests, it was pretty simple. I tried to create really simple project in QT creator, which does include these files:
main.cpp
tests.cpp
factorial.cpp
factorial.h
catch.hpp
main.cpp :
#include <stdio.h>
#include "factorial.h"
int main(void)
{
printf("%d", factorial(5));
return 0;
}
tests.cpp :
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "factorial.h"
TEST_CASE( "factorial on valid numbers", "[factorial]" ) {
REQUIRE( factorial(1) == 1 );
REQUIRE( factorial(5) == 120 );
REQUIRE( factorial(10) == 3628800 );
}
factorial.cpp :
#include "factorial.h"
int factorial(int number)
{
if(number < 0)
return -1;
int result = 1;
for(int i = number; i > 0; i--)
result *= i;
return result;
}
factorial.h :
#ifndef FACTORIAL
#define FACTORIAL
int factorial(int number);
#endif // FACTORIAL
and catch.hpp is catch framework for unit tests
I am coding in C, not C++, extension ".cpp" is just because of catch, which is not working with files that have extension ".c"
There is one more file: testing.pro, which contains
Q
MAKE_CFLAGS += -std=c99 -pedantic -Wall -Wextra
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp \
factorial.cpp \
tests.cpp
include(deployment.pri)
qtcAddDeployment()
HEADERS += \
catch.hpp \
factorial.h
This file was generated by QT creator.
Ok, and my problem is, that when I try to build this project, I get error: "multiple definition of main".
I get it. I have main in main.cpp file and also in tests.cpp. But I don't know, what should I do to make it work. I want to have project, with fully working main and file with tests, where I can test my functions. I searched almost everywhere. I guess I have to organize my project somehow in QT creator, but I have no idea how. I have no idea how it should work.
Thanks for advices
You do indeed have two mains:
The solution is to either remove the macro and use your main, or leave out your main to use the Catch-generated one.
More info here: Catch: Supplying your own main()