Qt Unit test in a project
-
@jsulm are you trying to say that I also need to add the calculator class into tests project? I already have the Calculator class implemented into src project.
-
@jsulm are you trying to say that I also need to add the calculator class into tests project? I already have the Calculator class implemented into src project.
@HemantSuryawanshi said in Qt Unit test in a project:
are you trying to say that I also need to add the calculator class into tests project?
No, you need to add a reference to that file to your test project, something like:
add_executable(${PROJECT_NAME} PATH_TO/calculator.cpp tst_unittest.cpp ... )
If the code you want to test compiles to a lib you could instead add a dependency to this lib.
-
@jsulm Its working now. Thanks for response
-
Hi, I also want to include Qt Quick Test in same project. I added required directives into project. but I have one problem that I already have the main definition and it will not take another main definition. Please tell me how cam I add
QUICK_TEST_MAIN(example)
this code into main function? @jsulm
-
Hi, I also want to include Qt Quick Test in same project. I added required directives into project. but I have one problem that I already have the main definition and it will not take another main definition. Please tell me how cam I add
QUICK_TEST_MAIN(example)
this code into main function? @jsulm
@HemantSuryawanshi said in Qt Unit test in a project:
I have one problem that I already have the main definition
Are you trying to test a cpp file containing main()?
Why?
There should not be anything to test. If there is something you want to test, then move that functionality in functions/methods outside of that source file. -
@HemantSuryawanshi said in Qt Unit test in a project:
I have one problem that I already have the main definition
Are you trying to test a cpp file containing main()?
Why?
There should not be anything to test. If there is something you want to test, then move that functionality in functions/methods outside of that source file.@jsulm No I want to test qml file which is present in my src project. And I want to integrate both cpp testing and qml testing into in single project which is tests. thats why I want to integrate that macro in main function. How can I do that, any idea?
-
@jsulm No I want to test qml file which is present in my src project. And I want to integrate both cpp testing and qml testing into in single project which is tests. thats why I want to integrate that macro in main function. How can I do that, any idea?
@HemantSuryawanshi said in Qt Unit test in a project:
How can I do that, any idea?
Make sure you have only one main() in your test project.
-
@HemantSuryawanshi said in Qt Unit test in a project:
How can I do that, any idea?
Make sure you have only one main() in your test project.
#include <QtTest> #include <QDebug> #include <tst_unittest.cpp> #include <tst_userdata.cpp> #include <QtQuickTest/quicktest.h> QUICK_TEST_MAIN(example) int main(int argc, char** argv) { int failedTests = 0; failedTests += QTest::qExec(new UnitTest, argc, argv); failedTests += QTest::qExec(new TestUserData, argc, argv); if(failedTests > 0){ qDebug() << "total number of failed tests: " << failedTests; }else{ qDebug() << "all tests passed :)"; } return failedTests; }
This is my main file. My project contains multiple unit tests including Qt test and Qt Quick test. In this I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro.
-
#include <QtTest> #include <QDebug> #include <tst_unittest.cpp> #include <tst_userdata.cpp> #include <QtQuickTest/quicktest.h> QUICK_TEST_MAIN(example) int main(int argc, char** argv) { int failedTests = 0; failedTests += QTest::qExec(new UnitTest, argc, argv); failedTests += QTest::qExec(new TestUserData, argc, argv); if(failedTests > 0){ qDebug() << "total number of failed tests: " << failedTests; }else{ qDebug() << "all tests passed :)"; } return failedTests; }
This is my main file. My project contains multiple unit tests including Qt test and Qt Quick test. In this I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro.
@HemantSuryawanshi said in Qt Unit test in a project:
I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro
Of course, you have two main() functions now.
I suggest you split your C++ and Qt Quick tests. -
@HemantSuryawanshi said in Qt Unit test in a project:
I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro
Of course, you have two main() functions now.
I suggest you split your C++ and Qt Quick tests.@jsulm how can I link the qml file to the Qt Quick test project. I tried with same way as I link cpp file earlier, but its not working.