c++ - 使用 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-questions和here 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.
这里出了什么问题?
解决方案
推荐阅读
- go - protoc 在 go 源文件上生成导入路径
- java - 缺少使用 org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder 的方法
- javascript - 将 HTML 附加到组件和 React 组件类意外令牌,预期 }
- python - 如何将图像转换为黑白?(使用 scipy 或 matplotlib)
- swift - 架构 arm64 的未定义符号:“_OBJC_CLASS_$_GADMobileAds”
- javascript - 刷新/重新加载页面后保持复选框选中的另一件事
- python - 使用 Python 访问嵌套 JSON [AWS 元数据]
- python - 如何在python文件(.py)中访问c++头文件
- python - django-tables2 如何为 _set 设置详细名称
- jquery - 为什么内联