blob: 7acb1dd55e7ae3da981d4df8c3c748ff2a8283ff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# Testing
Testing standards and practices.
**Framework**: QtTest is the only supported test framework. Do not introduce other frameworks.
---
## Requirements
| Change Type | Test Required |
|-------------|---------------|
| New logic | Yes |
| Bug fix | Yes (regression) |
| Refactor | Recommended |
| UI layout | No |
| Docs | No |
---
## Test Structure
```cpp
#include <QTest>
#include "MyClass.h"
class MyClassTest : public QObject {
Q_OBJECT
private slots:
void initTestCase(); // Before all tests
void cleanupTestCase(); // After all tests
void init(); // Before each test
void cleanup(); // After each test
void test_methodName_condition();
};
QTEST_GUILESS_MAIN(MyClassTest)
#include "MyClassTest.moc"
```
---
## Naming
- **File**: `ClassName_test.cpp`
- **Class**: `ClassNameTest`
- **Method**: `test_method_condition`
---
## Async Testing
Use `QSignalSpy` instead of `sleep()`:
```cpp
QSignalSpy spy(obj, &MyClass::finished);
obj->startAsync();
QVERIFY(spy.wait(1000));
QCOMPARE(spy.count(), 1);
```
---
## Mocking
Create fake implementations for external dependencies:
```cpp
class FakeNetworkService : public INetworkService {
public:
void get(const QString& url) override {
emit finished("Fake Data");
}
};
```
**Forbidden practices**:
- Real network requests
- Real file system access outside `QTemporaryDir`
- User accounts or credentials
- `sleep()` or timing-based waits (use `QSignalSpy::wait()`)
- System-specific state dependencies
## UI Testing
UI tests are generally discouraged. Prefer testing core logic via unit tests.
If UI testing is necessary:
- Keep scope minimal (widget behavior only)
- Do not test layout or visual appearance
- Use `QTest::mouseClick()` and `QTest::keyClick()` for interaction
- Isolate widgets from services using mocks
---
## Performance
- Single test: < 100ms
- Mark slow tests clearly
- Use `QTemporaryDir` for file tests
---
## Running Tests
**VS Code**: Testing tab → Run
**Command line (with presets)**:
```bash
ctest --preset default --output-on-failure
```
**Without presets**:
```bash
cd build
ctest --output-on-failure
```
**Single test**:
```bash
./build/<preset>/tests/MyClassTest
```
---
## CI
- All tests run via `ctest`
- Failing tests block merge
- Must pass on all platforms
## Launcher Coverage
- UI is excluded from core coverage requirements
- See the launcher subsystem test matrix: [Launcher Test Matrix](./LAUNCHER_TEST_MATRIX.md)
---
## Related
- [Architecture](./ARCHITECTURE.md)
- [Project Structure](./PROJECT_STRUCTURE.md)
|