-
Notifications
You must be signed in to change notification settings - Fork 915
Description
I propose adding a unit-testing framework and unit-tests to SU2. After chatting with @economon, I've decided to move the discussion here to get additional input.
What is unit testing?
For those not familiar with unit testing, unit testing allows the testing of small bits of behavior, ideally using isolated bits of code. It is not intended to replace validation testing or formal verification tests. Instead, it serves a unique purpose. Consider the three following use cases:
- You're developing a new feature, and you want to test it to see if it works. You could do a full simulation, but that takes a lot of time and computing power. You want to check if your new behavior behaves as you suspect before you throw a lot of resources at it.
- You submit a PR and discover that one of the regression tests has failed. But...why? You know that something is broken, but its hard to track down what broke. You want more granular test coverage that can demonstrate what broke.
- You are fixing a very small bug. You know that you should prove that your bug fix worked, but it doesn't seem logical to dedicate an entire validation case to one small bug fix. You want to write a small test for a small fix.
In all of these cases, unit testing fills a unique role. Unit testing increases time spent in development, but decreases the amount of time spent in bug-fixing and maintaining.
For more information, see this relevant Stack Exchange question.
What do I propose?
Our research group at UT Austin has implemented a unit testing framework on our branch, which we're happy with. Some choices were arbitrary, and some choices were made based on our development environment. Those choices may be different for other groups. Here's what we have done:
The unit testing framework is compiled and run using autotools. For more information on autotool's setup, see their documentation. Since autotools is the build system for SU2, this involves minimal changes.
Using automake, the build process for building unit tests becomes:
./bootstrap
./configure
make
make check
We use Boost's unit testing framework. This provides a convenient set of macros for instatiating tests, grouping tests into suites, and running checks. This choice was based on what is available in our development setup.
We have integrated our unit tests into our Travis CI regression testing. Every time we push commits or submit a pull request, the unit tests are run and checked.
What is my vision for unit testing in SU2?
I am not proposing that we start trying to get 100% code coverage with pre-existing code. That would not provide a good return on investment.
Instead, I see people adding unit tests as they write new code and as they find bugs. For each new behavior added to SU2, tests are first added to document the related existing behavior. These tests serve to check that the existing behavior isn't damaged by the new code. Then new tests are added to prove that the new behavior is working correctly. For bug fixes, the process is simpler. A test is added to confirm that something is not behaving as expected. Then the code is fixed to make the test pass.
What frameworks are available?
For a unit testing framework, here are the most popular options, with the following pros and cons:
Roll-your-own
- Requires no external dependencies
- The most flexible option
- Involves the most work to setup
- Will lack some of the more advances features of mature unit-testing frameworks.
Boost Test
- Can be header only, statically linked, or dynamically linked
- If statically or dynamically linked, then Boost is not very lightweight
- Easy to add if you're already using Boost
Google Test
- Most common unit-testing framework
- Can be easily combined with Google's powerful GMock mocking library
- Compiling and linking can be somewhat painful
Catch2
- Used by FEniCS
- Makes unit tests easily readable with lots of syntactic sugar.
- Has a very simple syntax
- Is header-only
Requires C++11 compilationRequires C++11 for full feature set, but offers a C++03 branch- Not as feature rich as Google Test or Boost Test
Questions
- How do developers feel about adding unit tests to SU2?
- If a unit-testing framework were added to SU2, would you actually use it?
- Do developers have a preference (or experience with) any of the unit testing frameworks?
- Should unit tests be expected when submitting PRs?