1

I recently started exploring unit-testing with QT library, so I just want to repeat simple code from its documentation and build it by Cmake (I have msvc compiler and use CLion IDE).

Cmake:

cmake_minimum_required(VERSION 3.17)
project(unit_tests)
find_package(Qt5Test REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)

enable_testing(true)

add_executable(mytest tst_mytest.cpp)
add_test(mytest mytest)

target_link_libraries(mytest PRIVATE Qt5::Test)

tst_mytest.cpp:

#include <QObject>
#include <QtTest/QTest>
class MyFirstTest: public QObject
{
    Q_OBJECT

private:
    bool myCondition()
    {
        return true;
    }

private slots:
    void initTestCase()
    {
        qDebug("Called before everything else.");
    }

    void myFirstTest()
    {
        QVERIFY(true);
        QCOMPARE(1, 1);
    }

    void mySecondTest()
    {
        QVERIFY(myCondition());
        QVERIFY(1 != 2);
    }

    void cleanupTestCase()
    {
        qDebug("Called after myFirstTest and mySecondTest.");
    }
};
QTEST_MAIN(MyFirstTest)
#include "tst_mytest.moc"

Then, I run All CTest and this test fails with error:

"F:\CLion 2020.3.1\bin\cmake\win\bin\ctest.exe" --extra-verbose
Testing started at 16:07 ...
UpdateCTestConfiguration  from :C:/Users/nikolya/CLionProjects/untitled13/cmake-build-debug/DartConfiguration.tcl
UpdateCTestConfiguration  from :C:/Users/nikolya/CLionProjects/untitled13/cmake-build-debug/DartConfiguration.tcl
Test project C:/Users/nikolya/CLionProjects/untitled13/cmake-build-debug
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end

1: Test command: C:\Users\nikolya\CLionProjects\untitled13\cmake-build-debug\foo.exe
1: Test timeout computed to be: 10000000

Terminated with exit code: 0xc0000135

Exception:


Errors while running CTest
0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.02 sec

The following tests FAILED:
      1 - foo (Exit code 0xc0000135
)
Process finished with exit code 8

For some reason the test is not appropriate for fixture requirements. I had a look for some other Qt-questions and here about structure of unit-tests, but didn`t see a lot of differences with my code. Here are Cmake options. After adding Qt directory to Path environment variable there is a linking error:

CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.

What is going wrong here?

  • Exit code `0xc0000135` means DLL not found. You need to add Qt binary directory to the PATH environment variable. – vre May 05 '21 at 14:55
  • Could you tell me how to do this in CLion? – profi_dreamer May 05 '21 at 15:43
  • Does this help? https://stackoverflow.com/questions/56251662/with-clion-how-do-i-set-an-environment-variable-for-the-cmake-build/56251942 – vre May 05 '21 at 15:47
  • Definetely, so should i write path= in "environment" field? And one more question: where can i find qt_directory? – profi_dreamer May 05 '21 at 16:03
  • Assuming your Qt is in C:\Qt\5.15.2\msvc2019_64 installed add PATH=C:\Qt\5.15.2\msvc2019_64\bin to this field. Try to inspect CMakeCache.txt for the Qt installation directory. – vre May 05 '21 at 16:57
  • Do you build 32 or 64Bit? If you build for 32Bit the path is C:\Qt\5.15.2\msvc2019\bin – vre May 05 '21 at 18:16
  • all right, 64-bit – profi_dreamer May 05 '21 at 18:31
  • What happens if you clear the CMake cache and rebuild the project? How to do it in CLion see here: https://www.jetbrains.com/help/clion/cmake-cache.html – vre May 06 '21 at 07:01
  • I had updated the question, after this had a look for [chicken-and-egg problem](https://stackoverflow.com/questions/35869564/cmake-on-windows), but still don`t understand how to overrule the generator – profi_dreamer May 06 '21 at 12:23

0 Answers0