Advertisement
rplantiko

A tiny test framework

Oct 22nd, 2011
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include "tests.hpp"
  2. // -------------------------------------------------------------------------
  3. // Runs the run() method of all registered subclasses of Test
  4. //
  5. // The output is formatted in the simple Test Anything Protocol (TAP), see
  6. //     http://testanything.org/wiki/index.php/Main_Page
  7. //
  8. //  Example output
  9. //
  10. //  1..2
  11. //  ok 1 - (TestNoFnordsAround)
  12. //  not ok 2 - Wrong Wizzle Number: Expected 9, actual 7 (TestWizzleFizzle)
  13. //  # This is a comment
  14. //
  15. // Uses <typeinfo>, boost::regex and boost::program_options
  16. // The regex is used in the command line for filtering the tests to run
  17. // typeinfo is used for obtaining the test class name at runtime.
  18. //
  19. // -------------------------------------------------------------------------
  20.  
  21.  
  22. // -------------------------------------------------------------------------
  23. // Implementation of tests
  24. // -------------------------------------------------------------------------
  25.  
  26. /* Example of a failing test
  27.    to be excluded when the test suite will be used later */
  28. class TestTestNotOK : public Test {
  29.   public:
  30.     virtual void run() {
  31.       throw "Example";
  32.       };
  33.   };
  34.  
  35. /* --- Further tests follow here:
  36.  
  37.    class Test... : public Test {
  38.       public :
  39.         virtual void run() {
  40.            }
  41.      };
  42.  
  43. */
  44.  
  45.  
  46. // -------------------------------------------------------------------------
  47. // main() - Reads command line arguments & executes the tests
  48. // -------------------------------------------------------------------------
  49. int main( int argc, char *argv[] ) {
  50.  
  51.   TestRunner tr;
  52.  
  53.   tr.getOptions(argc,argv);
  54.  
  55.   tr << new TestTestNotOK( );
  56.  
  57.   bool ok = tr.run();
  58.   return ok ? 0 : -1;
  59.  
  60.  }
  61.  
  62.  
  63. // -------------------------------------------------------------------------
  64. // The tiny test framework
  65. // -------------------------------------------------------------------------
  66.  
  67. // --- Determine the current class Name
  68. const char* Test::className( ) {
  69.   return typeid(*this).name();
  70.   }
  71.  
  72. void Test::setup() {
  73.   }
  74.  
  75.  
  76. // --- TestRunner Implementation
  77. bool TestRunner::run() {
  78.   int n = 0;
  79.   int n_ok = 0;
  80.   list<Test*>::iterator i;
  81.   printf("%d..%d\n",tests.size()?1:0,tests.size());
  82.   for (i=tests.begin();i!=tests.end();i++) {
  83.     n++;
  84.     (*i)->setup();
  85.     try {
  86.       (*i)->run();
  87.       printf("ok %d -",n);
  88.       n_ok++;
  89.       } catch (const char* f) {
  90.           printf("not ok %d - %s", n, f);
  91.           };
  92.     printf(" (%s)\n", (*i)->className());
  93.     (*i)->teardown();
  94.     }
  95.   printf("\n");
  96.   return (n_ok == n);
  97.   }
  98.  
  99. bool TestRunner::getOptions(int argc, char* argv[]) {
  100.  
  101.   string pat;
  102.  
  103. // Declare the supported options ( opt = boost::program_options )
  104.   opt::options_description desc("Allowed options");
  105.   desc.add_options()
  106.     ("help", "produce help message")
  107.     ("pattern,p", opt::value<string>(&pat), "regex pattern to filter classes to be executed");
  108.  
  109.   opt::variables_map varMap;
  110.   opt::store(opt::parse_command_line(argc, argv, desc), varMap);
  111.   opt::notify(varMap);    
  112.  
  113.   if (varMap.count("help")) {
  114.     cout << desc << "\n";
  115.     return false;
  116.   }
  117.  
  118.   pattern = varMap.count("pattern") ?
  119.               new boost::regex( pat ) : 0;
  120.              
  121.   if (pattern) cout << pat;            
  122.  
  123.   return true;
  124.  
  125.   }  
  126.  
  127. TestRunner::~TestRunner() {
  128.   if (pattern) delete pattern;
  129.   }  
  130.  
  131. TestRunner& operator<< (TestRunner& tr, Test* t) {
  132.   if (    tr.pattern == 0
  133.        || boost::regex_search( t->className(), *(tr.pattern)))
  134.     tr.tests.push_back(t);
  135.    
  136.   return tr;
  137.  
  138.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement