Lab: Unit Testing

Steven J Zeil

Last modified: Dec 21, 2019
Contents:

This is a self-assessment activity to give you practice in working with unit test frameworks. Feel free to share your problems, experiences, and choices in the Forum.

1 Choose Your Project

For this lab, you will want a fairly simple program, in C++ or Java, that contains at least one well-defined ADT.

You might find this among some of your old homework for CS250, 330, 361, or 382. If you cannot find anything suitable, you might try

Pick an ADT (not the main program) for testing.

If you have selected a C++ program, proceed to the next section. If you have selected a Java program, jump ahead (for now) to Java Testing.

2 C++ Testing

  1. If your original program was in Java, translate it to C++.

  2. Set up an Eclipse project for your C++ code.

  3. Add either the Google Test or Boost Test framework to your project.

  4. Write a very simple unit test to start with - something that just asserts that 1 + 1 == 2.

  5. Compile this test, in the presence of your other code (even though it won’t be using that code yet).

    You may find it necessary to remove or rename your “real” main() function for this purpose, as the default Eclipse project builder for C++ can’t really deal with multiple executables. We’ll have to live with this until we learn how to add custom build managers.

  6. Run the test driver as a “normal” C++ executable program.

  7. Run the test driver as a unit test under the Eclipse framework. You should see the Eclipse GUI report on the number of test successes.

  8. Now replace that simple test with a real set of tests for your chosen ADT.

    If you are attempting this lab before we have covered ADT testing, use your best general Black-box skills. After we have covered ADT testing, return and examine your tests from a mutator/accessor perspective. What, if anything, would you add for a good ADT test?

  9. Again, run your tests both as normal executable programs and as unit tests under the Eclipse framework.

  10. If you have all of your tests in a single .cpp file, try splitting them into two test sets in separate .cpp files. Or, if your program has more than one ADT, write a test or two for the other ADT in a separate .cpp file.

    Again, verify that you can compile and run these tests under the Eclipse Framework.

3 Java Testing

  1. If your original program was in C++, translate it to Java.

  2. Set up an Eclipse project for your Java code.

  3. Add the JUnit library to the build path of your Java project.

  4. Write a very simple unit test to start with - something that just asserts that 1 + 1 == 2.

  5. Compile this test.

  6. Run the test driver as a unit test under the Eclipse framework. You should see the Eclipse GUI report on the number of test successes.

  7. Now replace that simple test with a real set of tests for your chosen ADT.

    If you are attempting this lab before we have covered ADT testing, use your best general Black-box skills. After we have covered ADT testing, return and examine your tests from a mutator/accessor perspective. What, if anything, would you add for a good ADT test?

  8. Again, run your tests under the Eclipse framework.

  9. If you have all of your tests in a single .java file, try splitting them into two test sets in separate .java files. Or, if your program has more than one ADT, write a test or two for the other ADT in a separate .java file.

    Again, verify that you can compile and run these tests under the Eclipse Framework.

If your original program was Java, and you skipped the C++ section initially, go back now and do the C++ portion of this lab.