CS 371 Lab #10: Unit Testing
Due: before the final exam, May 8
In this lab we will work on Unit Testing. C++ programmers can use
a C++ Unit Testing tool called CppUnit.
Your goal is to produce unit tests for all major classes in your project.
Your friendly TA will be available to demonstrate JUnit.
Lab Work Step by Step
- C++ Instructions
- CppUnit lives in
/home/uni1/jeffery/371/cppunit-1.10.2/src/cppunit.
In order for g++ to find cppunit's libraries you will need
to tell it where they live, with a -L argument like:
-L/home/uni1/jeffery/371/cppunit-1.10.2/lib
- In order for g++ to find cppunit's include files you will need
to tell it where they live, with a -I argument like:
-I/home/uni1/jeffery/371/cppunit-1.10.2/include
- Read the CppUnit tutorial at http://www.evocomp.de/tutorials/tutorium_cppunit/howto_tutorial_cppunit.html
- Read the CppUnit documentation at
http://www.cs.nmsu.edu/~jeffery/courses/371/cppunit/
- Write a unit test suite for the following "dummy"
class.
These are supposed to be complex numbers, with standard
mathematical behavior as per the following definitions of
addition and
subtraction,
multiplication, and
division.
. Your unit test suite should test whether
created objects behave in a "sane" manner, i.e. whether the constructor and
methods behave correctly. Is there a bug here? Verify it by testing,
rather than simply staring at the code as you usually would. :-)
class Complex {
double re, im;
public:
Complex(double r1, double r2) {
re = r1;
im = r2;
}
double real() {
return re;
}
double imaginary() {
return im;
}
Complex add(Complex c) {
return Complex( re + c.real(), im + c.imaginary());
}
Complex subtract(Complex c) {
return Complex( re - c.real(), im - c.imaginary());
}
Complex multiply(Complex c) {
return Complex( re * c.real(), im * c.imaginary());
}
Complex divide(Complex c) {
return Complex( re / c.real(), im / c.imaginary());
}
};
- Write a unit test suite for two of the classes from your project.
Choose different classes than your team members so we don't duplicate
efforts here.
For each class, your tests should construct instances of that class
and then, for each method, write tests for that method.
Check method parameters. Try to get basic statement-block coverage,
i.e. have tests that execute each line in the method at least once.
- Add your test suite to CVS.
- Merge your unit test suite with the rest of the class to form a
"comprehensive unit test".
- Add a makefile entry for an executable that does the unit testing,
this goes at the bottom and should not affect our regular project build.
Check it into CVS.
- (Java Instructions)
- Set your CLASSPATH environment variable so it can find JUnit.
Add the following line to your .cshrc file:
setenv CLASSPATH .:/home/uni1/jeffery/junit3.8.1/junit.jar
- Read the Junit documentation at
- Read Yonggang Lu's Junit howto
- Write a unit test suite for the following "dummy" class. These are supposed to be complex numbers, with standard mathematical behavior as per the following definitions of
addition and
subtraction,
multiplication, and
division.
. Your unit test suite should test whether
created objects behave in a "sane" manner, i.e. whether the constructor and
methods behave correctly. Is there a bug here? Verify it by testing,
rather than simply staring at the code as you usually would. :-)
public class Complex {
double re, im;
public Complex(double r1, double r2) {
re = r1;
im = r2;
}
public double real() {
return re;
}
public double imaginary() {
return im;
}
public Complex add(Complex c) {
return new Complex( re + c.real(), im + c.imaginary());
}
public Complex subtract(Complex c) {
return new Complex( re - c.real(), im - c.imaginary());
}
public Complex multiply(Complex c) {
return new Complex( re * c.real(), im * c.imaginary());
}
public Complex divide(Complex c) {
return new Complex( re / c.real(), im / c.imaginary());
}
}
- Write a unit test suite for two of the classes from your project.
Choose different classes than your team members so we don't duplicate
efforts here.
For each class, your tests should construct instances of that class
and then, for each method, write tests for that method.
Check method parameters. Try to get basic statement-block coverage,
i.e. have tests that execute each line in the method at least once.
- Add your test suite to CVS.
- Merge your unit test suite with the rest of the class to form a
"comprehensive unit test".
- Add a makefile entry for an executable that does the unit testing,
this goes at the bottom and should not affect our regular project build.
Check it into CVS.