单元测试3

        看了《Think in C++ Volume 2》第2章中Cppunit基本框架之后,想起自己之前总结的“ 单元测试2”,便将源码拿过来扩展一下,再次体会了Cppunit的基本思想。(有空继续扩展)
        同时,阅读《How Google Test Software》时,里面的一句话让我反思软件测试的意义。
        Quality is not equal to test. Quality is achieved by putting development and testing into a blender and mixing them until one is indistinguishable from the other.

        软件质量不等于测试;软件质量是通过将开发和测试混为一体,直到无法区分彼此。

#include <iostream>
using namespace std;

//最大测试用例数
#define TEST_CASE_MAX 100

//测试检测点
#define CHECK_TRUE(value)                                                           \
  do                                                                                \
  {                                                                                 \
    if(!value)                                                                      \
    {                                                                               \
	  onCheckFailure(__results__);                                                  \
	  __reporter__.failedReport(#value, __FILE__, __LINE__);                        \
    }                                                                               \
  } while (false)

//测试结果类
class TestResults
{
public:
  TestResults():testCount_(0),testFailureCount_(0)
  {
  }
  void testFailureCount()
  {
    testFailureCount_++;
  }
  void testCount()
  {
    testCount_++;
  }
  int getTestCount()
  {
    return testCount_;
  }
  int getFailureCount()
  {
    return testFailureCount_;
  }  
private:
  //测试总数
  int testCount_;
  //测试失败数
  int testFailureCount_;  
};

//测试报告类
class TestReporter
{
    public:
	void testReport(TestResults& report)
	{
	  	cout<<"done! (Run:"<<report.getTestCount()<<", Failed:"<<report.getFailureCount()<<")"<<endl;
	}
	void failedReport(const std::string& checkValue, const char* fileName, long lineNum) 
	{
        cout << "Failure: (" << checkValue << ") , " << fileName << " (line " << lineNum << ")" << endl;	
	}

};

//测试接口类
class Test 
{
  public:
  //run()分别被TestCase和TestSuite覆盖,run的调用可能是TestCase或TestSuite
  virtual void run(TestResults& testResults_, TestReporter& testReporter_) = 0;  
};

//“测试用例”类
class TestCase:public Test 
{
public:
  void run(TestResults& testResults_, TestReporter& testReporter_)
  {
    cout<<"\n=========== Running test ==========="<<endl;
    testResults_.testCount();

	runImpl(testResults_, testReporter_);
	
	//report
	testReporter_.testReport(testResults_);
  }

  void onCheckFailure(TestResults& results)
  {
    results.testFailureCount();
  }
  virtual void runImpl(TestResults& testResults_, TestReporter& testReporter_) = 0;
};

//“测试套”类
class TestSuite:public Test 
{
  public:
  void run(TestResults& testResults_, TestReporter& testReporter_)
  {
    Test * currTest_p;
    for(int i = 0; i<testIndex; i++)
    {
      currTest_p = testList[i];
	  //如果是TestSuite,继续递归执行;否则,执行TestCase
	  currTest_p->run(testResults_, testReporter_);
    }
  }
  //addTest,可以是TestCase,也可以使TestSuite
  void addTest(Test &test)
  {
    if(testIndex<TEST_CASE_MAX)
    {
      testList[testIndex] = &test;
	  testIndex++;
    }
  }

  private:	
  //测试用例数组
  Test* testList[TEST_CASE_MAX];
  int  testIndex;
};

//测试控制类
class TestController
{
public:
  void run(Test& test)
  {
    test.run(testResults_,testReporter_);
  }
private:  
  TestResults testResults_;
  TestReporter testReporter_;
};


//(可扩展)宏定义测试用例类
#define TEST_CASE(Name)                                                             \
  class Test##Name : public TestCase                                                                      \
  {                                                                                                       \
    public:                                                                                               \
      Test##Name()                                                                                        \
      {                                                                                                   \
      }                                                                                                   \
      virtual ~Test##Name()                                                                               \
      {                                                                                                   \
      }                                                                                                   \
      void runImpl(TestResults& __results__, TestReporter& __reporter__);                                                                                     \
  } Name;                                                                                                 \
  void Test##Name::runImpl(TestResults& __results__, TestReporter& __reporter__)

//(可扩展)测试套声明类
#define DECLARE_TEST_SUITE(Name)                                      \
  class Test##Name: public TestSuite                                  \
  {                                                                   \
    public:                                                           \
      Test##Name()                                                    \
      {                                                               \
      }                                                               \
      virtual ~Test##Name()                                           \
      {                                                               \
      }                                                               \
      void construct();                                               \
  };                                                                  \
  extern Test##Name Name  
  
//(可扩展)测试套实现类
#define TEST_SUITE(Name)       \
  Test##Name Name;             \
  void Test##Name::construct() 

  
//测试目标类  
class myApp
{
public:
  void testApp()
  {
    cout<<"run application ..."<<endl;
  }
};
	 
	 
//测试用例,实际就是实现了类TestmyTestCase的runImpl()
TEST_CASE(myTestCase)
{
  cout<<"Hello, myTestCase"<<endl;
  myApp app;
  app.testApp();
  CHECK_TRUE(0);
}
//(添加)测试用例2
TEST_CASE(myTestCase2)
{
  cout<<"Hello, myTestCase2"<<endl;
}
//(添加)测试用例3
TEST_CASE(myTestCase3)
{
  cout<<"Hello, myTestCase3"<<endl;
  CHECK_TRUE(1);
}
//(添加)测试用例4
TEST_CASE(myTestCase4)
{
  cout<<"Hello, myTestCase4"<<endl;
}
//(添加)测试用例 ......


//测试套,实际就是实现了类TestmyTestSuite的construct()
DECLARE_TEST_SUITE(myTestSuite);
TEST_SUITE(myTestSuite)
{
  addTest(myTestCase);
  addTest(myTestCase3);
}
//(添加)测试套2
DECLARE_TEST_SUITE(myTestSuite2);
TEST_SUITE(myTestSuite2)
{
  addTest(myTestCase2);
  addTest(myTestCase4);
}
//(添加)测试套 ......


//合并测试套
DECLARE_TEST_SUITE(tests);
TEST_SUITE(tests)
{
  myTestSuite.construct();
  addTest(myTestSuite);
  
  myTestSuite2.construct();
  addTest(myTestSuite2);
}

int main()
{
  //构造用例
  tests.construct();
  
  //通过控制类来测试
  TestController controller;
  controller.run(tests);
}


你可能感兴趣的:(单元测试3)