Hello World test structure
Assuming you read the Orcraft tutorial overview, we can start developing a first test. This is a minimal implementation of an Orcraft test.
Open the following file in your preferred IDE (we use VS Code for this demonstration) and let's review its code:
orcraft-test-suite-tutorial/test_group_intro/test_hello_world/test_hello_world.py
Imports
from __future__ import annotations
from orcraft import (
OrcraftLogger as Logger,
OrcraftTest,
OrcraftTestConfig,
OrcraftTestCase,
OrcraftConfigField,
)
from __future__ import annotations lets the type hints below (e.g. TestHelloWorldConfig | None)
use the modern union syntax regardless of the Python version running the test.
The test imports the building blocks it needs from the orcraft package: a logger, the base test and config classes, the test case type, and the config field helpers.
Config class
class TestHelloWorldConfig(OrcraftTestConfig):
name = OrcraftConfigField.Text(default="Hello World")
description = OrcraftConfigField.Text(
default=(
"This is a minimal implementation of an Orcraft Test.\n"
"It demonstrates how to define a test config, inherit from OrcraftTest, and implement a single test case.\n"
"When executed, the test logs a simple Hello World message and returns a passing test result."
)
)
TestHelloWorldConfig is this test's user-defined config, inheriting from the framework's
OrcraftTestConfig. Its fields (name and
description) drive what is shown for this test in the controller's GUI and in the CLI's run tables.
name and description are both
built into OrcraftTestConfig itself, not
something this test adds - every test config exposes them. Overriding default here only replaces
the value shown when no other value is configured; it does not change what the field is used for.
Some fields are editable from the controller's GUI,
while others are not - name and
description are examples of fields that aren't configurable there. Complete documentation of
config fields is available in the Orcraft Configuration Fields page.
Test class
class TestHelloWorld(OrcraftTest):
def __init__(self, config: TestHelloWorldConfig | None = None) -> None:
super().__init__(config or TestHelloWorldConfig())
def _run_case(self, case: OrcraftTestCase):
Logger.log("Hello World", color=Logger.Color.BLUE)
return case.result
TestHelloWorld is the test itself. It accepts an optional config in __init__, falling
back to the default, and implements _run_case,
which is invoked once per configured test case.
The one line it logs uses Logger.log, whose
color argument picks one of the logger's colors.
Main
def main() -> None:
test = TestHelloWorld()
test.run()
if __name__ == "__main__":
main()
Finally, main lets the test run standalone,
outside the controller, when the file is executed directly as a script.