|
| 1 | +class_name MyTestRunner |
| 2 | +extends "res://addons/gdUnit4/src/core/runners/GdUnitTestSessionRunner.gd" |
| 3 | + |
| 4 | +# Usage: |
| 5 | +# var TestRunner := load("res://my_test_runner.gd") |
| 6 | +# var runner = TestRunner.new() |
| 7 | +# add_child(runner) |
| 8 | +# runner.include_tests("res://tests/") |
| 9 | + |
| 10 | +const GdUnitTools := preload("res://addons/gdUnit4/src/core/GdUnitTools.gd") |
| 11 | + |
| 12 | +signal finished(result_code) |
| 13 | + |
| 14 | +enum Result { |
| 15 | + SUCCESS = 0, |
| 16 | + FAILURE = 100, |
| 17 | + DIDNT_RUN = 200, |
| 18 | + TESTS_NOT_FOUND = 204 |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +class Stats: |
| 23 | + var num_total: int = 0 |
| 24 | + var num_failed: int = 0 |
| 25 | + var num_errors: int = 0 |
| 26 | + var num_skipped: int = 0 |
| 27 | + var num_flaky: int = 0 |
| 28 | + |
| 29 | + func clear(): |
| 30 | + num_total = 0 |
| 31 | + num_errors = 0 |
| 32 | + num_failed = 0 |
| 33 | + num_skipped = 0 |
| 34 | + num_flaky = 0 |
| 35 | + |
| 36 | + |
| 37 | +var stats := Stats.new() |
| 38 | +var suite_stats := Stats.new() |
| 39 | +var result_code: int = Result.DIDNT_RUN |
| 40 | + |
| 41 | +var _included_tests := PackedStringArray() |
| 42 | + |
| 43 | + |
| 44 | +## Initialize test execution |
| 45 | +func init_runner() -> void: |
| 46 | + print_rich(_step(), _prominent("Initializing test runner...")) |
| 47 | + _test_cases = _discover_tests() |
| 48 | + if _test_cases.is_empty(): |
| 49 | + print_rich(_error("No test cases found!")) |
| 50 | + _finish(Result.TESTS_NOT_FOUND) |
| 51 | + return |
| 52 | + _state = RUN |
| 53 | + |
| 54 | + |
| 55 | +## Returns the exit code based on test results.[br] |
| 56 | +## Maps test report status to process exit codes. |
| 57 | +func get_exit_code() -> int: |
| 58 | + if stats.num_total == 0: |
| 59 | + return Result.DIDNT_RUN |
| 60 | + elif stats.num_failed > 0 or stats.num_errors > 0: |
| 61 | + return Result.FAILURE |
| 62 | + return Result.SUCCESS |
| 63 | + |
| 64 | + |
| 65 | +## Cleanup and quit the runner. |
| 66 | +func quit(_exit_code: int) -> void: |
| 67 | + _finish(_exit_code) |
| 68 | + |
| 69 | + |
| 70 | +## Add file or dir for test discovery. |
| 71 | +func include_tests(path: String) -> void: |
| 72 | + _included_tests.append(path) |
| 73 | + |
| 74 | + |
| 75 | +## Discover tests added with include_tests() |
| 76 | +func _discover_tests() -> Array[GdUnitTestCase]: |
| 77 | + var gdunit_test_discover_added := GdUnitSignals.instance().gdunit_test_discover_added |
| 78 | + |
| 79 | + var scanner := GdUnitTestSuiteScanner.new() |
| 80 | + for path in _included_tests: |
| 81 | + var scripts := scanner.scan(path) |
| 82 | + for script in scripts: |
| 83 | + print_rich(_step(), "Scanning: ", script.resource_path) |
| 84 | + GdUnitTestDiscoverer.discover_tests(script, func(test: GdUnitTestCase) -> void: |
| 85 | + print_rich(_substep(), "Discovered %s" % test.display_name) |
| 86 | + _test_cases.append(test) |
| 87 | + gdunit_test_discover_added.emit(test) |
| 88 | + ) |
| 89 | + |
| 90 | + return _test_cases |
| 91 | + |
| 92 | + |
| 93 | +func _finish(code: int) -> void: |
| 94 | + _state = EXIT |
| 95 | + result_code = code |
| 96 | + GdUnitTools.dispose_all() |
| 97 | + await GdUnitMemoryObserver.gc_on_guarded_instances() |
| 98 | + await get_tree().process_frame |
| 99 | + await get_tree().physics_frame |
| 100 | + finished.emit(result_code) |
| 101 | + |
| 102 | + |
| 103 | +## Process test events. |
| 104 | +func _on_gdunit_event(event: GdUnitEvent) -> void: |
| 105 | + match event.type(): |
| 106 | + GdUnitEvent.INIT: |
| 107 | + print_rich(_step(), _prominent("Initializing...")) |
| 108 | + stats.clear() |
| 109 | + suite_stats.clear() |
| 110 | + |
| 111 | + GdUnitEvent.STOP: |
| 112 | + print_rich(_step(), _prominent("Finished all tests.")) |
| 113 | + _print_stats(stats, "Overall Summary") |
| 114 | + |
| 115 | + GdUnitEvent.TESTSUITE_BEFORE: |
| 116 | + print_rich(_step(), _prominent("Loading: "), _suite(event.resource_path())) |
| 117 | + suite_stats.clear() |
| 118 | + |
| 119 | + GdUnitEvent.TESTSUITE_AFTER: |
| 120 | + print_rich(_substep(), _prominent("Finished: "), _suite(event.resource_path())) |
| 121 | + _print_failure_report(event.reports()) |
| 122 | + _print_stats(suite_stats, "Summary") |
| 123 | + stats.num_total += suite_stats.num_total |
| 124 | + stats.num_errors += suite_stats.num_errors |
| 125 | + stats.num_failed += suite_stats.num_failed |
| 126 | + stats.num_skipped += suite_stats.num_skipped |
| 127 | + stats.num_flaky += suite_stats.num_flaky |
| 128 | + |
| 129 | + GdUnitEvent.TESTCASE_BEFORE: |
| 130 | + var test := _test_session.find_test_by_id(event.guid()) |
| 131 | + print_rich(_substep(), _prominent("Started: "), |
| 132 | + _suite(test.suite_name), " > ", _case(test.display_name)) |
| 133 | + |
| 134 | + GdUnitEvent.TESTCASE_AFTER: |
| 135 | + suite_stats.num_total += 1 |
| 136 | + suite_stats.num_errors += event.error_count() |
| 137 | + suite_stats.num_failed += event.failed_count() |
| 138 | + suite_stats.num_skipped += event.skipped_count() |
| 139 | + suite_stats.num_flaky += 1 if event.is_flaky() else 0 |
| 140 | + |
| 141 | + var test := _test_session.find_test_by_id(event.guid()) |
| 142 | + if event.is_success(): |
| 143 | + _print_result(_bold(_success("PASSED")), test.suite_name, test.display_name) |
| 144 | + elif event.is_skipped(): |
| 145 | + _print_result(_bold(_muted("SKIPPED")), test.suite_name, test.display_name) |
| 146 | + elif event.is_failed() or event.is_error(): |
| 147 | + _print_result(_bold(_error("FAILED")), test.suite_name, test.display_name) |
| 148 | + _print_failure_report(event.reports()) |
| 149 | + elif event.is_warning(): |
| 150 | + _print_result(_bold(_warning("WARNING")), test.suite_name, test.display_name) |
| 151 | + _print_failure_report(event.reports()) |
| 152 | + |
| 153 | + |
| 154 | +# *** CONSOLE OUTPUT |
| 155 | + |
| 156 | +static func _colored(text: String, color: Color) -> String: |
| 157 | + return "[color=%s]%s[/color]" % [color.to_html(), text] |
| 158 | + |
| 159 | + |
| 160 | +static func _bold(text: String) -> String: |
| 161 | + return "[b]%s[/b]" % text |
| 162 | + |
| 163 | + |
| 164 | +static func _step() -> String: return _bold(_colored("==> ", Color.WHITE)) |
| 165 | +static func _substep() -> String: return _bold(_colored("--> ", Color.WHITE)) |
| 166 | +static func _prominent(text: String) -> String: return _bold(_colored(text, Color.WHITE)) |
| 167 | + |
| 168 | +static func _primary(text: String) -> String: return _colored(text, Color.WHITE) |
| 169 | +static func _accent(text: String) -> String: return _colored(text, Color.DODGER_BLUE) |
| 170 | +static func _muted(text: String) -> String: return _colored(text, Color.GRAY) |
| 171 | +static func _success(text: String) -> String: return _colored(text, Color.GREEN) |
| 172 | +static func _warning(text: String) -> String: return _colored(text, Color.GOLDENROD) |
| 173 | +static func _error(text: String) -> String: return _colored(text, Color.RED) |
| 174 | + |
| 175 | +static func _suite(text: String) -> String: return _colored(text, Color.DARK_CYAN) |
| 176 | +static func _case(text: String) -> String: return _colored(text, Color.CYAN) |
| 177 | + |
| 178 | + |
| 179 | +static func _print_result(status: String, test_suite: String, test_case: String) -> void: |
| 180 | + print_rich(_substep(), status, ": ", |
| 181 | + _suite(test_suite), " > ", _case(test_case)) |
| 182 | + |
| 183 | + |
| 184 | +static func _print_failure_report(reports: Array[GdUnitReport]) -> void: |
| 185 | + for report in reports: |
| 186 | + if ( |
| 187 | + report.is_failure() |
| 188 | + or report.is_error() |
| 189 | + or report.is_warning() |
| 190 | + or report.is_skipped() |
| 191 | + ): |
| 192 | + var text: String = str(report).indent(" ".repeat(4)) |
| 193 | + print_rich(text) |
| 194 | + |
| 195 | + |
| 196 | +static func _print_stats(p_stats: Stats, p_header: String) -> void: |
| 197 | + var total: String = " %d total" % p_stats.num_total |
| 198 | + var errors: String = " %d errors" % p_stats.num_errors |
| 199 | + var failed: String = " %d failed" % p_stats.num_failed |
| 200 | + var skipped: String = " %d skipped" % p_stats.num_skipped |
| 201 | + var flaky: String = " %d flaky" % p_stats.num_flaky |
| 202 | + print_rich( |
| 203 | + _substep(), |
| 204 | + _bold(_accent(p_header)), ": ", |
| 205 | + _primary(total), |
| 206 | + _error(errors) if p_stats.num_errors > 0 else _primary(errors), |
| 207 | + _error(failed) if p_stats.num_failed > 0 else _primary(failed), |
| 208 | + _primary(skipped), |
| 209 | + _warning(flaky) if p_stats.num_flaky > 0 else _primary(flaky) |
| 210 | + ) |
0 commit comments