Testing C Code
Testing C Code
Testing C Code
… a short intro
Sufficient flags
-pedantic -Wconversion -Wall -Werror -Wextra -Wstrict-prototypes
http://virt08.itu.chalmers.se/mediawiki/index.php/Chapter:C_testing#Compiling_wit
h_sufficient_flags
Black box
“Black-box testing is a method of software testing that examines the functionality
of an application without peering into its internal structures or workings. This
method of test can be applied to virtually every level of software testing: unit,
integration, system and acceptance. It typically comprises most if not all higher
level testing, but can also dominate unit testing as well.”
https://en.wikipedia.org/wiki/Black-box_testing
Unit test
“In computer programming, unit testing is a software testing method by which
individual units of source code, sets of one or more computer program modules
together with associated control data, usage procedures, and operating
procedures, are tested to determine whether they are fit for use.[1] Intuitively, one
can view a unit as the smallest testable part of an application. In procedural
programming, a unit could be an entire module, but it is more commonly an
individual function or procedure.”
https://en.wikipedia.org/wiki/Unit_testing
max.c
int max(int a, int b)
if (a>b)
return a;
return b;
}
simple-math.c
https://github.com/progund/programming-with-c/blob/master/te
sting/presentation-material/simple-math.c
https://github.com/progund/programming-with-c/blob/master/te
sting/presentation-material/simple-math.h
Assert - example NOT using assert
value = max(13,14);
if (value!=14)
…...
assert (max(13,14)==14);
https://github.com/progund/programming-with-c/blob/master/te
sting/presentation-material/assert.c
Assert - example using assert
Example:
https://github.com/progund/programming-with-c/blob/master/testing/presentation-
material/check-max.c
Unit test frameworks - check
$ gcc check-max.c simple-math.c -lm -lcheck -o check-max && ./check-max
Example:
https://github.com/progund/programming-with-c/blob/master/testing/presentation-
material/cu-max.c
Unit test frameworks - CUnit
$ gcc cu-max.c simple-math.c -lcunit -lm -o cu-max && ./cu-max
http://cunit.sourceforge.net/
Unit test frameworks - CUnit
Suite: Suite_1
passed
suites 1 1 n/a 0 0
tests 1 1 1 0 0
asserts 1 1 1 0 n/a
https://en.wikipedia.org/wiki/Code_coverage
Code coverage - gcov
$ gcc -fprofile-arcs -ftest-coverage assert.c simple-math.c -o assert -lm
$ ./assert
$ gcov -b math.c
Code coverage - gcov
math.c.gcov:
-: 43: // Check if the sum fits in an int
$ ./assert
https://en.wikipedia.org/wiki/Test_automation
jenkins
https://builds.apache.org/