C Unit Testing
Introduction
Personal Software
Engineering – SE350
Summary
In this activity we will build on our familiarity with Java JUnit testing and gain some experience with unit testing in a C programming environment. We will be using a lightweight open source tool called “simplectest” which allows the developer to create unit test cases very similar to those seen in JUnit. There are many other C and C++ open source and commercial testing tools available.
Exercises
2. Download the files linked.c and linkedtest.c. Also download linked.h into the same directory. linkedtest.c uses the simplectest framework to manage test cases for the actual linked program. Be sure to have tests.h (simplectest) in the same directory as well. Use the linkedtest makefile to compile, link and run the test program linkedtest. When you separate your C program into multiple files, keep these points in mind:
· Be sure no two files have functions with the same name in it. The compiler will get confused.
· Similarly, if you use global variables in your program, be sure no two files define the same global variables.
· If you use global variables, be sure only one of the files defines them, and declare them in your .h as follows: extern node headNode; (See note below)
· To use functions from another file, make a .h file with the function prototypes (signatures), and use #include to include those .h files within your .c files.
· One of the files must have a main() function. Note that linked.c no longer has a main() function, the test code found in main() has been moved to linkedtest.c. That is why the make file compiles linked.c with the “–c” option meaning that it only produces an object file and no linking is performed. The linker is invoked to create an executable after the compile of linkedtest.c
Note: When you define a variable, it looks like this: Node headNode;. When you declare a variable, it looks like this: extern Node headNode;. The main difference is that a variable definition creates the variable, while a declaration indicates that the variable is defined elsewhere. A definition implies a declaration.