返回 Skill 列表
extension
分类: 开发与工程无需 API Key

qtest-patterns

当用户要求“编写一个测试”、“添加测试到”、“创建一个QTest”、“我如何测试一个小部件”、“Qt的单元测试”、“pytest-qt”、“测试一个PySide6类”、“QML测试”、“QtQuickTest”、“编写一个测试用例”、“测试这个类”或“生成一个测试文件”时,应该使用此技能。涵盖了C++ QTest、Python pytest-qt以及与CMake集成的QML TestCase模式。此外,对于“编写一个C++ Qt测试”、“添加一个CMake测试目标”或“设置testlib”也会激活此技能。

person作者: jakexiaohubgithub

Qt Test Patterns

Qt testing spans three ecosystems: C++ QTest (native, zero dependencies), Python pytest-qt (PySide6 apps), and QML TestCase (QML component logic). This skill covers all three with CMake integration.

Choosing a Test Framework

| Scenario | Framework | |---|---| | C++ Qt classes / business logic | C++ QTest (QObject subclass + QTEST_MAIN) | | PySide6 GUI application | pytest + pytest-qt (qtbot fixture) | | QML component behavior | QtQuickTest (TestCase QML type) | | PySide6 non-GUI logic | pytest (no pytest-qt needed) |

Python / PySide6 with pytest-qt

Complete pytest-qt patterns — see references/python-pytest-qt.md for the full qtbot fixture API, signal waiting, conftest patterns, model testing, parametrize, async tests, and common gotchas.

Key config:

# pytest.ini
[pytest]
testpaths = tests
qt_api = pyside6

C++ QTest

Complete C++ QTest patterns — see references/cpp-qtest.md for the full macro reference, QSignalSpy, GUI/input simulation, benchmark macros, output formats, CMake patterns, and troubleshooting.

Key structure: each test class is a QObject subclass; private slots are test functions; QTEST_MAIN(ClassName) + #include "test_name.moc" at the end of the file.

QML TestCase

Complete QML TestCase patterns — see references/qml-testcase.md for the full assertion API, component creation, SignalSpy, async/timer testing, CMake setup, and common issues.

Key structure: TestCase QML item; test functions must start with test_; always call obj.destroy() to prevent leaks.

Additional Resources

Consult reference files in this skill's references/ directory for detailed patterns:

  • references/cpp-qtest.md — Full QTest macro reference, QSignalSpy, benchmark macros, output formats
  • references/python-pytest-qt.md — Complete pytest-qt fixture API, async patterns, model testing, common gotchas
  • references/qml-testcase.md — QML TestCase full API, async signal testing, component creation patterns

Working examples:

  • examples/test_calculator.py — Complete pytest-qt example with fixtures
  • examples/calculator_test.cpp — Complete C++ QTest example with data-driven tests