Last active
January 3, 2016 18:29
-
-
Save elliotchance/8502845 to your computer and use it in GitHub Desktop.
Use the pretty dot-formatted unit test output for googletest like PHPUnit / rspec.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment