An attempt on unit testing in C++ I have no much experience with C++, so it is about time to start actually doing something about it. All in all, I coded this tiny unit test library:

assert.h:

#ifndef ASSERT_H
#define ASSERT_H

#include &lt;iostream&gt;

#define ASSERT(CONDITION) assert(CONDITION, #CONDITION, __FILE__, __LINE__);
#define REPORT            assert.report();
#define TOTAL_ASSERTIONS  assert.get_total_number_of_assertions()
#define FAILED_ASSERTIONS assert.get_number_of_failed_assertions()

class Assert {
public:
    bool operator()(const bool condition, 
                    const char *const condition_text, 
                    const char *const file_name, 
                    const int line_number);

    size_t get_total_number_of_assertions() const;
    size_t get_number_of_failed_assertions() const;
    void report() const;

private:
    size_t m_total_assertions;
    size_t m_failed_assertions;
};

// Can't think of anything better than a global.
extern Assert assert;

#endif  // ASSERT_H 


assert.cpp:

#include "assert.h"
#include &lt;iostream&gt;

bool Assert::operator()(const bool condition, 
                        const char *const condition_text, 
                        const char *const file_name, 
                        const int line_number)
{
    if (!condition)
    {
        m_failed_assertions++;
        std::cerr &lt;&lt; "'" &lt;&lt; condition_text &lt;&lt; "' is not true in file "
                  &lt;&lt; "'" &lt;&lt; file_name &lt;&lt; "' at line " &lt;&lt; line_number &lt;&lt; "." 
                  &lt;&lt; std::endl;
    }

    m_total_assertions++;
    return condition;
}

size_t Assert::get_number_of_failed_assertions() const
{
    return m_failed_assertions;
}

size_t Assert::get_total_number_of_assertions() const
{
    return m_total_assertions;
}

void Assert::report() const
{
    std::cout &lt;&lt; "[TOTAL ASSERTIONS: " 
              &lt;&lt; m_total_assertions
              &lt;&lt; ", FAILED ASSERTIONS: "
              &lt;&lt; m_failed_assertions
              &lt;&lt; ", PASS RATIO: ";

    if (m_total_assertions == 0) 
    {
        std::cout &lt;&lt; "N/A";
    }
    else
    {
        std::cout &lt;&lt; ((float) m_failed_assertions) / m_total_assertions;
    }

    std::cout &lt;&lt; "]" &lt;&lt; std::endl;
}

Assert assert;


main.cpp:

#include "assert.h"

static bool shit()
{
    return true;
}

int main(int argc, char** argv) {
    ASSERT(3 == 3)
    ASSERT(4 == 2)
    ASSERT(2 != 1)
    ASSERT(shit() &amp;&amp; false)
    ASSERT(4 &amp; 0)
    REPORT
    std::cout &lt;&lt; TOTAL_ASSERTIONS &lt;&lt; std::endl;
    std::cout &lt;&lt; FAILED_ASSERTIONS &lt;&lt; std::endl;
    return 0;
}


So, what should I fix in order to get to more idiomatic use of the language?
I don't see anything wrong with your code as it is: formatting is consistent and tidy, functions do what they need to do and no more, no apparent bugs.

One nit: your header file includes &lt;iostream&gt; but doesn't need it - you should remove that.

Having said that, "polluting" the global namespace with an object named assert isn't a good idea: assert is defined as a macro in &lt;cassert&gt;, you'll get all sorts of problems if your users include that, directly or indirectly. (Having a file called assert.h isn't a great idea either, that's the name of the C standard library assert header.)

Also your ASSERT macro doesn't do what the standard assert macro does (calls std::abort if the condition doesn't check), so you're likely to confuse people.

While the ASSERT macro itself is necessary to pull in the context information (AFAIK there's no other way to do that), the other three are a bit gratuitous and would benefit from being plain functions. Don't use macros when they provide no real benefit.

Here's a sketch about how you could clean that up a bit and avoid these issues (I'm not very good with names, so YMMV):


In a header, declare four plain functions inside a namespace:


bool check(bool, char const*, char const*, int): same as your Assert::operator() function
void report() (or possibly void report(std::ostream&amp;) for a bit more flexibility)
int total_checks() and int failed_checks().

Also in that header define the CHECK function-like macro as you did your ASSERT, but using Testing::check instead.
In an implementation file, inside the namespace:


Define two static ints to store the total and failed counts
Define all the functions you declared.



This avoids the name clash, and "hides" the global state (the state is still global, so you still need to worry about thread-safety and such, it is just not as visible anymore).

Note: I don't think there's anything fundamentally wrong with using a class rather than plain functions in a namespace, and a class approach would be better if you ever intend to have more than one (e.g. one "test counter" per thread). But as it is, just plain old functions and static data seems enough.

Sample incomplete implementation:

testing.h

#pragma once

#define CHECK(x) Testing::check((x), #x, __FILE__, __LINE__)

namespace Testing
{
bool check(bool, char const *, char const *, int);
int total();
int failed();
}


testing.cpp

#include "testing.h"

#include &lt;iostream&gt;

namespace Testing
{
static int total_checks;
static int failed_checks;

bool check(bool ok, char const *what, char const *file, int line)
{
  total_checks++;
  if (!ok) {
    failed_checks++;
    std::cerr &lt;&lt; "Failure of " &lt;&lt; what &lt;&lt; " at " &lt;&lt; file &lt;&lt; ":" &lt;&lt; line
              &lt;&lt; std::endl;
  }
  return ok;
}

int total() { return total_checks; }
int failed() { return failed_checks; }
}


Use case:

#include "testing.h"

#include &lt;iostream&gt;

int main()
{
  CHECK(1==1);
  CHECK(1==0);
  std::cout &lt;&lt; Testing::failed() &lt;&lt; " / " &lt;&lt; Testing::total() &lt;&lt; std::endl;
}