opentestadaptor.h
Go to the documentation of this file.00001 #ifndef CPPUT_OPENTESTADAPTOR_H_INCLUDED
00002 # define CPPUT_OPENTESTADAPTOR_H_INCLUDED
00003
00004 # include <cpput/testcase.h>
00005 # include <opentest/connector.h>
00006 # include <opentest/interfaces.h>
00007
00008 namespace CppUT {
00009
00010 class OpenTestAdaptor : public OpenTest::TestRunnerInterface
00011 , private TestResultUpdater
00012 {
00013 public:
00014 OpenTestAdaptor()
00015 : currentTestPlanId_( 0 )
00016 {
00017 }
00018
00019 virtual ~OpenTestAdaptor()
00020 {
00021 }
00022
00023 void addTest( const TestPtr &test )
00024 {
00025 tests_.push_back( test );
00026 }
00027
00028 public:
00029
00030 virtual void getTestDescriptions()
00031 {
00032 OpenTest::TestDescriptions descriptions;
00033 for ( Tests::const_iterator it = tests_.begin(); it != tests_.end(); ++it )
00034 {
00035 addTestDescriptions( *it, descriptions );
00036 }
00037 connector().testDriver().setTestDescriptions( descriptions );
00038 }
00039
00040 OpenTest::TestId allocateTestId( const TestPtr &test )
00041 {
00042 OpenTest::TestId testId;
00043 IdsByTest::iterator it = idsByTest_.find( test );
00044 if ( it == idsByTest_.end() )
00045 {
00046 testId = OpenTest::TestId( idsByTest_.size() + 1 );
00047 idsByTest_.insert( IdsByTest::value_type( test, testId ) );
00048 testsById_.insert( TestsById::value_type( testId, test ) );
00049 }
00050 else
00051 testId = it->second;
00052 return testId;
00053 }
00054
00055 void setCommonTestDescription( const TestPtr &test,
00056 OpenTest::TestId testId,
00057 OpenTest::TestDescriptionCommon &description )
00058 {
00059 description.name_ = test->name();
00060 description.description_ = test->description();
00061
00062
00063 }
00064
00065 void addTestDescriptions( const TestPtr &test,
00066 OpenTest::TestDescriptions &descriptions )
00067 {
00068 OpenTest::TestId testId = allocateTestId( test );
00069
00070 if ( test->isTestSuite() )
00071 {
00072 OpenTest::TestSuiteDescription suiteDescription;
00073 setCommonTestDescription( test, testId, suiteDescription );
00074 const AbstractTestSuite *suite = static_cast<AbstractTestSuite*>( test.get() );
00075 for ( int index = 0; index < suite->testCount(); ++index )
00076 {
00077 TestPtr childTest = suite->testAt(index);
00078 addTestDescriptions( childTest, descriptions );
00079 suiteDescription.children_.push_back( allocateTestId( childTest ) );
00080 }
00081 descriptions.testSuites_.insert( OpenTest::TestDescriptions::TestSuites::value_type( testId, suiteDescription ) );
00082 }
00083 else
00084 {
00085 OpenTest::TestCaseDescription testCaseDescription;
00086 setCommonTestDescription( test, testId, testCaseDescription );
00087 descriptions.testCases_.insert( OpenTest::TestDescriptions::TestCases::value_type( testId, testCaseDescription ) );
00088 }
00089 }
00090
00091 virtual void getTestPlans()
00092 {
00093 OpenTest::TestPlans plans;
00094 for ( TestsById::iterator it = testsById_.begin(); it != testsById_.end(); ++it )
00095 {
00096 if ( it->second->isTestCase() )
00097 {
00098 OpenTest::TestPlan plan;
00099 plan.testCase_ = it->first;
00100 plans.testPlans_.push_back( plan );
00101 }
00102 }
00103 connector().testDriver().setDefaultTestPlans( plans );
00104 }
00105
00106 virtual void runTests( const OpenTest::TestPlans &plans )
00107 {
00108 TestInfo::threadInstance().setTestResultUpdater( *this );
00109 for ( OpenTest::TestPlans::Plans::const_iterator it = plans.testPlans_.begin(); it != plans.testPlans_.end(); ++it )
00110 {
00111 const OpenTest::TestPlan &plan = *it;
00112 TestsById::const_iterator itTest = testsById_.find( plan.testCase_ );
00113 if ( itTest != testsById_.end() )
00114 {
00115 currentTestPlanId_ = OpenTest::TestPlanId( it - plans.testPlans_.begin() );
00116 TestPtr test = itTest->second;
00117 if ( test->isTestCase() )
00118 {
00119 AbstractTestCase &testCase = static_cast<AbstractTestCase &>(*test);
00120 connector().testDriver().startTesting( currentTestPlanId_ );
00121 testCase.runTest();
00122 publishTestResult();
00123 }
00124 }
00125 }
00126 TestInfo::threadInstance().removeTestResultUpdater();
00127 }
00128
00129 void publishTestResult()
00130 {
00131 OpenTest::ResultStatus status;
00132 const TestStatus &actualStatus = TestInfo::threadInstance().testStatus();
00133 switch ( actualStatus.status() )
00134 {
00135 case TestStatus::passed:
00136 status.status_ = "passed";
00137 break;
00138 case TestStatus::failed:
00139 status.status_ = "failed";
00140 break;
00141 case TestStatus::skipped:
00142 status.status_ = "skipped";
00143 break;
00144 default:
00145 CPPTL_DEBUG_ASSERT_UNREACHABLE;
00146 }
00147 status.statistics_["assertionCount"] = actualStatus.assertionCount();
00148 status.statistics_["failedAssertionCount"] = actualStatus.failedAssertionCount();
00149 status.statistics_["ignoredFailureCount"] = actualStatus.ignoredFailureCount();
00150 connector().testDriver().setTestResult( currentTestPlanId_, status );
00151 }
00152
00153 virtual void stopTests()
00154 {
00155 }
00156
00157 private:
00158 virtual void addResultLog( const Json::Value &log )
00159 {
00160 OpenTest::ResultLog result;
00161 result.log_ = log;
00162 connector().testDriver().addResultLog( currentTestPlanId_, result );
00163 }
00164
00165 virtual void addResultAssertion( const Assertion &assertion )
00166 {
00167 OpenTest::ResultAssertion result;
00168 result.assertionType_ = (assertion.kind() == Assertion::fault) ? "fault"
00169 : "assertion";
00170 result.assertionFormat_ = "generic";
00171 result.message_ = assertion.messages().toString();
00172 result.specific_ = assertion.testData();
00173 result.isIgnoredFailure_ = assertion.isIgnoredFailure();
00174 connector().testDriver().addResultAssertion( currentTestPlanId_, result );
00175 }
00176
00177 private:
00178 typedef std::map<OpenTest::TestId,TestPtr> TestsById;
00179 typedef std::map<TestPtr,OpenTest::TestId> IdsByTest;
00180 typedef std::deque<TestPtr> Tests;
00181 Tests tests_;
00182 OpenTest::TestPlanId currentTestPlanId_;
00183 TestsById testsById_;
00184 IdsByTest idsByTest_;
00185 };
00186 }
00187
00188
00189 #endif // CPPUT_OPENTESTADAPTOR_H_INCLUDED