| layout | default |
|---|---|
| title | CppUTest |
CppUTest is a C /C++ based unit xUnit test framework for unit testing and for test-driving your code. It is written in C++ but is used in C and C++ projects and frequently used in embedded systems but it works for any C/C++ project.
CppUTest is based on the following design principles
- Simple in design and simple in use.
- Portable to old and new platforms.
Linux
There is an Debian and Ubuntu package available for CppUTest. This is by far the easiest way to install it, via:
{% highlight bash %} $ apt-get install cpputest {% endhighlight %}
MacOSX
For Mac, a Homebrew package is available too. You can install via:
{% highlight bash %} $ brew install cpputest {% endhighlight %}
The download link for github is at the top of this page. You can either get the latest code or a specific release.
You can find all the downloads at the download page
Alternatively, you can clone the github repository, read-only:
{% highlight bash %} $ git clone git://github.com/cpputest/cpputest.git {% endhighlight %}
Or clone it via ssh (which requires a github account)
{% highlight bash %} $ git clone git@github.com:cpputest/cpputest.git {% endhighlight %}
- The CppUTest manual can be found at CppUTest.org
- If you have any question, check out the Google Groups
- The source is at the main github page
- You can report bugs or features at the issues page
- You can follow CppUTest on twitter
To write your first test, all you need is a new cpp file with a TEST_GROUP and a TEST, like:
{% highlight c++ %} TEST_GROUP(FirstTestGroup) { };
TEST(FirstTestGroup, FirstTest) { FAIL("Fail me!"); } {% endhighlight %}
This test will fail.
You can add new tests to the test group by just writing more tests in the file, like this:
{% highlight c++ %} TEST(FirstTestGroup, SecondTest) { STRCMP_EQUAL("hello", "world"); LONGS_EQUAL(1, 2); CHECK(false); } {% endhighlight %}
You do need to create a main where you run all the unit tests. Such a main will look like this:
{% highlight c++ %} int main(int ac, char** av) { return CommandLineTestRunner::RunAllTests(ac, av); } {% endhighlight %}
For more information, We'd recommend to read the manual or, even better, check some existing tests such as SimpleStringTest or (a bit more complicated) MemoryLeakDetectorTest or the mocking tests or just check out the Cheat Sheet
- For Eclipse users, check out the CppUTest Eclipse Plugin Project
CppUTest has had many contributions from its users. We can't remember all, but we appreciate it a lot!. Much of the original code was written by Michael Feathers (based on CppUnit Lite). The current main maintainers are @jwgrenning and @basvodde