Computing unit tests in QT creator - multiple definitions of core
I would like to use the Catch unit test framework to test my projects. I read a tutorial on how to write tests, it was pretty simple. I tried to create a really simple project in QT builder that includes 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 the catch scheme for unit tests
I am coding in C, not C ++, the ".cpp" extension only happens because of the catch which does not work with files with the ".c" extension
There is another file called testing.pro containing
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 created by the creator of QT.
Ok, and my problem is that when I try to build this project, I get the error: "multiple definition of main".
I understand. I have a main file called main.cpp and also in tests.cpp. But I don't know what to do to make it work. I want to have a project with a fully working main and test file where I can test my functions. I have searched almost everywhere. I guess I need to somehow organize my project in the QT creator, but I have no idea how to do this. I have no idea how this is supposed to work.
Thanks for the advice
source to share
You do have two networks:
- one in main.cpp
- provided by Catch, as per your request, using the definition of the CATCH_CONFIG_MAIN macro.
The solution is to either delete the macro or use the main part or not use the main part to use the generated Catch.
More info here: Catch: Providing your own main ()
source to share