Issues on Qt test framework
Hello everyone !
I'm a in a professionnal licence (France) and i have issue on my tutored project.
I have a big problem on the Qt test framework and online tutorials I find didn't help me at all ... All take for exemple the ToUpper() method test, but the issue is because i didn't find how to use this freaking tests on my own classes ! T-T
I've some questions on QT Test:
-> do I made a .cpp and a .h for my test class ? Or putting all on the .cpp ?
-> I my class need to be in antoher project ? Or in the same project ? If i need to make another project, how to link both ?
-> do i need to importe .cpp of the class i need to test or the .h ? Or both maybe ?
This i the code of my test class:
Code:
#include <QString>
#include <QtTest>
#include "carre.h"
#include "cercle.h"
#include "point.h"
#include "rectangle.h"
#include "segment.h"
#include "figure.h"
#include "position.h"
class TestsProjetTest
: public QObject{
Q_OBJECT
public:
TestsProjetTest();
Carre* c;
Position* p;
private Q_SLOTS:
void testDeplacement();
};
TestsProjetTest::TestsProjetTest()
{
c = new Carre();
p = new Position(10, 10);
}
void TestsProjetTest::testDeplacement()
{
QVERIFY2(true, "Failure");
}
QTEST_APPLESS_MAIN(TestsProjetTest)
#include "tst_testsprojettest.moc"
It's really a simple code which actually don't test anything, but i've some issues i didn't understand what can I do... There are the issues:
Code:
error: undefined reference to `Carre::Carre()'
error: undefined reference to `Position::Position(int, int)'
:-1: error: collect2: error: ld returned 1 exit status
It's really strange because methods work pretty well in my project and don't have any problems... I put NameOfMyClass:: for each method on my project, and it work ! So i don't understand why i have problems like that ...
I have there problems for one week and i didn't find a solution ... I really need help :(
And please, dont redirect me on the ToUpper() tuto you find on Qt doc because i didn't help me at all ! :(
Same thing for the tuto on developpez.net :(
I hope you will find the problem and explain to me how to use Qt test framework ! I really need to improve my skill on it, because Qt will be my professionnal IDE, so i need to learn many things !
Thanks by advance !
Axel.
Re: Issues on Qt test framework
Quote:
Originally Posted by
Noren
-> do I made a .cpp and a .h for my test class ? Or putting all on the .cpp ?
Both work, for simple tests just a .cpp is usually less work.
In case of just having a .cpp you need to include the "moc" file at the end of the file (see your current example).
Quote:
Originally Posted by
Noren
-> I my class need to be in antoher project ? Or in the same project ? If i need to make another project, how to link both ?
You can either build the unit (the class you test) as part of the test (add its .cpp to SOURCES and its .h to HEADERS) or you put them in a library that both your application and your tests uses in their respective .pro file's LIBS variable.
Quote:
Originally Posted by
Noren
-> do i need to importe .cpp of the class i need to test or the .h ? Or both maybe ?
including the .cpp is also possible, but very uncommon.
Quote:
Originally Posted by
Noren
It's really a simple code which actually don't test anything, but i've some issues i didn't understand what can I do... There are the issues:
Code:
error: undefined reference to `Carre::Carre()'
error: undefined reference to `Position::Position(int, int)'
:-1: error: collect2: error: ld returned 1 exit status
Linker error, saying that it can't find the code backing these functions.
See your second question.
Quote:
Originally Posted by
Noren
It's really strange because methods work pretty well in my project and don't have any problems... I put NameOfMyClass:: for each method on my project, and it work ! So i don't understand why i have problems like that ...
Your main project most likely builds the class as part of its SOURCES or you have it in a library that the main project's LIBS points to.
Cheers,
_