Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

cpputest issue with QTest to show the widget on embedded linux



  • Hi,
    When unit test binary(using cpputest to run the testcases + QTest for the specific Qt functionality)is executed on embedded target(linux), it doesnot executes the testcases with widget->show() and cause segfault when unit test binary is run with -qws:

    Main to run qt unit tests looks like below:
    int main(int argc, char *argv[])
    {
    qputenv("QT_LOGGING_TO_CONSOLE","1");
    QApplication app( argc, argv );
    CommandLineTestRunner::RunAllTests(argc, argv);
    return 0;
    }

    Any suggestions to make widget->show() or widget->setvisible(true) work while running unit test binary on embedded linux?

    Based on https://doc.qt.io/archives/qt-4.8/qt-embedded-running.html, it looks fine to run with -qws but whenever widget is being shown with show() or setVisible(true), it throws seg fault with cpputest version 3.7.1. However, would like to know if we need to add any display option to show the widget.


  • Lifetime Qt Champion

    Hi,

    So the tests run properly on your machine with a standard build of Qt ?

    Can you show the stack trace you get ?



  • @SGaist Yes the tests run properly with standard Qt where event loop is being run and on ubuntu as well.
    In the unit test binary, there is no event loop being run unlike normal Qt app. Shall add the stack trace of the seg fault thrown if widget->show() or widget->isVisible() being invoked.


  • Lifetime Qt Champion

    Something is not clear. You say that the test is running on your desktop machine but the same test fails on the target ?



  • @SGaist said in cpputest issue with QTest to show the widget on embedded linux:

    Something is not clear. You say that the test is running on your desktop machine but the same test fails on the target ?

    Yes the same test fails on target which works on ubuntu desktop. Only difference is that we run the same test on target with -qws, though for the unit tests, event loop is not being run both on ubuntu and target..

    I noticed in Qt 4.8, we cannot start event loop with QSignalSpy unlike in Qt 5.0 https://doc.qt.io/qt-5/qsignalspy.html.
    It got to do with event loop being run until signal is received,right?

    Basically the test is to check resizeEvent() being invoked after calling widget->show() followed by widget->resize(100,100).
    Only when the widget is visible, the resize() calls will cause the resizeEvent() handler to be called.

    Can I run event loop in ui unit test main() or per only one test to determine if this is the reason on target?

    int main(int argc, char *argv[])
    {
    qputenv("QT_LOGGING_TO_CONSOLE","1");
    QApplication app( argc, argv );
    //app.exec();
    CommandLineTestRunner::RunAllTests(argc, argv);
    return 0;
    }



  • Here is the stack trace but it doesnot points to any Qt sources but only cpputest:

    #0 0x002ad996 in isInPeriod (period=mem_leak_period_checking, node=0x10, this=<optimized out>)
    at ../src/CppUTest/MemoryLeakDetector.cpp:270
    #1 MemoryLeakDetectorList::isInPeriod (this=0xe658ac, node=0x10, period=mem_leak_period_checking)
    at ../src/CppUTest/MemoryLeakDetector.cpp:268
    #2 0x002ada86 in MemoryLeakDetectorList::getTotalLeaks (this=this@entry=0xe658ac,
    period=period@entry=mem_leak_period_checking) at ../src/CppUTest/MemoryLeakDetector.cpp:354
    #3 0x002adbae in MemoryLeakDetectorTable::getTotalLeaks (this=0xe65824, period=mem_leak_period_checking)
    at ../src/CppUTest/MemoryLeakDetector.cpp:391
    #4 0x002a7e78 in MemoryLeakWarningPlugin::postTestAction (this=0xbeebfba0, test=..., result=...)
    at ../src/CppUTest/MemoryLeakWarningPlugin.cpp:551
    #5 0x002aadb0 in runAllPostTestAction (result=..., test=..., this=0xbeebfba0)
    at ../src/CppUTest/TestPlugin.cpp:60
    #6 TestPlugin::runAllPostTestAction (this=0xbeebfba0, test=..., result=...)
    at ../src/CppUTest/TestPlugin.cpp:57
    #7 0x002aada0 in TestPlugin::runAllPostTestAction (this=0xbeebfb58, test=..., result=...)
    at ../src/CppUTest/TestPlugin.cpp:59
    #8 0x002ac664 in UtestShell::runOneTestInCurrentProcess (
    this=0x63ab8c <TEST_uiNormalTestGroup_resizeEvent_NormalTest_TestShell_instance>,
    plugin=0xbeebfb58, result=...) at ../src/CppUTest/Utest.cpp:216
    #9 0x002ab65a in helperDoRunOneTestInCurrentProcess (data=<optimized out>) at ../src/CppUTest/Utest.cpp:116
    #10 0x002ac90c in PlatformSpecificSetJmpImplementation (
    function=0x2ab651 <helperDoRunOneTestInCurrentProcess(void*)>, data=0xbeebfacc)
    at ../src/Platforms/Gcc/UtestPlatform.cpp:144
    #11 0x002ab6b4 in UtestShell::runOneTest (
    this=0x63ab8c <TEST_uiSystemNormalTestGroup_resizeEvent_SystemNormalTest_TestShell_instance>,
    plugin=<optimized out>, result=...) at ../src/CppUTest/Utest.cpp:185
    #12 0x002ab4b6 in TestRegistry::runAllTests (this=0x6bd5bc TestRegistry::getCurrentRegistry()::registry,
    result=...) at ../src/CppUTest/TestRegistry.cpp:62
    #13 0x002a6668 in CommandLineTestRunner::runAllTests (this=this@entry=0xbeebfb90)
    at ../src/CppUTest/CommandLineTestRunner.cpp:119
    #14 0x002a66ee in CommandLineTestRunner::runAllTestsMain (this=this@entry=0xbeebfb90)
    at ../src/CppUTest/CommandLineTestRunner.cpp:80
    #15 0x002a6794 in CommandLineTestRunner::RunAllTests (ac=4, av=0xbeebfd44)
    at ../src/CppUTest/CommandLineTestRunner.cpp:50
    #16 0x000b243c in main (argc=4, argv=0xbeebfd44) at src/uiUnitTest.cpp:59


  • Lifetime Qt Champion

    Might be a silly question but are you sure that -qws parameter is passed to your QApplication instance ?

    Depending on what you want to test, you might also want to consider Qt's own QTest framework.



  • @SGaist Yes running the unit test binary with -qws..As we need to do mock c libraries as well, ui unit tests are run within CPPUTEST framework and use QTEST library to execute Qt specific functionality like QSignalSpy. Any suggestion/hint if it could throw any memory leak incase widget->show() is being called on target with -qws or any known issue in Qt bug tracker in Qt 4.8?


  • Lifetime Qt Champion

    Did you check whether cpputest has a runner for Qt ?

    In any case, if you have so much trouble running Qt tests, the I would recommand using the QtTest framework as it will handle all Qt related features to test your GUI.



  • @SGaist
    Do you mean Qt testrunner in https://github.com/autosportlabs/CppUTest/blob/master/examples/qt/Main.cpp?
    As we need cpputest to test mock c libraries within UI, we donot have flexibility to use only QTest as it doesnot supports mocking. Is it possible to switch between cpputest and qtest runs as and when required?

    Apart from running app.exec() in the below, are there any other means to make event loop available or run per test as mentioned in http://blog.davidecoppola.com/2017/11/cpp-unit-testing-with-qt-test-introduction/?

    int main(int argc, char *argv[])
    {
    qputenv("QT_LOGGING_TO_CONSOLE","1");
    QApplication app( argc, argv );
    CommandLineTestRunner::RunAllTests(argc, argv);
    return 0;
    }


  • Lifetime Qt Champion

    Yes, that's the kind of runner I am thinking about. Did you already test it ?



  • @SGaist Nope as the runner shall not support mocking of c library functions which are required to unit test qt code.


  • Lifetime Qt Champion

    What do you mean by shall not support mocking ?



  • @SGaist Sorry for the delayed response. I meant no in-built support for mocks creation using QTest Unlike Google mock or Cpputest support which simplifies the way mocks are created so they can be manipulated for each testcase.

    As we need to unit test the connect statements/event filter/event callbacks (showEvent,HideEvent,resizeEvent,changeEvent etc) , could you please suggest on how to test in existing cpputest runner using QTest library? Does adding app.exec() in the below would help or any means using QSignalSpy?

    int main(int argc, char *argv[])
    {
    qputenv("QT_LOGGING_TO_CONSOLE","1");
    QApplication app( argc, argv );
    int returnVal = app.exec();
    CommandLineTestRunner::RunAllTests(argc, argv);
    return returnVal ;
    }


  • Lifetime Qt Champion

    No because exec is a blocking call.

    One thing I would try is to use a single shot QTimer with a lambda to call RunAllTests.


Log in to reply