当前位置: 首页 > article >正文

f QT测试

# 添加 Qt Test 模块,用于支持单元测试功能
QT += testlib

# 添加 Qt 的核心模块和 GUI 模块,这是构建 Qt 应用程序的基础模块
QT       += core gui

# 如果 Qt 的主版本号大于 4,则添加 widgets 模块。
# 这是因为 Qt Widgets 模块是从 Qt 5 开始引入的,用于构建基于 QWidget 的传统桌面应用程序。
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

# 配置项目属性:
# - qt:表示这是一个基于 Qt 的项目。
# - console:启用控制台支持,允许程序在控制台中输出信息。
# - warn_on:启用编译器的所有警告信息,有助于发现潜在问题。
# - depend_includepath:启用依赖项的头文件路径。
# - testcase:启用测试用例的配置,与 testlib 模块配合使用。
CONFIG += qt console warn_on depend_includepath testcase

# 移除 app_bundle 配置选项。
# app_bundle 主要用于 macOS,表示将应用程序打包为一个应用程序包(.app)。
# 移除该选项可能是因为项目不需要以应用程序包的形式构建,或者是为了兼容其他平台。
CONFIG -= app_bundle

# 定义项目模板类型为 "app",表示这是一个应用程序项目。
# 其他模板类型包括 "lib"(库)、"subdirs"(子目录项目)等。
TEMPLATE = app

# 列出项目中需要编译的源文件(.cpp):
# - tst_qbtntest.cpp:可能是与测试相关的源文件。
# - mainwindow.cpp:与主窗口相关的源文件。
SOURCES +=  tst_qbtntest.cpp \
    mainwindow.cpp

# 列出项目中需要包含的头文件(.h):
# - mainwindow.h:与主窗口相关的头文件。
HEADERS += \
    mainwindow.h

# 列出项目中需要处理的 UI 文件(.ui):
# - mainwindow.ui:使用 Qt Designer 设计的主窗口的 UI 文件。
# Qt 的 uic 工具会将 .ui 文件转换为 C++ 代码,生成对应的头文件(如 ui_mainwindow.h)。
FORMS += \
    mainwindow.ui
//mainwindow.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_PushButton_clicked();


private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_PushButton_clicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_PushButton_clicked()
{
    qDebug() << "Button clicked!";
}
#include <QtTest>
#include "mainwindow.h"
#include <QPushButton>

// 单元测试类 qbtntest
class qbtntest : public QObject
{
    Q_OBJECT

public:
    qbtntest();  // 构造函数
    ~qbtntest(); // 析构函数

private slots:
    void test_case1();  // 测试用例

private:
    MainWindow* mainWindow;  // MainWindow 的实例
};

// qbtntest 构造函数
qbtntest::qbtntest()
{
    mainWindow = new MainWindow();  // 创建 MainWindow 实例
}

// qbtntest 析构函数
qbtntest::~qbtntest()
{
    delete mainWindow;  // 释放 MainWindow 实例
}

// 测试用例:测试按钮点击事件
void qbtntest::test_case1()
{
    // 从 MainWindow 中查找名为 "pushButton" 的 QPushButton
    QPushButton* button = mainWindow->findChild<QPushButton*>("pushButton");
    QVERIFY(button != nullptr);  // 断言:确保按钮存在

    // 使用 QSignalSpy 捕获按钮的 clicked 信号
    QSignalSpy spy(button, &QPushButton::clicked);

    // 模拟鼠标点击按钮
    QTest::mouseClick(button, Qt::LeftButton);

    // 验证信号是否被触发
    QCOMPARE(spy.count(), 1);  // 断言:信号被触发一次
}

// 定义测试主函数
QTEST_MAIN(qbtntest)

// 包含 MOC(元对象系统)生成的代码
#include "tst_qbtntest.moc"
<?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>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>350</x>
      <y>170</y>
      <width>112</width>
      <height>34</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

全部工程文件放在一起1.5h


http://www.kler.cn/a/582193.html

相关文章:

  • 微服务的认识与拆分
  • Ubuntu用户安装cpolar内网穿透
  • 实战:DHCP服务器配置与防御欺骗攻击(附华为设备命令)
  • react基础语法视图层类组件
  • 如何精准打点解决卡牌、SLG、开放大世界、放置类游戏卡顿难题
  • 洗鞋小程序(源码+文档+讲解+演示)
  • 基于SpringBoot的美食信息推荐系统设计与实现(源码+SQL脚本+LW+部署讲解等)
  • C++设计模式总结
  • Java中的四种排序算法详解
  • 【JavaWeb学习Day24】
  • 【langchain/入门】使用langchain调用本地部署的大模型(以llama.cpp以及ollama为例)
  • excel的导入和下载(poi)
  • 强化科技内核 “人工智能+”助力农业新质生产力飞跃
  • Hexo博客Icarus主题不蒜子 UV、PV 统计数据初始化配置
  • LVTTL(Low Voltage Transistor-Transistor Logic)电平详解
  • K8S中MySQL配置持久化存储
  • Go 语言入门指南
  • OPPO手机怎么录制屏幕?屏幕录制软件分享
  • comctl32!ListView_OnSetItem函数分析LISTSUBITEM结构中的image表示图标位置
  • 开源模型时代的 AI 开发革命:Dify 技术深度解析