Test Setup/Teardown
Definition
A TestSuite is a collection of tests that are aligned to a specific class or module that you want to test. When writing tests, it is common to find that several tests require similar test data to be created before and cleaned up after the test run. GdUnit TestSuites allow you to define pre-initialized test data and cleanup functions that will be executed at specific points during test execution.
In addition to containing multiple test cases, a TestSuite can also contain setup and teardown functions that are executed before and after each test case, as well as before and after the entire TestSuite. This allows you to control the test environment and ensure that tests are executed in a consistent and repeatable manner.
The TestSuite Execution Graph
TestSuite Hooks
GdUnit TestSuites provide the following hooks that allow you to control the test environment. GdUnit4 allows you to use asserts within these stages, and any errors that occur will be reported.
-
GdUnit uses predefined functions to define the execution stages.
To define a test, you must use the prefixtest_
, e.g.test_verify_is_string
.Stage Description before executed only once at the start of the TestSuite run after executed only once at the end of the TestSuite run before_test executed before each test is started after_test executed after each test has finished test_ executes the test content, where <name>
is the test name -
GdUnit use attributes to define the execution stages and tests.
All GdUnit attributes are contained in thenamespace GdUnit4
Stage Description [Before] executed only once at the start of the TestSuite run [After] executed only once at the end of the TestSuite run [BeforeTest] executed before each test is started [AfterTest] executed after each test has finished [TestCase] executes the test content
The stage before
This function is executed once at the beginning of the TestSuite and is used to set up any resources or data that will be required by all of the test cases in the suite. For example, if you need to create a database connection or initialize a global configuration object, you would do so in this function.
-
func before()
Use this function inside your TestSuite to define a pre-hook and prepare your TestSuite data.class_name GdUnitExampleTest extends GdUnitTestSuite var _test_data :Node # create some test data here func before(): _test_data = Node.new()
-
[Before]
Use this attribute inside your TestSuite to define a method as pre hook to prepare your TestSuite data.using GdUnit4; using static GdUnit4.Assertions; namespace ExampleProject.Tests { [TestSuite] public class ExampleTest { private Godot.Node _test_data; [Before] public void Setup() { // create some test data here _test_data = new Godot.Node(); } } }
Alternatively, you can use the tool auto_free(), the object is automatically freed at after stage.
-
class_name GdUnitExampleTest extends GdUnitTestSuite var _test_data :Node # create some test data here func before(): _test_data = auto_free(Node.new())
-
using GdUnit4; using static GdUnit4.Assertions; namespace ExampleProject.Tests { [TestSuite] public class ExampleTest { private Godot.Node _test_data; [Before] public void Setup() { // create some test data here _test_data = AutoFree(new Godot.Node()); } } }
Stage after
This function is executed once at the end of the TestSuite and is used to clean up any resources or data that was created or modified during the test run. For example, if you need to close a database connection or delete temporary files, you would do so in this function.
-
func after():
Use this function inside your TestSuite to define a shutdown hook and release your TestSuite data.class_name GdUnitExampleTest extends GdUnitTestSuite var _test_data :Node # give free resources where was created in before() func after(): _test_data.free()
-
[After]
Use this attribute inside your TestSuite to define a method as shutdown hook to release your TestSuite data.using GdUnit4; using static GdUnit4.Assertions; namespace ExampleProject.Tests { [TestSuite] public class ExampleTest { private Godot.Node _test_data; [After] public void TearDownSuite() { // give free resources where was created in before() _test_data.Free(); } } }
Stage before_test
This function is executed before each test case and is used to set up any resources or data that are required by the test case. For example, if you need to create a temporary file or initialize a class instance, you would do so in this function.
-
func before_test():
Use this function inside your TestSuite to define a pre hook to initalize your TestCase data.class_name GdUnitExampleTest extends GdUnitTestSuite var _test_data :Node # initalizize you test data here func before_test(): _test_data = Node.new()
-
[BeforeTest]
Use this attribute inside your TestSuite to define a method as pre hook to initalize your TestCase data.using GdUnit4; using static GdUnit4.Assertions; namespace ExampleProject.Tests { [TestSuite] public class ExampleTest { private Godot.Node _test_data; [BeforeTest] public void SetupTest() { // initalizize you test data here _test_data = new Godot.Node(); } } }
Alternatively, you can use the tool auto_free(), the object is automatically freed at after_test stage.
-
class_name GdUnitExampleTest extends GdUnitTestSuite var _test_data :Node # initalizize you test data here func before_test(): _test_data = auto_free(Node.new())
-
using GdUnit4; using static GdUnit4.Assertions; namespace ExampleProject.Tests { [TestSuite] public class ExampleTest { private Godot.Node _test_data; [BeforeTest] public void SetupTest() { // initalizize you test data here _test_data = AutoFree(new Godot.Node()); } } }
Stage after_test
This function is executed after each test case and is used to clean up any resources or data that were created or modified during the test case. For example, if you need to delete a temporary file or close a network connection, you would do so in this function.
-
func after_test():
Use this function inside your TestSuite to define a test clean up hook to release your TestCase data if neeed.class_name GdUnitExampleTest extends GdUnitTestSuite # clean up your test data func after_test(): ...
-
[AfterTest]
Use this attribute inside your TestSuite to define a method as clean up hook to release your TestCase data if neeed.using GdUnit4; using static GdUnit4.Assertions; namespace ExampleProject.Tests { [TestSuite] public class ExampleTest { [AfterTest] public void TearDownTest() { // clean up your test data } } }