3장: GUI 이벤트 시뮬레이션

Qt Test 에는 그래픽 사용자 인터페이스를 테스트하는 몇 가지 메커니즘이 있습니다. Qt Test 은 네이티브 윈도우 시스템 이벤트를 시뮬레이션하는 대신 내부 Qt 이벤트를 전송합니다. 즉, 테스트가 실행되는 머신에 부작용이 없습니다.

이 장에서는 간단한 GUI 테스트를 작성하는 방법을 보여줍니다.

GUI 테스트 작성하기

이번에는 QLineEdit 클래스의 동작을 테스트하고 싶다고 가정해 보겠습니다. 이전과 마찬가지로 테스트 함수가 포함된 클래스가 필요합니다:

#include <QtWidgets>
#include <QTest>

class TestGui: public QObject
{
    Q_OBJECT

private slots:
    void testGui();

};

유일한 차이점은 QTest 네임스페이스 외에 Qt GUI 클래스 정의를 포함해야 한다는 것입니다.

void TestGui::testGui()
{
    QLineEdit lineEdit;

    QTest::keyClicks(&lineEdit, "hello world");

    QCOMPARE(lineEdit.text(), QString("hello world"));
}

테스트 함수를 구현할 때는 먼저 QLineEdit 를 만든 다음 QTest::keyClicks() 함수를 사용하여 줄 편집에서 "hello world"를 작성하는 시뮬레이션을 합니다.

참고: 키보드 단축키를 올바르게 테스트하려면 위젯도 표시되어야 합니다.

QTest::keyClicks() 함수는 위젯에서 일련의 키를 클릭하는 것을 시뮬레이션합니다. 선택적으로 키보드 수정자를 지정하고 각 키 클릭 후 테스트의 지연 시간(밀리초)을 지정할 수 있습니다. 비슷한 방식으로 QTest::keyClick(), QTest::keyPress(), QTest::keyRelease(), QTest::mouseClick(), QTest::mouseDClick(), QTest::mouseMove(), QTest::mousePress() 및 QTest::mouseRelease() 함수를 사용하여 관련 GUI 이벤트를 시뮬레이션할 수 있습니다.

마지막으로 QCOMPARE() 매크로를 사용하여 줄 편집의 텍스트가 예상대로 표시되는지 확인합니다.

독립 실행형 실행 파일 준비하기

이전과 마찬가지로 테스트 케이스를 독립 실행형 실행 파일로 만들려면 다음 두 줄이 필요합니다:

QTEST_MAIN(TestGui)
#include "testgui.moc"

QTEST_MAIN() 매크로는 모든 테스트 함수를 실행하는 간단한 main() 메서드로 확장되며, 테스트 클래스의 선언과 구현이 모두 .cpp 파일에 있으므로 생성된 moc 파일도 포함해야 Qt의 인트로스펙션이 작동할 수 있습니다.

실행 파일 빌드하기

CMake 또는 qmake를 사용하여 테스트 케이스 실행 파일을 빌드할 수 있습니다.

CMake로 빌드하기

CMakeLists.txt 파일에서 빌드 설정을 구성합니다:

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

cmake_minimum_required(VERSION 3.16)
project(tutorial3 LANGUAGES CXX)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)

qt_standard_project_setup()

qt_add_executable(tutorial3
    testgui.cpp
)

set_target_properties(tutorial3 PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)

target_link_libraries(tutorial3 PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Test
    Qt6::Widgets
)

install(TARGETS tutorial3
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET tutorial3
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})

그런 다음 명령줄에서 cmake 를 실행하거나 Qt-prefix/<version>/<platform>/bin/qt-cmake 에 있는 qt-cmake 편의 스크립트를 사용합니다:

<Qt-prefix>/<version>/<platform>/bin/qt-cmake <source-dir> <build-dir> -G Ninja

그런 다음 선호하는 생성기 도구를 실행하여 실행 파일을 빌드합니다. 여기서는 Ninja를 사용하고 있습니다:

ninja

qmake로 빌드하기

.pro 파일에서 빌드 설정을 구성합니다:

QT += widgets testlib

SOURCES = testgui.cpp

# install
target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial3
INSTALLS += target

그런 다음 qmake 을 실행하고 마지막으로 make 을 실행하여 실행 파일을 빌드합니다:

qmake
make

실행 파일 실행하기

결과 실행 파일을 실행하면 다음과 같은 출력이 표시됩니다:

********* Start testing of TestGui *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS   : TestGui::initTestCase()
PASS   : TestGui::testGui()
PASS   : TestGui::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 20ms
********* Finished testing of TestGui **

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.