mirror of
https://github.com/ysoftdevs/cpp-examples.git
synced 2026-01-17 17:21:49 +01:00
Add Check unit testing example
This commit is contained in:
13
README.md
13
README.md
@@ -25,6 +25,19 @@ How to run:
|
|||||||
./example-02
|
./example-02
|
||||||
./example-03
|
./example-03
|
||||||
|
|
||||||
|
Check - unit test
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Check is unit testing framework for C: http://check.sourceforge.net/
|
||||||
|
|
||||||
|
This directory contains simple example. You can uncomment age++ to create bug and rerun tests.
|
||||||
|
|
||||||
|
How to run:
|
||||||
|
|
||||||
|
cd check
|
||||||
|
make
|
||||||
|
./test
|
||||||
|
|
||||||
curl
|
curl
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|||||||
15
check/Makefile
Normal file
15
check/Makefile
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
OBJ = main.o
|
||||||
|
LDADD = -lcheck
|
||||||
|
|
||||||
|
test: $(OBJ)
|
||||||
|
${CC} ${OBJ} -o test ${LDADD}
|
||||||
|
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
${CC} ${CFLAGS} -c $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f test
|
||||||
|
|
||||||
92
check/main.c
Normal file
92
check/main.c
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Check testing framework - http://check.sourceforge.net/ */
|
||||||
|
#include <check.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application logic for employee.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef struct Person {
|
||||||
|
char * name;
|
||||||
|
char * surname;
|
||||||
|
int age;
|
||||||
|
} Person;
|
||||||
|
|
||||||
|
void increment_age(Person *person) {
|
||||||
|
person->age++;
|
||||||
|
// Uncomment following line to create bug
|
||||||
|
// person->age++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rename_person(Person *person, char *new_name) {
|
||||||
|
person->name = new_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit testing stuff - This is just example. In real test case
|
||||||
|
* put this to separate file.
|
||||||
|
*/
|
||||||
|
Person *employee;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test fixture - setup method - initialize employee
|
||||||
|
*/
|
||||||
|
void setup() {
|
||||||
|
employee = malloc(sizeof(Person));
|
||||||
|
employee->name = "Arnost";
|
||||||
|
employee->surname = "Topinka";
|
||||||
|
employee->age = 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up after testing. Release memory.
|
||||||
|
*/
|
||||||
|
void teardown() {
|
||||||
|
free(employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
START_TEST (test_age) {
|
||||||
|
setup();
|
||||||
|
increment_age(employee);
|
||||||
|
fail_unless(employee->age == 43, "Incorrect age.");
|
||||||
|
teardown();
|
||||||
|
}
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_name) {
|
||||||
|
setup();
|
||||||
|
rename_person(employee, "Franta");
|
||||||
|
fail_unless( !strcmp( employee->name, "Franta"), "Employee should be renamed.");
|
||||||
|
teardown();
|
||||||
|
}
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create test suite.
|
||||||
|
*/
|
||||||
|
Suite *make_suite() {
|
||||||
|
Suite *suite;
|
||||||
|
TCase *test_case;
|
||||||
|
suite = suite_create("Employee testing");
|
||||||
|
test_case = tcase_create("Core tests");
|
||||||
|
tcase_add_test(test_case, test_age);
|
||||||
|
tcase_add_test(test_case, test_age);
|
||||||
|
tcase_add_test(test_case, test_name);
|
||||||
|
tcase_add_test(test_case, test_name);
|
||||||
|
tcase_add_test(test_case, test_name);
|
||||||
|
tcase_add_test(test_case, test_age);
|
||||||
|
tcase_add_test(test_case, test_name);
|
||||||
|
suite_add_tcase(suite, test_case);
|
||||||
|
return suite;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int number_failed;
|
||||||
|
Suite *suite = make_suite();
|
||||||
|
SRunner *suite_runner = srunner_create(suite);
|
||||||
|
srunner_run_all (suite_runner, CK_NORMAL);
|
||||||
|
number_failed = srunner_ntests_failed (suite_runner);
|
||||||
|
srunner_free (suite_runner);
|
||||||
|
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user