A simple testing and logging framework in c++

95 views
Skip to first unread message

Dilawar

unread,
May 20, 2014, 1:06:37 PM5/20/14
to wncc...@googlegroups.com
This is a pre-release version: posted here for comments and contribution. The project is hosted at https://github.com/dilawar/simpletest.

It provides few macros for testing purpose. The behaviour of these macros are like google-test macros with one modification: in google-test, one writes,

 EXPECT_EQ(2, 3) << "This must fail for sure";

In this framework, one writes,

  EXPECT_EQ(2, 3, "This must fail for sure");

This is not as clean as google-test but it enables me to write most of my macros in few header-files. The third argument is of `ostreamstring1` type. Therefore, following is also possible.
int expected = 2;
int provided = 3;
EXPECT_EQ
(2, 3, "I was expecting " << expected << " but got " << provided ");

One needs to overload `<<` operator and a clas oobject can aslo be passed to this function.

By default colored output is disabled. It can be enabled by enclosing the message by backtick (`). One should try to look at the `test1.cpp` file in `tests`` directory.

LOGGING:

For logging, two functions are provided, one is `dump` and second is `log`. Function `dump` writes to terminal and `log` writes to a log file `__simpletest__.log`. There are defined in file `print_functions.hpp` file. They are used in above macros. Their implementation can change therefore they are documented. Function `log` also redirectes its input to console with color-support but remove colors while logging to file (to avoid dirty looking color escape strings).

A snippet of test:

#include "../simple_test.hpp"


#include <iostream>
using namespace std;


int main()
{
   
/* Following two macros takes two arguments: first is condition and second
 * is message to print on console in case of failture
 */

    EXPECT_TRUE
( 1 == 2, "1 == 2 must fail");


   
/* TO instroduce colors into user-messages, sorround them with ` */
    EXPECT_TRUE
( 1 == 2, "`1 == 2 must fail`");


    EXPECT_FALSE
( 2 != 2, "2 != 2 must also fail");




   
/* Following macros takes three argument and they try to mimic GTEST macros. */
    EXPECT_EQ
(2, 3, "This should fail");


   
/* This is colored. */
    EXPECT_NEQ
(2, 2, "`I was expecting not equal to 2`");


   
int expected = 3;
    EXPECT_GTE
(2, expected, "I was expecting greater than equal to" << expected );
    EXPECT_GT
(3, expected, "I was expecting greater than " << expected);


    EXPECT_LTE
(5, expected, "I was expecting less than equal to " << expected);
    EXPECT_LT
(3, expected, "I was expecting less than "<< expected);


   
/* If a marco begins with ASSERT_ rather than EXPECT_ then it throws an
 * exception.
 */

    ASSERT_LT
(3, expected, "I was expecting less than " << expected );


   
return 0;
}




SAMPLE OUTPUT when compiling and running `tests/test1.cpp` file.

[EXPECT_FAILURE] 1 == 2 must fail
 
+
[EXPECT_FAILURE] In function: int main() file: test1.cpp:34
 
+ Expected 2, received 3
 
+ This should fail
 
+
[EXPECT_FAILURE] In function: int main() file: test1.cpp:35
 
+ Not expected 2
 
+ I was expecting not equal to 2
 
+
[EXPECT_FAILURE] In function: int main() file: test1.cpp:38
 
+ Expected greater than or equal to 2, received 3
 
+ I was expecting greater than equal to3
 
+
[EXPECT_FAILURE] In function: int main() file: test1.cpp:39
 
+ Expected greater than 3, received 3
 
+ I was expecting greater than 3
 
+
[EXPECT_FAILURE] In function: int main() file: test1.cpp:42
 
+ Expected less than 3, received 3
 
+ I was expecting less than 3
 
+
[EXPECT_FAILURE] In function: int main() file: test1.cpp:47
 
+ Expected less than 3, received 3
 
+ I was expecting less than 3
 
+
[ASSERTION_FAILURE] I was expecting less than 3
terminate called after throwing an instance of
'FatalTestFailure'
/bin/bash: line 1: 30260 Aborted                 ./a.out


ACKNOWLEDGEMENT:

Some macors used and modified here such as `SIMPLE_ASSERT` and `SIMPLE_ASSERT_MSG` were part of `boose/assert.hpp` file. THey were modified and used first in `moose.ncbs.res.in` simulator and ported to this library. The file `assert.hpp` is distributed under boost-license. Rest of the code is distributed under GNU_GPL.



Reply all
Reply to author
Forward
0 new messages