Home

xUnit++

xUnit++ is a unit testing platform for C++ inspired by xUnit.net. It is written entirely in C++11-conformant code, and should be portable to any platform that is minimally compliant with the standard (where minimally compliant means "what Visual Studio 2012 supported at RTM").

For status updates, follow me on Twitter: @xunitpp, @moswald.

Documentation

Installation and Setup
Tests
Suites and Attributes
Test Events: Assert, Check, Warn, and Log
Running tests
How does xUnit++ compare to ...?

About xUnit++

xUnit++ borrows heavily from xUnit.net, which means it has the same strengths. It is built to support Test Driven Development, with a goal of being as simple and flexible as possible without sacrificing features found in modern unit testing platforms written for other languages (such as C#). It is compatible with Visual Studio 2012 (and includes integration with the IDE's test explorer window) and gcc 4.7+ (tested on Ubuntu Linux).

Quick Look

Let's look at a simple example:

#include "xUnit/xUnit++.h"

int Double(int x)
{
   return x * x;
}

FACT("Doubling 2 returns 4")
{
   auto expected = 4;
   Assert.Equal(expected, Double(2));
}

Now, this is obviously a contrived example, not to mention the code under test is incorrect. A typical suite of unit tests for a C++ function like this would want to include multiple checks like the following:

FACT("Doubling 1 returns 2")
{
   auto expected = 2;
   Assert.Equal(expected, Double(1));
}

FACT("Doubling -1 returns -2")
{
   auto expected = -2;
   Assert.Equal(expected, Double(-1));
}

Now you've got a couple of tests that are going to expose the error in the function Double, but it's a bit annoying to have to write all those different tests that are each five lines long, four of which are boilerplate.. There's got to be a better way, and with xUnit++, there is:

#include "xUnit++/xUnit++.h"

int Double(int x)
{
   return x * x;
}

THEORY("Double Test", (int expected, int value),
   std::make_tuple(2, 1),
   std::make_tuple(4, 2),
   std::make_tuple(-2, -1),
   std::make_tuple(0, 0)
)
{
   Assert.Equal(expected, Double(value));
}

A Theory creates a parameterized test. Each set of parameters is bound to an instance of the test and run individually. In this example, the THEORY macro creates a test named Double Test which takes parameters (int expected, int value). The rest of the macro's parameters are std::tuples whose members resolve to the same type as the test parameters.

If your Theory data requires detailed setup (for instance, if the details of the test parameters are stored in a local database or CSV file), another macro DATA_THEORY creates the same sort of test, only instead of individual std::tuples, it takes any function object (raw function pointer, std::function, or functor object) that returns a container of std::tuples with a begin/end pair:

// functor
struct SqlProvider
{
   SqlProvider()
   {
      // connect to database
      // fill data
   }

   const std::set<std::tuple<int, int>> &operator()() const
   {
      return data;
   }

   std::set<std::tuple<int, int>> data;
} functor;

// std::function
std::function<std::vector<std::tuple<int, int>>()> stdFn = ...;

// a custom container
struct CustomContainer
{
   CustomContainer()
   {
      // fill data
   }

   const CustomContainer &operator ()() const
   {
      return *this;
   }

   friend const std::tuple<int, int> *begin(const CustomContainer &cc)
   {
      return &cc.data[0];
   }

   friend const std::tuple<int, int> *end(const CustomContainer &cc)
   {
      return &cc.data[10];
   }

   std::tuple<int, int> data[10];
}

// or even a lambda! (note the parens around the lambda)
DATA_THEORY("Lambda Theory", (int expected, int value),
([]() -> std::vector<std::tuple<int, int>>
{
   std::vector<std::tuple<int, int>> data;

   // connect to database
   // fill data

   return data;
}))
{
   Assert.Equal(expected, Double(value));
}

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.