66 lines
2.0 KiB
C++
66 lines
2.0 KiB
C++
|
|
/**
|
||
|
|
* @file test_framework.cpp
|
||
|
|
* @brief 测试框架实现
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "test_framework.h"
|
||
|
|
|
||
|
|
namespace extra2d {
|
||
|
|
namespace test {
|
||
|
|
|
||
|
|
static TestStats g_stats;
|
||
|
|
|
||
|
|
std::vector<TestCase>& getTestCases() {
|
||
|
|
static std::vector<TestCase> cases;
|
||
|
|
return cases;
|
||
|
|
}
|
||
|
|
|
||
|
|
TestRegistrar::TestRegistrar(const std::string& suite, const std::string& name, TestFunc func) {
|
||
|
|
getTestCases().push_back({name, suite, func});
|
||
|
|
}
|
||
|
|
|
||
|
|
int runAllTests() {
|
||
|
|
auto& cases = getTestCases();
|
||
|
|
std::cout << "========================================\n";
|
||
|
|
std::cout << "Running " << cases.size() << " tests...\n";
|
||
|
|
std::cout << "========================================\n\n";
|
||
|
|
|
||
|
|
std::string currentSuite;
|
||
|
|
|
||
|
|
for (const auto& tc : cases) {
|
||
|
|
if (tc.suite != currentSuite) {
|
||
|
|
currentSuite = tc.suite;
|
||
|
|
std::cout << "\n[" << currentSuite << "]\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
auto start = std::chrono::high_resolution_clock::now();
|
||
|
|
|
||
|
|
try {
|
||
|
|
tc.func();
|
||
|
|
auto end = std::chrono::high_resolution_clock::now();
|
||
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||
|
|
|
||
|
|
std::cout << " [PASS] " << tc.name << " (" << duration.count() << "ms)\n";
|
||
|
|
g_stats.passed++;
|
||
|
|
} catch (const std::exception& e) {
|
||
|
|
auto end = std::chrono::high_resolution_clock::now();
|
||
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||
|
|
|
||
|
|
std::cout << " [FAIL] " << tc.name << " (" << duration.count() << "ms)\n";
|
||
|
|
std::cout << " Error: " << e.what() << "\n";
|
||
|
|
g_stats.failed++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::cout << "\n========================================\n";
|
||
|
|
std::cout << "Results: " << g_stats.passed << " passed, "
|
||
|
|
<< g_stats.failed << " failed, "
|
||
|
|
<< g_stats.skipped << " skipped\n";
|
||
|
|
std::cout << "========================================\n";
|
||
|
|
|
||
|
|
return g_stats.failed > 0 ? 1 : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace test
|
||
|
|
} // namespace extra2d
|