Next: , Up: Unit tests


2.1.1 What are unit tests?

Unit tests are simple, short tests that test the functionality of individual modules in your source code. A module can be a method of a class, or can be smaller or larger parts of your code. Unit tests are usually developed at the same time code is written, independently testing each small component of your algorithm is on it's own.

The main goal of unit testing is to isolate each part of your code and ensure that each part is correct. This allows you to refactor your code at a later time with the confidence that your refactoring gives the same behaviour as the original code. This allows you to quickly write code that works, and then later come and refactor your code to make it run faster.

Unit testing also allows you to check whether a particular piece of code still works properly. This is useful in a large software project such as Marsyas which has currently over 180,000 lines of code and many developers on different continents.

Unit tests can also provide a sort of living documentation for the system. Well written unit tests have example code in them that can help new developers understand how to use different MarSystems. Written documentation added to the unit tests also helps in this regard, and often the best place for verbose documentation is in the unit tests rather than in the main functions themselves, where they can occasionally hide the beautiful logic therein.

Test-Driven Development (TDD) is a technique where you first write your tests, and then write the actual that will make the tests pass. In TDD you have short iterative development cycles on pre-written test cases that define desired improvements or new functionality. On each iteration of the cycle, you write just the code that you need to make the tests pass. With TDD you write just the code that you need, not the code you think you might need at some point in the future.

Cxxtest unit testing framework that is built into Marsyas lets you write your tests either before, as in standard unit testing, or after, as in TDD.