int test::runAllTests() { const std::vector<TestCase*> &testCases = TestCase::getCases(); int passed = 0; const char *prevFixture = ""; for (int i = 0; i < testCases.size(); i++) { TestCase *tc = testCases[i]; if (strcmp(tc->fixture, prevFixture) != 0) { if (strlen(prevFixture) != 0) { cout << endl; } cout << tc->fixture << ":" << endl; prevFixture = tc->fixture; } cout << "- " << tc->name << ": " << flush; try { tc->run(); cout << "[OK]" << endl; passed++; } catch (TestFailedException &e) { cout << "[FAILED]" << endl; cout << " " << e.message << endl; cout << " (" << e.file << ":" << e.line << ")" << endl; } } cout << endl << passed << " / " << testCases.size() << " tests passed." << endl; return (passed == testCases.size() ? 0 : 1); }
Result * Suite::run(){ setupTests(); Result * r = new Result( _name ); Result::cur = r; for ( list<TestCase*>::iterator i=_tests.begin(); i!=_tests.end(); i++ ){ TestCase * tc = *i; r->_tests++; bool passes = false; log(1) << "\t" << tc->getName() << endl; try { tc->run(); passes = true; } catch ( ... ){ log() << "unknown exception in test: " << tc->getName() << endl; } if ( ! passes ) r->_fails++; } return r; }
void Runner::runTestAt( unsigned int index, TestResult &result ) const { TestCase *test = tests_[index](); result.setTestName( test->testName() ); printf( "Testing %s: ", test->testName() ); fflush( stdout ); #if JSON_USE_EXCEPTION try { #endif // if JSON_USE_EXCEPTION test->run( result ); #if JSON_USE_EXCEPTION } catch ( const std::exception &e ) { result.addFailure( __FILE__, __LINE__, "Unexpected exception caugth:" ) << e.what(); } #endif // if JSON_USE_EXCEPTION delete test; const char *status = result.failed() ? "FAILED" : "OK"; printf( "%s\n", status ); fflush( stdout ); }
Result * Suite::run(){ tlogLevel = -1; log(1) << "\t about to setupTests" << endl; setupTests(); log(1) << "\t done setupTests" << endl; Result * r = new Result( _name ); Result::cur = r; /* see note in SavedContext */ //writelock lk(""); for ( list<TestCase*>::iterator i=_tests.begin(); i!=_tests.end(); i++ ){ TestCase * tc = *i; r->_tests++; bool passes = false; log(1) << "\t going to run test: " << tc->getName() << endl; stringstream err; err << tc->getName() << "\t"; try { tc->run(); passes = true; } catch ( MyAssertionException * ae ){ err << ae->ss.str(); delete( ae ); } catch ( std::exception& e ){ err << " exception: " << e.what(); } catch ( int x ){ err << " caught int : " << x << endl; } catch ( ... ){ cerr << "unknown exception in test: " << tc->getName() << endl; } if ( ! passes ){ string s = err.str(); log() << "FAIL: " << s << endl; r->_fails++; r->_messages.push_back( s ); } } if ( r->_fails ) r->_rc = 17; log(1) << "\t DONE running tests" << endl; return r; }
int TextTestRunner::run(TestCase& testCase) { testCase.testResult.attach(this); Stopwatch sw; testCase.run(); std::cout << sw.stop() << " mus" << std::endl; testCase.testResult.detach(this); return testCase.testResult.total - testCase.testResult.successful; }
void TestSuite::run() { unsigned i; this->mAll = 0; this->mPassed = 0; for (i = 0; i < this->mTests->count(); ++i) { TestCase * ts = (TestCase*)this->mTests->objectAtIndex(i); this->mPassed += ts->run(); this->mAll += ts->getNumTest(); } }
void run_h_tc_child(void* v) { run_h_tc_data* data = static_cast< run_h_tc_data* >(v); TestCase tc; tc.init(data->m_config); tc.run("result"); std::exit(EXIT_SUCCESS); }
int main() { std::sort(TestCase::allTests().begin(), TestCase::allTests().end(), TestComparator()); DebuggingTestImpl::get()->printf("Starting tests\n"); bool anyFailed = false; for(int i = 0; i < (int)TestCase::allTests().size(); ++i) { if(i > 0) DebuggingTestImpl::get()->printf(""); TestCase *test = TestCase::allTests()[i]; string name = test->name(); DebuggingTestImpl::get()->printf("running %s", name.c_str()); bool failed = true; try { DebuggingTestImpl::get()->startTiming(name); DebuggingTestImpl::get()->indent(); test->run(); failed = false; DebuggingTestImpl::get()->unindent(); } catch(Assertion) { DebuggingTestImpl::get()->unindent(); } catch(...) { DebuggingTestImpl::get()->unindent(); DebuggingTestImpl::get()->printf("Exception thrown"); } DebuggingTestImpl::get()->elapsedTime(name); if(failed) DebuggingTestImpl::get()->printf("FAILED! %s", name.c_str()); else DebuggingTestImpl::get()->printf("passed %s", name.c_str()); anyFailed = anyFailed || failed; } return anyFailed ? 1 : 0; }
Result * Suite::run(){ setupTests(); Result * r = new Result( _name ); Result::cur = r; for ( list<TestCase*>::iterator i=_tests.begin(); i!=_tests.end(); i++ ){ TestCase * tc = *i; r->_tests++; bool passes = false; log(1) << "\t" << tc->getName() << endl; stringstream err; err << tc->getName() << "\t"; try { tc->run(); passes = true; } catch ( MyAssertionException * ae ){ err << ae->ss.str(); delete( ae ); } catch ( std::exception& e ){ err << " exception " << " : " << e.what(); } catch ( int x ){ err << " caught int : " << x << endl; } catch ( ... ){ cerr << "unknown exception in test: " << tc->getName() << endl; } if ( ! passes ){ r->_fails++; r->_messages.push_back( err.str() ); } } return r; }
/* * run the test case * return 0 for pass. -1 for fail. */ inline int runTestCase( TestCase & test, TestContext & context, std::ostream & log) { int rst = 0; try { test.run(context); log << test.name() << " Passed " << std::endl; rst = 0; } catch (XTestFailure& e) { log << outputLine(e.m_file, e.m_line) << e.what() << '\n' << "FAILED: "; rst = -1; } catch (std::exception& e) { log << outputLine(__FILE__, __LINE__) << "caught standard exception - " << e.what() << '\n' << "FAILED: "; rst = -1; } return rst; }
void TestSuite::run(TestResult *result, TestOption *opt) { list<TestCase *>::iterator iter; iter = this->tests.begin(); while (iter != this->tests.end()) { TestCase *tc = *iter; if (opt->verbose) cout << "Starting test " << tc->name << "\n"; tc->result = result; try { tc->setup(); result->n_run++; tc->statusSet = false; tc->run(); if (tc->statusSet == false) { result->n_fail++; result->tests_f.push_back( tc->name ); } } catch (TestFailure e) { TestFailure::setUnwindDone(); if (opt->verbose) cout << " caught test fail exception\n"; result->tests_e.push_back( tc->name + e.what() ); result->n_exceptions++; } if (opt->verbose) cout << " done\n"; iter++; } return; }
int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { TestCase test; return test.run(argc, argv); }
int mtsutil(int argc, char **argv) { char optchar, *end_ptr = NULL; try { /* Default settings */ int nprocs = getProcessorCount(); std::string nodeName = getHostName(), networkHosts = "", destFile=""; bool quietMode = false; ELogLevel logLevel = EInfo; FileResolver *fileResolver = Thread::getThread()->getFileResolver(); bool testCaseMode = false; if (argc < 2) { help(); return 0; } optind = 1; /* Parse command-line arguments */ while ((optchar = getopt(argc, argv, "+a:c:s:n:p:qhvt")) != -1) { switch (optchar) { case 'a': { std::vector<std::string> paths = tokenize(optarg, ";"); for (unsigned int i=0; i<paths.size(); ++i) fileResolver->addPath(paths[i]); } break; case 'c': networkHosts = networkHosts + std::string(";") + std::string(optarg); break; case 't': testCaseMode = true; break; case 's': { std::ifstream is(optarg); if (is.fail()) SLog(EError, "Could not open host file!"); std::string host; while (is >> host) { if (host.length() < 1 || host.c_str()[0] == '#') continue; networkHosts = networkHosts + std::string(";") + host; } } break; case 'n': nodeName = optarg; break; case 'v': logLevel = EDebug; break; case 'p': nprocs = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the processor count!"); break; case 'q': quietMode = true; break; case 'h': default: help(); return 0; } } /* Configure the logging subsystem */ ref<Logger> log = Thread::getThread()->getLogger(); log->setLogLevel(logLevel); /* Initialize OpenMP */ Thread::initializeOpenMP(nprocs); /* Disable the default appenders */ for (size_t i=0; i<log->getAppenderCount(); ++i) { Appender *appender = log->getAppender(i); if (appender->getClass()->derivesFrom(MTS_CLASS(StreamAppender))) log->removeAppender(appender); } log->addAppender(new StreamAppender(formatString("mitsuba.%s.log", nodeName.c_str()))); if (!quietMode) log->addAppender(new StreamAppender(&std::cout)); SLog(EInfo, "Mitsuba version %s, Copyright (c) " MTS_YEAR " Wenzel Jakob", Version(MTS_VERSION).toStringComplete().c_str()); /* Configure the scheduling subsystem */ Scheduler *scheduler = Scheduler::getInstance(); for (int i=0; i<nprocs; ++i) scheduler->registerWorker(new LocalWorker(formatString("wrk%i", i))); std::vector<std::string> hosts = tokenize(networkHosts, ";"); /* Establish network connections to nested servers */ for (size_t i=0; i<hosts.size(); ++i) { const std::string &hostName = hosts[i]; ref<Stream> stream; if (hostName.find("@") == std::string::npos) { int port = MTS_DEFAULT_PORT; std::vector<std::string> tokens = tokenize(hostName, ":"); if (tokens.size() == 0 || tokens.size() > 2) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 2) { port = strtol(tokens[1].c_str(), &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } stream = new SocketStream(tokens[0], port); } else { std::string path = "~/mitsuba"; // default path if not specified std::vector<std::string> tokens = tokenize(hostName, "@:"); if (tokens.size() < 2 || tokens.size() > 3) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 3) { path = tokens[2]; } std::vector<std::string> cmdLine; cmdLine.push_back(formatString("bash -c 'cd %s; . setpath.sh; mtssrv -ls'", path.c_str())); stream = new SSHStream(tokens[0], tokens[1], cmdLine); } try { scheduler->registerWorker(new RemoteWorker(formatString("net%i", i), stream)); } catch (std::runtime_error &e) { if (hostName.find("@") != std::string::npos) { #if defined(WIN32) SLog(EWarn, "Please ensure that passwordless authentication " "using plink.exe and pageant.exe is enabled (see the documentation for more information)"); #else SLog(EWarn, "Please ensure that passwordless authentication " "is enabled (e.g. using ssh-agent - see the documentation for more information)"); #endif } throw e; } } scheduler->start(); if (testCaseMode) { std::vector<fs::path> dirPaths = fileResolver->resolveAll("plugins"); std::set<std::string> seen; int executed = 0, succeeded = 0; for (size_t i=0; i<dirPaths.size(); ++i) { fs::path dirPath = fs::complete(dirPaths[i]); if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) break; fs::directory_iterator end, it(dirPath); for (; it != end; ++it) { if (!fs::is_regular_file(it->status())) continue; std::string extension(boost::to_lower_copy(it->path().extension())); #if defined(WIN32) if (extension != ".dll") continue; #elif defined(__OSX__) if (extension != ".dylib") continue; #elif defined(__LINUX__) if (extension != ".so") continue; #else #error Unknown operating system! #endif std::string shortName = it->path().stem(); if (seen.find(shortName) != seen.end() || !boost::starts_with(shortName, "test_")) continue; seen.insert(shortName); Plugin plugin(shortName, it->path()); if (!plugin.isUtility()) continue; ref<Utility> utility = plugin.createUtility(); TestCase *testCase = static_cast<TestCase *>(utility.get()); if (!utility->getClass()->derivesFrom(MTS_CLASS(TestCase))) SLog(EError, "This is not a test case!"); if (testCase->run(argc-optind, argv+optind) != 0) SLog(EError, "Testcase unexpectedly returned with a nonzero value."); executed += testCase->getExecuted(); succeeded += testCase->getSucceeded(); } } SLog(EInfo, "Ran %i tests, %i succeeded, %i failed.", executed, succeeded, executed-succeeded); } else { if (argc <= optind) { std::cerr << "A utility name must be supplied!" << endl; return -1; } fs::path pluginName(argv[optind]); /* Build the full plugin file name */ #if defined(WIN32) pluginName.replace_extension(".dll"); #elif defined(__OSX__) pluginName.replace_extension(".dylib"); #elif defined(__LINUX__) pluginName.replace_extension(".so"); #else #error Unknown operating system! #endif fs::path fullName = fileResolver->resolve(fs::path("plugins") / pluginName); if (!fs::exists(fullName)) { /* Plugin not found! */ SLog(EError, "Utility \"%s\" not found (run \"mtsutil\" without arguments to " "see a list of available utilities)", fullName.file_string().c_str()); } SLog(EInfo, "Loading utility \"%s\" ..", argv[optind]); Plugin *plugin = new Plugin(argv[optind], fullName); if (!plugin->isUtility()) SLog(EError, "This plugin does not implement the 'Utility' interface!"); Statistics::getInstance()->logPlugin(argv[optind], plugin->getDescription()); ref<Utility> utility = plugin->createUtility(); int retval = utility->run(argc-optind, argv+optind); scheduler->pause(); utility = NULL; delete plugin; return retval; } } catch (const std::exception &e) { std::cerr << "Caught a critical exeption: " << e.what() << std::endl; } catch (...) { std::cerr << "Caught a critical exeption of unknown type!" << endl; } return 0; }
Result * Suite::run( const string& filter ) { // set tlogLevel to -1 to suppress tlog() output in a test program tlogLevel = -1; log(1) << "\t about to setupTests" << endl; setupTests(); log(1) << "\t done setupTests" << endl; Result * r = new Result( _name ); Result::cur = r; /* see note in SavedContext */ //writelock lk(""); for ( list<TestCase*>::iterator i=_tests.begin(); i!=_tests.end(); i++ ) { TestCase * tc = *i; if ( filter.size() && tc->getName().find( filter ) == string::npos ) { log(1) << "\t skipping test: " << tc->getName() << " because doesn't match filter" << endl; continue; } r->_tests++; bool passes = false; log(1) << "\t going to run test: " << tc->getName() << endl; stringstream err; err << tc->getName() << "\t"; { scoped_lock lk(minutesRunningMutex); minutesRunning = 0; currentTestName = tc->getName(); } try { tc->run(); passes = true; } catch ( MyAssertionException * ae ) { err << ae->ss.str(); delete( ae ); } catch ( std::exception& e ) { err << " exception: " << e.what(); } catch ( int x ) { err << " caught int : " << x << endl; } catch ( ... ) { cerr << "unknown exception in test: " << tc->getName() << endl; } if ( ! passes ) { string s = err.str(); log() << "FAIL: " << s << endl; r->_fails++; r->_messages.push_back( s ); } } if ( r->_fails ) r->_rc = 17; log(1) << "\t DONE running tests" << endl; return r; }