public
Last active

Use the pretty dot-formatted unit test output for googletest like PHPUnit / rspec.

  • Download Gist
example.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Produces an output like (will use the full available screen width):
 
............................................................ 60 / 1136 ( 5%)
............................................................ 120 / 1136 ( 10%)
...........F................................................ 180 / 1136 ( 15%)
............................................................ 240 / 1136 ( 21%)
............................................................ 300 / 1136 ( 26%)
............................................................ 360 / 1136 ( 31%)
............................................................ 420 / 1136 ( 36%)
............................................................ 480 / 1136 ( 42%)
............................................................ 540 / 1136 ( 47%)
............................................................ 600 / 1136 ( 52%)
............................................................ 660 / 1136 ( 58%)
............................................................ 720 / 1136 ( 63%)
............................................................ 780 / 1136 ( 68%)
............................................................ 840 / 1136 ( 73%)
............................................................ 900 / 1136 ( 79%)
............................................................ 960 / 1136 ( 84%)
............................................................ 1020 / 1136 ( 89%)
............................................................ 1080 / 1136 ( 95%)
........................................................ 1136 / 1136 (100%)
 
Ran 1136 tests from 404 test cases in 473 ms
Failed: 1, Skipped: 0
 
Summary of Errors:
 
MathsTest / Min
/Users/elliot/Development/eagle/test/eagle/MathsTest.cpp:9:
Value of: 123
Expected: Maths::Min(789, 456)
Which is: 456
main.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#include "gtest/gtest.h"
#include <cmath>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
 
using namespace testing;
 
class ConfigurableEventListener : public TestEventListener
{
protected:
TestEventListener* eventListener;
public:
unsigned int screenWidth;
unsigned int screenPos;
int totalTests;
unsigned int finishedTests;
map< string, map < string, map<string, int> > > errors;
explicit ConfigurableEventListener(TestEventListener* theEventListener) : eventListener(theEventListener)
{
screenWidth = getScreenWidth();
screenPos = 0;
totalTests = 0;
finishedTests = 0;
}
static unsigned int getScreenWidth()
{
#ifdef TIOCGSIZE
struct ttysize ts;
ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
if(ts.ts_cols > 0) {
return ts.ts_cols;
}
#elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
if(ts.ws_col > 0) {
return ts.ws_col;
}
#endif
 
// default to 80
return 80;
}
virtual ~ConfigurableEventListener()
{
}
virtual void OnTestProgramStart(const UnitTest& unit_test)
{
totalTests = unit_test.reportable_test_count();
}
virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration)
{
}
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test)
{
}
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test)
{
}
virtual void OnTestCaseStart(const TestCase& test_case)
{
}
virtual void OnTestStart(const TestInfo& test_info)
{
}
virtual void OnTestPartResult(const TestPartResult& result)
{
eventListener->OnTestPartResult(result);
}
void printResult(char ch)
{
// how many digits does it take to represent all the tests?
unsigned int dig = 1 + (unsigned int) log10((double) totalTests);
// calculate how much space we need per line
unsigned int dotWidth = screenWidth - (strlen(" ") + dig + strlen(" / ") + dig + strlen(" (100%)"));
// print result
printf("%c", ch);
++screenPos;
++finishedTests;
// reached EOL?
if(screenPos >= dotWidth) {
printf(" %*d / %*d (%3d%%)\n", dig, finishedTests, dig, totalTests, (finishedTests * 100) / totalTests);
screenPos = 0;
}
// reached end
if(finishedTests == totalTests) {
printf(" %*d / %*d (100%%)\n", dig + (dotWidth - screenPos), finishedTests, dig, totalTests);
}
}
virtual void OnTestEnd(const TestInfo& test_info)
{
if(test_info.result()->Failed()) {
printResult('F');
}
else {
printResult('.');
}
}
virtual void OnTestCaseEnd(const TestCase& test_case)
{
}
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test)
{
}
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test)
{
}
virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration)
{
printf("\n");
eventListener->OnTestIterationEnd(unit_test, iteration);
}
virtual void OnTestProgramEnd(const UnitTest& unit_test)
{
}
};
 
int main(int argc, char **argv)
{
// initialize
InitGoogleTest(&argc, argv);
// attach the custom listener
TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
TestEventListener *default_printer = listeners.Release(listeners.default_result_printer());
ConfigurableEventListener *listener = new ConfigurableEventListener(default_printer);
listeners.Append(listener);
// run
return RUN_ALL_TESTS();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.