F4: Headless test harness
Add an in-repo test runner that discovers test/**/test_*.gd and runs under --headless. It uses Godot's Logger API, so engine and script errors during a test fail that test. make test runs the suite, and make test-selfcheck runs deliberately failing fixtures to prove each kind of failure is detected. Closes #4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
## Headless test runner. Discovers test_*.gd files, runs their test_* methods, and
|
||||
## exits non-zero if anything fails.
|
||||
##
|
||||
## tools/run_tests.sh [--root=res://test/sim] [--filter=substring]
|
||||
extends SceneTree
|
||||
|
||||
const DEFAULT_ROOT: String = "res://test"
|
||||
const FIXTURES_ROOT: String = "res://test/fixtures"
|
||||
|
||||
var _logger: TestLogger = TestLogger.new()
|
||||
var _root: String = DEFAULT_ROOT
|
||||
var _filter: String = ""
|
||||
var _passed: int = 0
|
||||
var _failed: int = 0
|
||||
var _reports: Array[String] = []
|
||||
|
||||
|
||||
func _initialize() -> void:
|
||||
OS.add_logger(_logger)
|
||||
_parse_args()
|
||||
_run.call_deferred()
|
||||
|
||||
|
||||
func _parse_args() -> void:
|
||||
for arg: String in OS.get_cmdline_user_args():
|
||||
if arg.begins_with("--root="):
|
||||
_root = arg.trim_prefix("--root=").trim_suffix("/")
|
||||
elif arg.begins_with("--filter="):
|
||||
_filter = arg.trim_prefix("--filter=")
|
||||
else:
|
||||
push_error("Unknown test runner argument: %s" % arg)
|
||||
|
||||
|
||||
func _run() -> void:
|
||||
var files: Array[String] = []
|
||||
_discover(_root, files)
|
||||
files.sort()
|
||||
# A runner that finds nothing must not look like a passing suite.
|
||||
var ran_nothing: bool = files.is_empty()
|
||||
for path: String in files:
|
||||
await _run_file(path)
|
||||
if _passed + _failed == 0:
|
||||
ran_nothing = true
|
||||
_finish(ran_nothing)
|
||||
|
||||
|
||||
func _discover(dir_path: String, out: Array[String]) -> void:
|
||||
# Fixtures contain deliberate failures. They only run when targeted directly.
|
||||
var skipping_fixtures: bool = dir_path == FIXTURES_ROOT and not _root.begins_with(FIXTURES_ROOT)
|
||||
if skipping_fixtures:
|
||||
return
|
||||
for name: String in DirAccess.get_files_at(dir_path):
|
||||
if name.begins_with("test_") and name.ends_with(".gd"):
|
||||
out.append(dir_path.path_join(name))
|
||||
for name: String in DirAccess.get_directories_at(dir_path):
|
||||
if not name.begins_with("."):
|
||||
_discover(dir_path.path_join(name), out)
|
||||
|
||||
|
||||
func _run_file(path: String) -> void:
|
||||
_logger.take_errors()
|
||||
var script: GDScript = load(path) as GDScript
|
||||
var load_errors: Array[String] = _logger.take_errors()
|
||||
if script == null or not script.can_instantiate():
|
||||
_record_load_failure(path, "could not compile the script", load_errors)
|
||||
return
|
||||
|
||||
var instance: TestCase = script.new() as TestCase
|
||||
if instance == null:
|
||||
_record_load_failure(path, "script does not extend TestCase", _logger.take_errors())
|
||||
return
|
||||
|
||||
var names: Array[String] = _test_names(script)
|
||||
for test_name: String in names:
|
||||
if _filter.is_empty() or (path + ":" + test_name).contains(_filter):
|
||||
await _run_test(instance, path, test_name)
|
||||
|
||||
|
||||
func _test_names(script: GDScript) -> Array[String]:
|
||||
var names: Array[String] = []
|
||||
for method: Dictionary in script.get_script_method_list():
|
||||
var method_name: String = method["name"]
|
||||
if method_name.begins_with("test_") and method_name not in names:
|
||||
names.append(method_name)
|
||||
names.sort()
|
||||
return names
|
||||
|
||||
|
||||
func _run_test(instance: TestCase, path: String, test_name: String) -> void:
|
||||
_logger.take_errors()
|
||||
instance.reset_for_test()
|
||||
await instance.before_each()
|
||||
await instance.call(test_name)
|
||||
await instance.after_each()
|
||||
# Let deferred calls raise their errors while this test is still current.
|
||||
await process_frame
|
||||
_record(path, test_name, instance.finish_test(_logger.take_errors()))
|
||||
|
||||
|
||||
func _record_load_failure(path: String, reason: String, errors: Array[String]) -> void:
|
||||
var failures: Array[String] = [reason]
|
||||
failures.append_array(errors)
|
||||
_record(path, "(load)", failures)
|
||||
|
||||
|
||||
func _record(path: String, test_name: String, failures: Array[String]) -> void:
|
||||
var label: String = "%s::%s" % [path.trim_prefix("res://"), test_name]
|
||||
if failures.is_empty():
|
||||
_passed += 1
|
||||
print("PASS %s" % label)
|
||||
return
|
||||
_failed += 1
|
||||
print("FAIL %s" % label)
|
||||
for failure: String in failures:
|
||||
print(" - %s" % failure)
|
||||
_reports.append("%s: %s" % [label, failure])
|
||||
|
||||
|
||||
func _finish(ran_nothing: bool) -> void:
|
||||
print("")
|
||||
for report: String in _reports:
|
||||
print("FAILED: %s" % report)
|
||||
if ran_nothing:
|
||||
print("No tests ran under %s. Treating this as a failure." % _root)
|
||||
print("Tests: %d passed, %d failed." % [_passed, _failed])
|
||||
quit(1 if _failed > 0 or ran_nothing else 0)
|
||||
Reference in New Issue
Block a user