Solved QTest::fetchData(): "Test data requested, but no testdata available."
-
Hi
I'm using Qt 5.7.1, on Ubuntu
I was following this tutorial : http://doc.qt.io/qt-5/qttestlib-tutorial2-example.html
But i block on this problem, all my google skills haven't be able to find a solution in forums.This is my code.
#include <QtTest> class TestQString: public QObject { Q_OBJECT private slots: void toUpper(); void toUpper_data(); }; void TestQString::toUpper_data() { QTest::addColumn<QString>("input"); QTest::addColumn<QString>("output"); QTest::newRow("all lower") << "hello" << "HELLO"; QTest::newRow("mixed") << "Hello" << "HELLO"; QTest::newRow("all upper") << "HELLO" << "HELLO"; } void TestQString::toUpper() { QFETCH(QString, input); QFETCH(QString, output); QCOMPARE(input.toUpper(), output); } QTEST_MAIN(TestQString) #include "moc_TestQString.cpp"
I get this error:
********* Start testing of TestQString ********* Config: Using QtTest library 5.7.1, Qt 5.7.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 6.3.0 20170124) PASS : TestQString::initTestCase() QFATAL : TestQString::toUpper() ASSERT failure in QTest::fetchData(): "Test data requested, but no testdata available.", file qtestcase.cpp, line 1018 FAIL! : TestQString::toUpper() Received a fatal error. Loc: [Unknown file(0)] Totals: 1 passed, 1 failed, 0 skipped, 0 blacklisted, 0ms ********* Finished testing of TestQString ********* Aborted (core dumped)
By break stepping into the Debug execution, i found out that toUpper_data() was never called. That's why there is no "testdata available" since a data table was never created.
Why does the toUpper_data() was not called by the Main generated by the Macro : QTEST_MAIN ?
Thank you.
-
Hi and welcome to devnet,
Are you using qmake to manage your project ?
I've just tested it on macOS and it's working fine. Just had to change the name of the included .moc file in order to build this properly though.
-
Hi,
I'm not using qmake but cmake.
Here is a short copy of the CMakeLists.txtcmake_minimum_required(VERSION 3.7.2) project(ocs-gui) set(CMAKE_INCLUDE_CURREND_DIR on) set(CMAKE_BUILD_TYPE Debug) find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui Test) if(Qt5_FOUND) message("Qt5 found") endif() set(test_files test/TestQString.cpp ) set(headers_test_files test/TestQString.h ) qt5_wrap_cpp(header_test_files_moc ${headers_test_files}) add_definitions(${QT_DEFINITIONS}) include_directories(${CMAKE_CURRENT_BINARY_DIR} include) add_executable(ocs-gui-test ${source_files} ${header_files_moc} ${form_headers_files} ${test_files} ${test_files_headers}) target_compile_features(ocs-gui-test PRIVATE cxx_range_for) link_directories(test) target_link_libraries(ocs-gui-test Qt5::Widgets Qt5::Test)
Could you copy the content of your .pro ?
-
QT += widgets testlib SOURCES = testqstring.cpp
And I've changed the last line for
#include "testqstring.moc"
-
Alright, problems came from my CMakeLists.txt.
Thank for helping me out. With your post, i've stoped looking into the Qt Version or into the Qt code to see if something when wrong.My guess is that the qt5_wrap_cpp() wasn't generating good moc files since the Q_OBJECT was declared inside the cpp.
Here is a good CMakeLists.txt
cmake_minimum_required(VERSION 3.7.2) project(test-app) set(CMAKE_INCLUDE_CURREND_DIR on) set(CMAKE_BUILD_TYPE Debug) find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui Test) if(Qt5_FOUND) message("Qt5 found") endif() set(test_files test/TestQString.cpp ) set(headers_test_files test/TestQString.h ) qt5_wrap_cpp(header_test_files_moc ${headers_test_files}) add_definitions(${QT_DEFINITIONS}) include_directories(${CMAKE_CURRENT_BINARY_DIR} include) add_executable(test-app ${test_files} ${test_files_headers}) target_compile_features(test-app PRIVATE cxx_range_for) link_directories(test) target_link_libraries(test-app Qt5::Widgets Qt5::Test)
Thank you !
LAL -
I'm using qmake, I got this error too. After I deleted the build directory and run qmake again, it worked.
-
For this i have the same error getting. please check it where should i wrong?
testcalculator cpp file
#include <QtTest>
#include "Maths.hpp"
class TestCalculator : public QObject
{
Q_OBJECTprivate slots:
void test_case1();
void testNumbers();};
void TestCalculator::test_case1()
{
QTest::addColumn<int>("a");
QTest::addColumn<int>("b");
QTest::addColumn<int>("expectedAddition");
QTest::addColumn<int>("expectedSubstraction");
QTest::addColumn<int>("expectedMultiplication");QTest::newRow("all zero") << 0 << 0 << 0 << 0 << 0 ; QTest::newRow("same number") << 10 << 10 << 20 << 0 << 100 ; QTest::newRow("different number") << 10 << 5 << 15 << 5 << 50 ; QTest::newRow("1 +ve, 1 -ve number") << 10 << -5 << 5 << 15 << -50 ; QTest::newRow("-ve numbers") << -5 << -10 << -15 << 5 << 50;
}
void TestCalculator::testNumbers()
{
QFETCH(int, a);
QFETCH(int, b);
QFETCH(int, expectedAddition);
QFETCH(int, expectedSubstraction);
QFETCH(int, expectedMultiplication);Maths objNumber; int addResult=objNumber.add(a,b); qDebug()<< a; qDebug()<< b; qDebug()<< expectedAddition; qDebug()<< addResult; QCOMPARE(addResult, expectedAddition); int diffResult=objNumber.subtract(a,b); qDebug()<< a; qDebug()<< b; qDebug()<< expectedSubstraction; qDebug()<< diffResult; QCOMPARE(diffResult, expectedSubstraction); int mulResult=objNumber.multiplication(a,b); qDebug()<< a; qDebug()<< b; qDebug()<< expectedMultiplication; qDebug()<< mulResult; QCOMPARE(mulResult, expectedMultiplication);
}
QTEST_APPLESS_MAIN(TestCalculator)
#include "tst_testcalculator.moc"
********* Start testing of TestCalculator *********
Config: Using QtTest library 5.12.7, Qt 5.12.7 (x86_64-little_endian-llp64 shared (dynamic) debug build; by GCC 7.3.0)
PASS : TestCalculator::initTestCase()
PASS : TestCalculator::test_case1()
QFATAL : TestCalculator::testNumbers() ASSERT failure in QTest::fetchData(): "Test data requested, but no testdata available.", file qtestcase.cpp, line 1167
FAIL! : TestCalculator::testNumbers() Received a fatal error.
Unknown file(0) : failure location
Totals: 2 passed, 1 failed, 0 skipped, 0 blacklisted, 1ms
********* Finished testing of TestCalculator *********
00:05:38: The program has unexpectedly finished.
00:05:38: The process was ended forcefully.This is the testcalculator.pro file
QT += testlib
QT -= guiCONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundleTEMPLATE = app
SOURCES += tst_testcalculator.cpp
Maths.cppHEADERS +=
Maths.hppThank you,
Surendranath -
@Surendranath Ok i got the solution .
And where i went wrong was i gave the wrong test case definition.
So i correct it testNumbers_data() instead of test_case1().
Now problem solved.
Thank you.