Test framework
- C course
- Advanced
- Lesson 68/83
- 5 min
Learn to write your own test framework.
Test framework
A test framework is a small skeleton that runs tests and collects results. At its simplest it has a counter and a condition check.
The function void test_run(int cond) increments the passed counter when the condition is true. At the end you print how many passed with printf("geçti: %d\n", passed);. This is the core of real frameworks (Unity, CMocka); the function signature and counter structure start here.
Incrementing the counter
You declare a global counter int passed = 0;. Inside test_run you do passed++ when the condition is true. Globals are practical for small programs; larger projects move them into a struct.
The function signature: void test_run(int cond) { if (cond) passed++; }. With this minimal framework you can test any number of conditions. At the end you print a summary with printf. Counter accuracy is the foundation of a reliable framework.
Practice Simple test framework
Declare a global int passed = 0;. Write void test_run(int cond): if (cond) passed++;. In main call test_run(2 + 3 == 5); and print printf("geçti: %d\n", passed);.
Starter code
#include <stdio.h>
// Declare a global passed counter (start at 0)
// Write test_run: increment counter when condition is true
// main: test one arithmetic condition and print passed count
int main(void) {
return 0;
}Sample solution
#include <stdio.h>
int passed = 0;
void test_run(int cond) {
if (cond) passed++;
}
int main(void) {
test_run(2 + 3 == 5);
printf("geçti: %d\n", passed);
return 0;
}Expected output
geçti: 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match framework parts
| Term | Meaning |
|---|---|
test_run |
Tests condition |
passed |
Success counter |
cond |
Condition parameter |
printf |
Prints result |
2 What does test_run do?
- Allocates memory
- Opens a file
- Sends a signal
- Runs a test Correct answer
Explanation: test_run checks the condition and updates the counter.
3 test_run signature
void test_run(____) { if (cond) passed++; }
void test_run(int cond) { if (cond) passed++; }
Explanation: The int cond parameter carries the condition.