PicoTest
A minimalist unit testing framework for C programs
Loading...
Searching...
No Matches
filter.c File Reference

Example of PicoTest test filter, allows custom filtering of test functions. More...

Detailed Description

Example of PicoTest test filter, allows custom filtering of test functions.

#include <stdio.h>
#include <picotest.h>
/* Custom test filter declaration. */
PicoTestFilterProc matchSubstring;
#undef PICOTEST_FILTER
#define PICOTEST_FILTER matchSubstring
/* Test filter function. */
PicoTestFilterResult matchSubstring(PicoTestProc *test, const char *testName, const char *cond) {
/* Match tests containing **cond** substring. */
return (strstr(testName, cond) == NULL
}
/* Hooks */
PicoTestCaseEnterProc logCaseEnter;
PicoTestCaseLeaveProc logCaseLeave;
PicoTestSuiteEnterProc logSuiteEnter;
PicoTestSuiteLeaveProc logSuiteLeave;
#undef PICOTEST_CASE_ENTER
#undef PICOTEST_CASE_LEAVE
#undef PICOTEST_SUITE_ENTER
#undef PICOTEST_SUITE_LEAVE
#define PICOTEST_CASE_ENTER logCaseEnter
#define PICOTEST_CASE_LEAVE logCaseLeave
#define PICOTEST_SUITE_ENTER logSuiteEnter
#define PICOTEST_SUITE_LEAVE logSuiteLeave
int level = 0;
void indent(int level) {
while (level--) printf(" ");
}
void logCaseEnter(const char *name) {
indent(level++);
printf("running test case %s\n", name);
}
void logCaseLeave(const char *name, int fail) {
level--;
}
void logSuiteEnter(const char *name, int nb) {
indent(level++);
printf("running test suite %s\n", name);
}
void logSuiteLeave(const char *name, int nb, int fail) {
level--;
}
/* Main test suite */
#include "mainSuite.inc"
void main() {
/*
* Run all tests in order:
*
* running test suite mainSuite
* running test case testCase1
* running test case testCase2
* running test suite subSuite
* running test case testCase4
* running test case testCase5
* running test case testCase3
*/
printf("Run all tests:\n");
mainSuite(NULL);
printf("\n");
/*
* Run tests containing the string "Case":
*
* running test case testCase1
* running test case testCase2
* running test case testCase4
* running test case testCase5
* running test case testCase3
*/
printf("Run tests containing \"Case\":\n");
mainSuite("Case");
printf("\n");
}
int PicoTestProc(const char *cond)
Signature of test functions.
Definition picotest.h:104
PicoTestFilterResult PicoTestFilterProc(PicoTestProc *test, const char *testName, const char *cond)
Signature of test filter functions.
Definition picotest.h:215
PicoTestFilterResult
Result of test filter functions.
Definition picotest.h:175
@ PICOTEST_FILTER_SKIP_PROPAGATE
Test does not match the condition, skip this test but filter its subtests.
Definition picotest.h:185
@ PICOTEST_FILTER_PASS_PROPAGATE
Test matches the condition, run this test but filter its subtests.
Definition picotest.h:188
void PicoTestCaseEnterProc(const char *testName)
Function signature of test case enter hooks.
Definition picotest.h:633
void PicoTestCaseLeaveProc(const char *testName, int fail)
Function signature of test case leave hooks.
Definition picotest.h:681
void PicoTestSuiteEnterProc(const char *suiteName, int nb)
Function signature of test suite enter hooks.
Definition picotest.h:1469
void PicoTestSuiteLeaveProc(const char *suiteName, int nb, int fail)
Function signature of test suite leave hooks.
Definition picotest.h:1517
Example of a simple PicoTest suite.
This file defines a minimalist unit testing framework for C programs.