| name | qt-test-fixture-generator |
| description | Generate Qt Test fixtures with mock QObject signals and slots, data-driven tests, and GUI testing setup |
| allowed-tools | Read, Write, Edit, Bash, Glob, Grep |
| tags | ["qt","testing","unit-test","mock","fixtures"] |
| graph | {"domains":["domain:software-engineering"],"specializations":["specialization:desktop-development"],"skillAreas":["skill-area:desktop-ui-frameworks","skill-area:component-testing"],"roles":["role:desktop-developer","role:fullstack-engineer"],"workflows":["workflow:feature-development","workflow:release-management"]} |
qt-test-fixture-generator
Generate Qt Test fixtures with mock QObject signals and slots, data-driven tests, and GUI testing setup. This skill creates comprehensive test infrastructure for Qt applications.
Capabilities
- Generate QTest-based test fixtures
- Create mock QObjects with signal/slot support
- Set up data-driven tests with QFETCH
- Configure GUI testing with QTestLib
- Generate test doubles for Qt classes
- Set up benchmark tests
- Configure test coverage reporting
- Generate CMake test integration
Input Schema
{
"type": "object",
"properties": {
"projectPath": {
"type": "string",
"description": "Path to the Qt project"
},
"classToTest": {
"type": "string",
"description": "Name of the class to generate tests for"
},
"testType": {
"enum": ["unit", "integration", "gui", "benchmark"],
"default": "unit"
},
"mockDependencies": {
"type": "array",
"items": { "type": "string" },
"description": "Classes to mock"
},
"dataProviders": {
"type": "array",
"items": {
"type": "object",
"properties": {
"testName": { "type": "string" },
"columns": { "type": "array" },
"rows": { "type": "array" }
}
}
},
"generateCoverage": {
"type": "boolean",
"default": true
}
},
"required": ["projectPath", "classToTest"]
}
Output Schema
{
"type": "object",
"properties": {
"success": { "type": "boolean" },
"files": {
"type": "array",
"items": {
"type": "object",
"properties": {
"path": { "type": "string" },
"type": { "enum": ["test", "mock", "cmake"] }
}
}
},
"runCommand"
Generated Test Fixture
#include <QtTest/QtTest>
#include <QSignalSpy>
#include "mywidget.h"
#include "mockservice.h"
class tst_MyWidget : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
void testConstructor();
void testSetValue();
void testSetValue_data();
void testSignalEmission();
;
;
;
:
MyWidget* m_widget;
MockService* m_mockService;
};
{
() << ;
}
{
}
{
m_mockService = ();
m_widget = (m_mockService);
}
{
m_widget;
m_mockService;
m_widget = ;
m_mockService = ;
}
{
(m_widget != );
(m_widget->(), );
(m_widget->());
}
{
QTest::<>();
QTest::<>();
QTest::<>();
QTest::() << << << ;
QTest::() << << << ;
QTest::() << << << ;
QTest::() << INT_MAX << INT_MAX << ;
}
{
(, input);
(, expected);
(, shouldEmitSignal);
;
m_widget->(input);
(m_widget->(), expected);
(spy.(), shouldEmitSignal ? : );
}
{
;
(spy.());
m_widget->();
(spy.(), );
QList<QVariant> arguments = spy.();
(arguments.().(), );
}
{
QPushButton* button = m_widget-><QPushButton*>();
(button != );
;
QTest::(button, Qt::LeftButton);
(spy.(), );
}
{
QLineEdit* input = m_widget-><QLineEdit*>();
(input != );
input->();
QTest::(input, );
(input->(), ());
QTest::(m_widget, Qt::Key_S, Qt::ControlModifier);
}
{
QBENCHMARK {
m_widget->();
}
}
(tst_MyWidget)
Mock Object
#include <QObject>
class MockService : public QObject
{
Q_OBJECT
public:
explicit MockService(QObject* parent = nullptr) : QObject(parent) {}
int fetchDataCallCount() const { return m_fetchDataCalls; }
void resetCalls() { m_fetchDataCalls = 0; }
void setFetchDataResult(const QString& result) { m_fetchDataResult = result; }
public slots:
QString fetchData(int id) {
m_fetchDataCalls++;
m_lastFetchId = id;
emit dataRequested(id);
return m_fetchDataResult;
}
signals:
void dataRequested(int id);
public:
int lastFetchId() const { return m_lastFetchId; }
private:
int m_fetchDataCalls = ;
m_lastFetchId = ;
QString m_fetchDataResult = ;
};
CMake Integration
# tests/CMakeLists.txt
find_package(Qt6 REQUIRED COMPONENTS Test)
enable_testing()
# Add test executable
add_executable(tst_mywidget
tst_mywidget.cpp
mockservice.h
)
target_link_libraries(tst_mywidget PRIVATE
Qt6::Test
MyAppLib # Library under test
)
# Register with CTest
add_test(NAME tst_mywidget COMMAND tst_mywidget)
# Coverage (with gcov)
if(ENABLE_COVERAGE)
target_compile_options(tst_mywidget PRIVATE --coverage)
target_link_options(tst_mywidget PRIVATE --coverage)
endif()
Running Tests
ctest --test-dir build
./build/tests/tst_mywidget
./build/tests/tst_mywidget -v1
./build/tests/tst_mywidget testSetValue
./build/tests/tst_mywidget testSetValue:positive
./build/tests/tst_mywidget -o results.xml,xml
Best Practices
- One assertion per test: Keep tests focused
- Use QSignalSpy: Verify signal emissions
- Use data-driven tests: Avoid code duplication
- Mock dependencies: Isolate unit under test
- Test edge cases: Boundary values, null inputs
- Name tests clearly: Describe expected behavior
Related Skills
qt-cmake-project-generator - Project setup
desktop-unit-testing process - Testing workflow
cross-platform-test-matrix - CI testing
Related Agents
qt-cpp-specialist - Qt expertise
desktop-test-architect - Test strategy