TestRunner.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /*
  2.  * A command line based tool to run tests.
  3.  * TestRunner expects as its only argument the name of a TestCase class.
  4.  * TestRunner prints out a trace as the tests are executed followed by a
  5.  * summary at the end.
  6.  *
  7.  * You can add to the tests that the TestRunner knows about by 
  8.  * making additional calls to "addTest (...)" in main.
  9.  *
  10.  * Here is the synopsis:
  11.  *
  12.  * TestRunner [-wait] ExampleTestCase
  13.  *
  14.  */
  15. #include <iostream>
  16. #include <vector>
  17. #include "TextTestResult.h"
  18. #include "ExampleTestCase.h"
  19. using namespace std;
  20. typedef pair<string, Test *>           mapping;
  21. typedef vector<pair<string, Test *> >   mappings;
  22. class TestRunner
  23. {
  24. protected:
  25.     bool                                m_wait;
  26.     vector<pair<string,Test *> >        m_mappings;
  27. public:
  28.             TestRunner    () : m_wait (false) {}
  29.                 ~TestRunner   ();
  30.     void        run           (int ac, char **av);
  31.     void        addTest       (string name, Test *test)
  32.     { m_mappings.push_back (mapping (name, test)); }
  33. protected:
  34.     void        run (Test *test);
  35.     void        printBanner ();
  36. };
  37. void TestRunner::printBanner ()
  38. {
  39.     cout << "Usage: driver [ -wait ] testName, where name is the name of a test case class" << endl;
  40. }
  41. void TestRunner::run (int ac, char **av)
  42. {
  43.     string  testCase;
  44.     int     numberOfTests = 0;
  45.     for (int i = 1; i < ac; i++) {
  46.         if (string (av [i]) == "-wait") {
  47.             m_wait = true;
  48.             continue;
  49.         }
  50.         testCase = av [i];
  51.         if (testCase == "") {
  52.             printBanner ();
  53.             return;
  54.         }
  55.         Test *testToRun = NULL;
  56.         for (mappings::iterator it = m_mappings.begin ();
  57.                 it != m_mappings.end ();
  58.                 ++it) {
  59.             if ((*it).first == testCase) {
  60.                 testToRun = (*it).second;
  61.                 run (testToRun);
  62.             }
  63.         }
  64.         numberOfTests++;
  65.         if (!testToRun) {
  66.             cout << "Test " << testCase << " not found." << endl;
  67.             return;
  68.         } 
  69.     }
  70.     if (numberOfTests == 0) {
  71.         printBanner ();
  72.         return;        
  73.     }
  74.     if (m_wait) {
  75.         cout << "<RETURN> to continue" << endl;
  76.         cin.get ();
  77.     }
  78. }
  79. TestRunner::~TestRunner ()
  80. {
  81.     for (mappings::iterator it = m_mappings.begin ();
  82.              it != m_mappings.end ();
  83.              ++it)
  84.         delete it->second;
  85. }
  86. void TestRunner::run (Test *test)
  87. {
  88.     TextTestResult  result;
  89.     test->run (&result);
  90.     cout << result << endl;
  91. }
  92. int main (int ac, char **av)
  93. {
  94.     TestRunner runner;
  95.     runner.addTest ("ExampleTestCase", ExampleTestCase::suite ());
  96.     runner.run (ac, av);
  97.     return 0;
  98. }