Adding configuration parameter
Now that we have the Maze application downloaded,
let's write a test that runs it. This test introduces its first configuration parameter - a
Folder field pointing at the
folder holding the Maze executable.
Folder Parameter test
This test lives in the tutorial suite's Basic Configuration Parameters test group, alongside the other parameter tests we'll cover in this and the next few chapters:
Let's review the code of the file named test_folder_parameter.py, residing in this path:
orcraft-test-suite-tutorial/test_group_basic_configuration_parameters/test_folder_parameter/
Imports
from __future__ import annotations
from orcraft import (
OrcraftTest,
OrcraftTestConfig,
OrcraftTestCase,
OrcraftConfigField,
)
from common.maze_app import MazeApp
Alongside the same framework building blocks used by the Hello World test, this test also imports
MazeApp - a small helper, shared across the tutorial suite, that locates the Maze executable in a
folder and runs it, logging its output.
orcraft-test-suite-tutorial/common/maze_app.py
Config class
class TestFolderParameterConfig(OrcraftTestConfig):
name = OrcraftConfigField.Text(default="Folder Parameter")
description = OrcraftConfigField.Text(
default=(
"Demonstrates a folder configuration parameter.\n"
"Runs the maze app executable with no arguments to display its help page and logs the output."
)
)
maze_executable_folder = OrcraftConfigField.Folder(default="", label="Maze Executable Folder", required=True)
maze_executable_folder is this test's first configuration parameter beyond the built-in
name and description. It's declared with
OrcraftConfigField.Folder, which
renders as a folder picker in the controller's GUI.
label sets the field's display name in the GUI, separate from its Python attribute name.
required=True means the test can't be run until a folder is selected - there's no meaningful
default, since it depends entirely on where you downloaded the Maze application to. These and the other
options every field accepts are listed in
Common config field options.
Test class
class TestFolderParameter(OrcraftTest):
def __init__(self, config: TestFolderParameterConfig | None = None) -> None:
super().__init__(config or TestFolderParameterConfig())
def _run_case(self, case: OrcraftTestCase):
maze_app = MazeApp(self.config.maze_executable_folder)
maze_app.run()
return case.result
_run_case reads self.config.maze_executable_folder - the folder selected in the
GUI (or the CLI), exposed as an ordinary attribute of the
config - and passes it to MazeApp. Called with no command, MazeApp runs the
executable with no arguments, which - as we saw in the Maze application chapter - prints its help page.
Main
def main() -> None:
test = TestFolderParameter()
test.run()
if __name__ == "__main__":
main()