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

  1. From the simplectest site , download the latest version of simplectest and follow the instructions on the introduction page to run the included simple.c test. Simplectest uses C macro definitions in the include file tests.h to create its testing framework. Macros are dealt with at compile time by the C preprocessor and expanded prior to the actual compile taking place. For more info see : http://www.cprogramming.com/tutorial/cpreprocessor.html

 

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. 

  1. Write the SortList function and develop unit tests for it using the simplectest framework. Be sure to develop tests in small increments and after any change, re-run your tests to insure that previously successful tests continue to be so.

 

  1. Write and develop unit tests for the CopyList function.

 

  1. Note:  Linked also has a PrintList() function that outputs the contents of the list. How would you design unit test cases to test the print function?

Submission:

Submit the files linked.c and linkedtest.c to your repository in a directory named CUnitTesting