How to structure QtTest project as qt's github shows and how to access tested classes
-
Hello,
I have been struggling to implement QtTest on an existing Qt Project.
I would like to test multiple classes. I made an auto-test project with multiple test_classes and then execute them, it works but the IDE Test Tool doesn't work at all. I can't select which tests I want to execute separately, on top of that if I select X files, it will execute all files X times (if I have 30 tests in 3 folders if I select 2 folders it will execute 60 tests).After many adventures, I found this topic saying that the correct method is to create one auto-test project for each class, and this is how it's done on qt's github. So I've tested this solution on a little sample, it worked well.
But the problem is that, I want to implement this solution on a bigger project with a lot of classes, meaning that I have folders with classes and folders in them.
How do I structure that ?Following qt's Github I have created a "sub-project" project which will contain all unit test projects.
I encountered another problem, which is that I can't add a folder in a "subproject" project so I have all my tests as like following
I tried to put the autotest projects in folders but it did not change anything.Subproject - Project - Charges - Charge.h - anotherFile.h - Structures - file.h - newFile.h - Test_Subproject - tst_charges_autoTestProject - tst_charge.cpp - tst_anotherFile_autoTestProject - tst_anotherFile.cpp - tst_structure_autoTestProject - tst_structure.cpp
What I want my file hierarchy to look like (same as on github)
- Test_Subproject - Charges - tst_charges_autoTestProject - tst_charge.cpp - tst_anothFile_autoTestProject - tst_anothFile.cpp - Structure - tst_structure_autoTestProject - tst_structure.cpp
But it raised another problem (again) How do I access the class I want to test from the tests classes ?
Here is how I had access to the tested classes when I tried with all test_class.cpp/h in the same auto-test projectSOURCES += \ *.cpp \ $$files(../cpp/*.cpp, true) \ HEADERS += \ *.h \ $$files(../cpp/*.h, true) \ INCLUDEPATH += \ $$PWD \ ../cpp/
Accessing "charge.h" from "test_charge.h" for example
#include <../cpp/Charges/charge.h>
I tried something similar on my current project but it does not seem to work.
To sum up I have 2 two questions- How do I get a "good looking" hierarchy like mentionned above. Looking similar to the project src hierarchy.
- How do I access the class I want to test from the tests classes ?
I hope it is not too confusing if you need more information tell me. I will update this topic if I find something new.Important note : Unlike qt's github, I am not using cmake but qmake (with mingw)
-
Hi,
If you want to see the test structure of Qt using qmake, switch to the Qt 5.15 branch.
If you want to follow Qt's structure (which is a good idea), have a test folder at the root of your project and then you can define the same structure as your main project.
What would be done then is that your project code structure would be:
- 1 subproject for your application
- 1 or more subprojects that would be librairies that provide all the bits and pieces to create your application (GUI, business logic, etc)
You application project would then essentially be a main.cpp file and link to the librairies.
Then in your test you just have to point your test to include folders of the library they will be testing and link to said library.
Doing it that way allows you to do unit testing, integration testing and application testing.
-
You're welcome !
Since that answers your question please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer that you deem correct so other forum users may know a solution has been found :-)