Allow running individual unit tests

This commit is contained in:
Vitaliy 2023-06-25 12:13:48 +03:00 committed by GitHub
parent aada2403c9
commit 2f6a9d12f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 6 deletions

@ -217,7 +217,10 @@ int main(int argc, char *argv[])
// Run unit tests // Run unit tests
if (cmd_args.getFlag("run-unittests")) { if (cmd_args.getFlag("run-unittests")) {
#if BUILD_UNITTESTS #if BUILD_UNITTESTS
return run_tests(); if (cmd_args.exists("test-module"))
return run_tests(cmd_args.get("test-module")) ? 0 : 1;
else
return run_tests() ? 0 : 1;
#else #else
errorstream << "Unittest support is not enabled in this binary. " errorstream << "Unittest support is not enabled in this binary. "
<< "If you want to enable it, compile project with BUILD_UNITTESTS=1 flag." << "If you want to enable it, compile project with BUILD_UNITTESTS=1 flag."
@ -327,6 +330,8 @@ static void set_allowed_options(OptionList *allowed_options)
_("Run the unit tests and exit")))); _("Run the unit tests and exit"))));
allowed_options->insert(std::make_pair("run-benchmarks", ValueSpec(VALUETYPE_FLAG, allowed_options->insert(std::make_pair("run-benchmarks", ValueSpec(VALUETYPE_FLAG,
_("Run the benchmarks and exit")))); _("Run the benchmarks and exit"))));
allowed_options->insert(std::make_pair("test-module", ValueSpec(VALUETYPE_STRING,
_("Only run the specified test module"))));
allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING, allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
_("Same as --world (deprecated)")))); _("Same as --world (deprecated)"))));
allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING, allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,

@ -226,12 +226,12 @@ bool run_tests()
u32 num_total_tests_failed = 0; u32 num_total_tests_failed = 0;
u32 num_total_tests_run = 0; u32 num_total_tests_run = 0;
std::vector<TestBase *> &testmods = TestManager::getTestModules(); std::vector<TestBase *> &testmods = TestManager::getTestModules();
for (size_t i = 0; i != testmods.size(); i++) { for (auto *testmod: testmods) {
if (!testmods[i]->testModule(&gamedef)) if (!testmod->testModule(&gamedef))
num_modules_failed++; num_modules_failed++;
num_total_tests_failed += testmods[i]->num_tests_failed; num_total_tests_failed += testmod->num_tests_failed;
num_total_tests_run += testmods[i]->num_tests_run; num_total_tests_run += testmod->num_tests_run;
} }
u64 tdiff = porting::getTimeMs() - t1; u64 tdiff = porting::getTimeMs() - t1;
@ -251,7 +251,49 @@ bool run_tests()
<< "++++++++++++++++++++++++++++++++++++++++" << "++++++++++++++++++++++++++++++++++++++++"
<< "++++++++++++++++++++++++++++++++++++++++" << std::endl; << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
return num_modules_failed; return num_modules_failed == 0;
}
static TestBase *findTestModule(const std::string &module_name) {
std::vector<TestBase *> &testmods = TestManager::getTestModules();
for (auto *testmod: testmods) {
if (module_name == testmod->getName())
return testmod;
}
return nullptr;
}
bool run_tests(const std::string &module_name)
{
TestGameDef gamedef;
auto testmod = findTestModule(module_name);
if (!testmod) {
errorstream << "Test module not found: " << module_name << std::endl;
return 1;
}
g_logger.setLevelSilenced(LL_ERROR, true);
u64 t1 = porting::getTimeMs();
bool ok = testmod->testModule(&gamedef);
u64 tdiff = porting::getTimeMs() - t1;
g_logger.setLevelSilenced(LL_ERROR, false);
const char *overall_status = ok ? "PASSED" : "FAILED";
rawstream
<< "++++++++++++++++++++++++++++++++++++++++"
<< "++++++++++++++++++++++++++++++++++++++++" << std::endl
<< "Unit Test Results: " << overall_status << std::endl
<< " " << testmod->num_tests_failed << " / "
<< testmod->num_tests_run << " failed tests." << std::endl
<< " Testing took " << tdiff << "ms." << std::endl
<< "++++++++++++++++++++++++++++++++++++++++"
<< "++++++++++++++++++++++++++++++++++++++++" << std::endl;
return ok;
} }
//// ////

@ -140,3 +140,4 @@ extern content_t t_CONTENT_LAVA;
extern content_t t_CONTENT_BRICK; extern content_t t_CONTENT_BRICK;
bool run_tests(); bool run_tests();
bool run_tests(const std::string &module_name);