Testing C Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

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)

printf (" FAILED\n");

…...

printf (" OK\n");


Assert - example using assert
printf ("Test max using assert: ");

assert (max(13,14)==14);

printf (" OK\n");

https://github.com/progund/programming-with-c/blob/master/te
sting/presentation-material/assert.c
Assert - example using assert

$ gcc assert.c simple-math.c -o assert -lm && ./assert

Test max using assert: OK


Assert - disabling assert in GCC

gcc -DNDEBUG assert.c simple-math.c -o assert -lm &&


./assert
Unit test frameworks - check
Check - https://libcheck.github.io/check/

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

Running suite(s): Max

Test max using assert: OK

100%: Checks: 1, Failures: 0, Errors: 0


Unit test frameworks - CUnit
CUnit - http://cunit.sourceforge.net/

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

CUnit - A unit testing framework for C - Version 2.1-3

http://cunit.sourceforge.net/
Unit test frameworks - CUnit
Suite: Suite_1

Test: test of max() ...Test max using assert: OK

passed

Run Summary: Type Total Ran Passed Failed Inactive

suites 1 1 n/a 0 0

tests 1 1 1 0 0

asserts 1 1 1 0 n/a

Elapsed time = 0.000 seconds


Code coverage
“In computer science, code coverage is a measure used to describe the degree to
which the source code of a program is executed when a particular test suite runs.
A program with high code coverage, measured as a percentage, has had more of
its source code executed during testing which suggests it has a lower chance of
containing undetected software bugs compared to a program with low code
coverage. Many different metrics can be used to calculate code coverage; some of
the most basic are the percent of program subroutines and the percent of program
statements called during execution of the test suite.”

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

#####: 44: if ((sum>INT_MAX) || (sum<INT_MIN) )

branch 0 never executed

branch 1 never executed

branch 2 never executed

branch 3 never executed


Code coverage - gcov and lcov

$ gcc -g --coverage -fprofile-arcs -ftest-coverage assert.c simple-math.c -o assert


-lm

$ ./assert

$ lcov --directory . --capture --output-file simple-math.info

$ genhtml -o coverage simple-math.info


Test automation
“In software testing, test automation is the use of special software (separate from
the software being tested) to control the execution of tests and the comparison of
actual outcomes with predicted outcomes.[1] Test automation can automate some
repetitive but necessary tasks in a formalized testing process already in place, or
add additional testing that would be difficult to perform manually. Test automation
is critical for continuous testing.”

https://en.wikipedia.org/wiki/Test_automation
jenkins
https://builds.apache.org/

You might also like