首页 > 解决方案 > 使用 Cmake 和 Ctest 构建 Qt-test

问题描述

我最近开始探索使用 QT 库进行单元测试,所以我只想重复其文档中的简单代码并通过 Cmake 构建它(我有 msvc 编译器并使用 CLion IDE)。

制作:

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"

然后,我运行 All CTest 并且此测试失败并出现错误:

"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

由于某种原因,该测试不适用于夹具要求。我查看了其他一些Qt-questionshere about structure of unit-tests,但没有看到我的代码有很多差异。这里是 Cmake 选项。将 Qt 目录添加到 Path 环境变量后,出现链接错误:

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.

这里出了什么问题?

标签: c++qtunit-testingcmakectest

解决方案


推荐阅读