コード例 #1
0
bool TextOutputter::write(std::string filename,
                          RTF::TestMessage* errorMsg) {
    if(filename.empty()) {
        if(errorMsg != NULL) {
            errorMsg->setMessage("Cannot open the file.");
            errorMsg->setDetail("Empty file name.");
        }
        return false;
    }

    ofstream outputter;
    outputter.open(filename.c_str());
    if (!outputter.is_open() || !outputter.good()) {
        if(errorMsg != NULL) {
            errorMsg->setMessage("Cannot open the file.");
            const char* errorStr = strerror(errno);
            errorMsg->setDetail(errorStr);
        }
        return false;
    }

    TestResultCollector::EventResultIterator itr;
    TestResultCollector::EventResultContainer events = collector.getResults();

    for(itr=events.begin(); itr!=events.end(); ++itr) {
       ResultEvent* e = *itr;

       // start suit
       if(dynamic_cast<ResultEventStartSuit*>(e))
           outputter<<"Test suit "<<e->getTest()->getName()<<" started..."<<endl;
       // end suit
       else if(dynamic_cast<ResultEventEndSuit*>(e)) {
           outputter<<"Test suit "<<e->getTest()->getName();
           if(e->getTest()->succeeded())
               outputter<<" passed!"<<endl;
           else
               outputter<<" failed!"<<endl;
       }

       // start test case
       else if(dynamic_cast<ResultEventStartTest*>(e))
           outputter<<"Test case "<<e->getTest()->getName()<<" started..."<<endl;

       // end test case
       else if(dynamic_cast<ResultEventEndTest*>(e)) {
           outputter<<"Test case "<<e->getTest()->getName();
           if(e->getTest()->succeeded())
               outputter<<" passed!"<<endl;
           else
               outputter<<" failed!"<<endl;
       }

       // report event
       else if(dynamic_cast<ResultEventReport*>(e)) {
           outputter<<MSG_REPORT<<"("<<e->getTest()->getName()<<") "
               <<e->getMessage().getMessage()<<": "<<e->getMessage().getDetail()<<endl;
           if(verbose && e->getMessage().getSourceLineNumber() != 0) {
               outputter<<e->getMessage().getSourceFileName()
                       <<" at "<<e->getMessage().getSourceLineNumber()<<"."<<endl<<endl;
           }
       }

       // failure event
       else if(dynamic_cast<ResultEventFailure*>(e)) {
           outputter<<MSG_FAIL<<"("<<e->getTest()->getName()<<") "
               <<e->getMessage().getMessage()<<": "<<e->getMessage().getDetail()<<endl;
           if(verbose && e->getMessage().getSourceLineNumber() != 0) {
               outputter<<e->getMessage().getSourceFileName()
                       <<" at "<<e->getMessage().getSourceLineNumber()<<"."<<endl<<endl;
           }
       }

       // error event
       else if(dynamic_cast<ResultEventError*>(e)) {
           outputter<<MSG_ERROR<<"("<<e->getTest()->getName()<<") "
               <<e->getMessage().getMessage()<<": "<<e->getMessage().getDetail()<<endl;
           if(verbose && e->getMessage().getSourceLineNumber() != 0) {
               outputter<<e->getMessage().getSourceFileName()
                       <<" at "<<e->getMessage().getSourceLineNumber()<<"."<<endl<<endl;
           }
       }
    } // end for

    outputter.close();
    return true;
}
コード例 #2
0
bool JUnitOutputter::write(std::string filename,
                          RTF::TestMessage* errorMsg) {
    if(filename.empty()) {
        if(errorMsg != nullptr) {
            errorMsg->setMessage("Cannot open the file.");
            errorMsg->setDetail("Empty file name.");
        }
        return false;
    }

    TiXmlDocument doc;
    TiXmlElement* root = new TiXmlElement("testsuites");
    root->SetAttribute("suites", collector.suiteCount());
    root->SetAttribute("tests", collector.testCount());
    root->SetAttribute("failures", collector.failedCount());
    doc.LinkEndChild(root);

    TiXmlElement* testsuite = nullptr;
    TiXmlElement* testcase = nullptr;
    string classname;

    // If there is not any test suite, add one!
    if(collector.suiteCount() == 0) {
        classname = "default";
        testsuite = new TiXmlElement("testsuite");
        testsuite->SetAttribute("name", classname.c_str());
        root->LinkEndChild(testsuite);
    }

    TestResultCollector::EventResultIterator itr;
    TestResultCollector::EventResultContainer events = collector.getResults();

    string errorMessages, failureMessages, reportsMessages;
    for(itr=events.begin(); itr!=events.end(); ++itr) {
       ResultEvent* e = *itr;

       // start suit
       if(dynamic_cast<ResultEventStartSuite*>(e)) {
           classname = e->getTest()->getName();
           testsuite = new TiXmlElement("testsuite");
           testsuite->SetAttribute("name", classname.c_str());
           root->LinkEndChild(testsuite);
       }
       // end suit
       //else if(dynamic_cast<ResultEventEndSuite*>(e)) { }

       // start test case
       else if(dynamic_cast<ResultEventStartTest*>(e)) {
           if(testsuite == nullptr) continue;
           testcase = new TiXmlElement("testcase");
           testcase->SetAttribute("name", e->getTest()->getName());
           testcase->SetAttribute("classname", classname+"."+e->getTest()->getName());
           testsuite->LinkEndChild(testcase);
           errorMessages.clear();
           failureMessages.clear();
           reportsMessages.clear();
       }

       // end test case
       else if(dynamic_cast<ResultEventEndTest*>(e)) {
           if(testcase == nullptr) continue;
           // adding falures
           if(failureMessages.size()) {
               TiXmlElement * failure = new TiXmlElement("failure");
               failure->SetAttribute("message", "Something went wrong. See Stacktrace for detail.");
               failure->LinkEndChild(new TiXmlText(failureMessages.c_str()));
               testcase->LinkEndChild(failure);
            }

           // adding errors
           if(errorMessages.size()){
               TiXmlElement * error = new TiXmlElement("error");
               error->SetAttribute("message", "Something went wrong. See Stacktrace for detail.");
               error->LinkEndChild(new TiXmlText(errorMessages.c_str()));
               testcase->LinkEndChild(error);
            }

           // adding reports
           if(reportsMessages.size()) {
               TiXmlElement * report = new TiXmlElement("system-out");
               report->SetAttribute("message", "See Standard Output for detail.");
               report->LinkEndChild(new TiXmlText(reportsMessages.c_str()));
               testcase->LinkEndChild(report);
            }
       }

       // failure event
       else if(dynamic_cast<ResultEventFailure*>(e)) {
           string msg;
           if(verbose) {
                msg = Asserter::format("%s : %s (%s at %d) <br>",
                                         e->getMessage().getMessage().c_str(),
                                         e->getMessage().getDetail().c_str(),
                                         e->getMessage().getSourceFileName().c_str(),
                                         e->getMessage().getSourceLineNumber());
           }
           else
               msg = e->getMessage().getMessage() + ": " + e->getMessage().getDetail() + "\n";
            failureMessages += msg;
            reportsMessages += MSG_FAIL + e->getTest()->getName()+ ") " + msg;
       }

       // error event
       else if(dynamic_cast<ResultEventError*>(e)) {
           string msg;
           if(verbose) {
                msg = Asserter::format("%s : %s (%s at %d) <br>",
                                         e->getMessage().getMessage().c_str(),
                                         e->getMessage().getDetail().c_str(),
                                         e->getMessage().getSourceFileName().c_str(),
                                         e->getMessage().getSourceLineNumber());
           }
           else
               msg = e->getMessage().getMessage() + ": " + e->getMessage().getDetail() + "\n";
            errorMessages += msg;
            reportsMessages += MSG_ERROR + e->getTest()->getName()+ ") " + msg;
       }

       // report event
       else if(dynamic_cast<ResultEventReport*>(e)) {
           string msg;
           if(verbose) {
                msg = Asserter::format("%s : %s (%s at %d) <br>",
                                         e->getMessage().getMessage().c_str(),
                                         e->getMessage().getDetail().c_str(),
                                         e->getMessage().getSourceFileName().c_str(),
                                         e->getMessage().getSourceLineNumber());
           }
           else
               msg = e->getMessage().getMessage() + ": " + e->getMessage().getDetail() + "\n";
            reportsMessages += MSG_REPORT + e->getTest()->getName()+ ") " + msg;
       }

    } // end for

    if(!doc.SaveFile(filename.c_str())) {
        if(errorMsg != nullptr) {
            errorMsg->setMessage("Cannot write to the " + filename);
            if (doc.Error())
                errorMsg->setDetail(Asserter::format("%s (line: %d, column %d)", doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol()));
        }
        return false;
    }
    return true;
}