我第一次尝试在 Qt应用程序上学习/使用 Catch ( https://github.com/catchorg/Catch2 )。
我正在尝试遵循 Catch 初始页面 ( https://github.com/catchorg/Catch2/blob/devel/docs/tutorial.md#top ) 上提供的教程。
上述教程的第一行说,理想情况下,我应该通过其“CMake 集成”(https://github.com/catchorg/Catch2/blob/devel/docs/cmake-integration.md#top)来使用 Catch2。我忠实地遵循“理想”的道路。
在“CMake 集成”页面的第二段中,我开始迷失:如果您不需要自定义主功能,您应该...
我需要自定义主函数吗?为什么有人需要一个?一个人没有一个人怎么活?我完全不知道,文本既没有解释这些,也没有提供任何合理的默认方向(如果你不知道我们在说什么,就假装你......或类似的东西)。
我试图忽略这一点,然后继续。
在第三段(根据请求在下面复制)呈现了一个代码块,读者会知道它应该足以完成代码块。代码块是做什么的?我应该将此代码包含在一些预先存在的文件中吗?哪个文件?在所述文件的哪一部分?或者我应该使用建议的内容创建一个新文件?哪个文件?我应该把它放在哪里?
这意味着如果系统上已经安装了 Catch2,应该足够了
> find_package(Catch2 3 REQUIRED)
> # These tests can use the Catch2-provided main add_executable(tests test.cpp) target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
>
> # These tests need their own main add_executable(custom-main-tests test.cpp test-main.cpp) target_link_libraries(custom-main-tests
> PRIVATE Catch2::Catch2)
有人可以提供一个在 Qt 项目中简单使用 Catch2 的工作示例吗?最好是桌面应用程序?
2022 年 1 月 14 日更新:
这是我对尝试实现最小的 Qt + Catch2 集成的看法,类似于 Catch 教程中的第一个示例(https://github.com/catchorg/Catch2/blob/v2.x/docs/tutorial.md#writing-tests)。
我创建了一个名为 QtCatch 的 Qt Widget 应用程序。这是它的文件结构:
.
├── CMakeLists.txt
├── include
│ ├── calculator.cpp
│ └── calculator.h
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── mainwindow.ui
└── tests
├── CMakeLists.txt
├── main.cpp
└── tst_qtcatchtest.cpp
我在下面包含了所有文件内容以供参考。
该文件结构是通过 Qt“新建项目”对话框创建的。主项目是“Application (Qt) > Qt Widgets Application”,测试子项目是“Other Project >> Auto Test Project”
我的 Qt 应用程序运行没有问题。
如果我尝试编译测试子项目或主项目取消注释主 CMakeLists.txt 文件中的“add_subdirectory(tests)”行,我会收到相同的错误:
对 Calculator::Calculator() 的未定义引用
尽管
#include "../include/calculator.h"
tst_qtcatchtest.cpp中的行
如何使这个简单的 Catch2 测试用例在 Qt 6 中工作?
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(QtCatch VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
# Manually added
#add_subdirectory(tests)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
include/calculator.h include/calculator.cpp
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(QtCatch
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET QtCatch APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(QtCatch SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(QtCatch
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(QtCatch PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
set_target_properties(QtCatch PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(QtCatch)
endif()
主.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_factorialPushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
主窗口.cpp:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "include/calculator.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_factorialPushButton_clicked()
{
Calculator aCalc;
int factorial = aCalc.Factorial(ui->numberLineEdit->text().toInt());
QString result = QString("Result: %1").arg(factorial);
ui->resultLabel->setText(result);
}
主窗口.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>214</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="numberLabel">
<property name="text">
<string>Number</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="numberLineEdit"/>
</item>
<item>
<widget class="QPushButton" name="factorialPushButton">
<property name="text">
<string>Calculate Factorial</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="resultLabel">
<property name="text">
<string>Result</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
包括/计算器.h:
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator
{
public:
Calculator();
int Factorial( int number );
};
#endif // CALCULATOR_H
包括/calculator.cpp:
#include "calculator.h"
Calculator::Calculator()
{
}
int Calculator::Factorial( int number )
{
return number <= 1 ? 1 : Factorial( number - 1 ) * number;
}
测试/CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(QtCatch VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
# Manually added
add_subdirectory(tests)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
include/calculator.h include/calculator.cpp
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(QtCatch
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET QtCatch APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(QtCatch SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(QtCatch
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(QtCatch PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
set_target_properties(QtCatch PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(QtCatch)
endif()
测试/main.cpp:
#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp>
#include <QtGui/QGuiApplication>
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
return Catch::Session().run(argc, argv);
}
测试/tst_qtcatchtest.cpp:
#include <catch2/catch.hpp>
#include "../include/calculator.h"
TEST_CASE( "Factorial of 0 is 1 (fail)", "[qt]" ) {
Calculator aCalc;
REQUIRE( aCalc.Factorial(0) == 1 );
}
TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[qt]" ) {
Calculator aCalc;
REQUIRE( aCalc.Factorial(1) == 1 );
REQUIRE( aCalc.Factorial(2) == 2 );
REQUIRE( aCalc.Factorial(3) == 6 );
REQUIRE( aCalc.Factorial(10) == 3628800 );
}