-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_9.html
More file actions
93 lines (80 loc) · 3.6 KB
/
Copy pathCpp2_9.html
File metadata and controls
93 lines (80 loc) · 3.6 KB
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
<article>
<h1>C++ II: Page 9 - Testing: Unit Testing</h1>
<section>
<h2>The Quality Assurance Cycle</h2>
<p>Unit testing is mandatory in professional C++ development. It ensures your code behaves as expected and protects against regressions when you refactor or add new features.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">
/*
* File: test_example.cpp
* Demonstration of robust unit testing using GoogleTest structure
*/
#include <iostream>
#include <cassert>
#include <string>
#include <vector>
using namespace std;
// Function to test
int Add(int a, int b) {
return a + b;
}
// A simple test runner (Manual)
void runTest(bool condition, const string& testName) {
if (condition) {
cout << "[PASS] " << testName << endl;
} else {
cout << "[FAIL] " << testName << endl;
}
}
int main() {
cout << "--- Running Tests ---" << endl;
// Test Case 1: Positive Numbers
runTest(Add(2, 3) == 5, "Add_PositiveNumbers");
// Test Case 2: Negative Numbers
runTest(Add(-1, -1) == -2, "Add_NegativeNumbers");
// Test Case 3: Zero
runTest(Add(0, 5) == 5, "Add_Zero");
// In a professional environment, use Catch2 or GoogleTest:
/*
TEST_CASE("Addition works", "[math]") {
REQUIRE(Add(2, 3) == 5);
REQUIRE(Add(-1, -1) == -2);
}
// GoogleTest example:
TEST(AddTest, HandlesPositive) {
EXPECT_EQ(Add(2, 3), 5);
}
*/
return 0;
}
/*
* Testing Nuances:
* 1. Unit tests: Verify the smallest piece of logic in isolation.
* 2. Integration tests: Verify interaction between modules (e.g., DB connection).
* 3. Assertions (cassert): Simple runtime checks; use for debugging.
* 4. Test frameworks (GTest, Catch2): Provide detailed reports,
* test fixtures, and sophisticated assertions.
* 5. TDD (Test Driven Development): Write the test first, then the code.
* 6. Coverage tools: Tools like gcov/lcov ensure your tests cover
* the majority of your codebase.
* 7. Mocking: Use libraries like GoogleMock to simulate dependencies
* (e.g., database, network) to test in isolation.
*/
</code></pre>
<div class="interactive-sim">
<h3>Test Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_9');
out.innerHTML = 'Running tests...<br>';
setTimeout(() => out.innerHTML += '>> [PASS] Add_PositiveNumbers<br>>> [PASS] Add_Zero', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_9" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_8.html" style="text-decoration: none; color: #6c757d;">← Previous: Build: CMake</a>
<a href="#" data-file="Cpp2_10.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Advanced Concepts: Growth Path →</a>
</div>
</footer>
</article>