이 페이지에서

제1장: 단위 테스트 작성하기

이 첫 번째 장에서는 간단한 단위 테스트를 작성하는 방법과 테스트 케이스를 독립 실행형 프로그램으로 실행하는 방법을 설명합니다.

테스트 작성하기

QString 클래스의 동작을 테스트한다고 가정해 봅시다. 먼저, 테스트 함수를 포함하는 클래스가 필요합니다. 이 클래스는 QObject 를 상속받아야 합니다:

#include <QTest>

class TestQString: public QObject
{
    Q_OBJECT
private slots:
    void toUpper();
};

참고: 테스트 프레임워크가 테스트 함수를 찾아 실행할 수 있도록 ` QTest ` 헤더를 포함하고 테스트 함수를 private 슬롯으로 선언해야합니다 .

그런 다음 테스트 함수 자체를 구현해야 합니다. 구현은 다음과 같을 수 있습니다:

void TestQString::toUpper()
{
    QString str = "Hello";
    QVERIFY(str.toUpper() == "HELLO");
}

QVERIFY() 매크로는 인수로 전달된 식을 평가합니다. 식의 평가 결과가 true인 경우, 테스트 함수의 실행이 계속됩니다. 그렇지 않은 경우, 실패를 설명하는 메시지가 테스트 로그에 추가되고 테스트 함수의 실행이 중단됩니다.

하지만 테스트 로그에 더 자세한 출력을 원한다면, 대신 QCOMPARE() 매크로를 사용해야 합니다:

void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}

두 문자열이 같지 않은 경우, 두 문자열의 내용이 모두 테스트 로그에 추가되므로 비교가 실패한 이유를 즉시 파악할 수 있습니다.

독립 실행 파일 준비

마지막으로, 테스트 케이스를 독립 실행형으로 만들려면 다음 두 줄이 필요합니다:

QTEST_MAIN(TestQString)
#include "testqstring.moc"

QTEST_MAIN() 매크로는 모든 테스트 함수를 실행하는 간단한 ` main() ` 메서드로 확장됩니다. 테스트 클래스의 선언과 구현이 모두 ` .cpp ` 파일에 있는 경우, Qt의 인트로스펙션이 작동하도록 생성된 moc 파일도 포함해야 한다는 점에 유의하십시오.

실행 파일 빌드

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(tutorial1 LANGUAGES CXX)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)

qt_standard_project_setup()

qt_add_executable(tutorial1
    testqstring.cpp
)

set_target_properties(tutorial1 PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)

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

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

qt_generate_deploy_app_script(
    TARGET tutorial1
    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 = testqstring.cpp

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

다음으로 qmake 를 실행하고, 마지막으로 make 를 실행하여 실행 파일을 빌드하십시오:

qmake
make

참고: Windows를 사용하는경우 , make 을 nmake 또는 사용 중인 빌드 도구로 대체하십시오.

실행 파일 실행

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

********* Start testing of TestQString *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS   : TestQString::initTestCase()
PASS   : TestQString::toUpper()
PASS   : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********

축하합니다! 방금 Qt Test 프레임워크를 사용하여 첫 번째 단위 테스트를 작성하고 실행했습니다.

© 2026 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.