Link Search Menu Expand Document

TestSuite

Definition

A TestSuite is a collection of tests basically alligned to a class you want to test.
When writing tests, it is common to find that several tests need similar test data to created before and cleanup after test run.
GdUnit TestSuite runs in execution stages (hooks) where you allow to define preinitalisized test data.

  • GdUnit use predefined functions to define the execution stages.
    To define a test you have to use the prefix test_ e.g. test_verify_is_string

    Stage Description
    before executed only once at TestSuite run is started
    after executed only once at TestSuite run has finished
    before_test executed before each test is started
    after_test executed after each test has finished
    test_ executes the test content
  • GdUnit use attributes to define the execution stages and tests.
    All GdUnit attributes are contained in the namespace GdUnit3

    Stage Description
    [Before] executed only once at TestSuite run is started
    [After] executed only once at TestSuite run has finished
    [BeforeTest] executed before each test is started
    [AfterTest] executed after each test has finished
    [TestCase] executes the test content

Stage before

This stage is executed only once at the beginning of a TestSuite execution.
GdUnit3 allows to use asserts within this stage, occurring errors are reported.

  • 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 GdUnit3;
    using static GdUnit3.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();
            }
        }
    }
    
Advice
If you create objects in the Before stage, you must release the object at the end in the After stage, otherwise, the object is reported as an orphan 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 GdUnit3;
    using static GdUnit3.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 stage is executed only once at the end of a TestSuite execution.
GdUnit3 allows to use asserts within this stage, occurring errors are reported.

  • 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 GdUnit3;
    using static GdUnit3.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 stage is executed before each TestCase.
GdUnit3 allows to use asserts within the this stage, occurring errors are reported.

  • 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 GdUnit3;
    using static GdUnit3.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();
            }
        }
    }
    
Advice
If you create objects in the before_test stage, you must release the object at the end in the after_test stage, otherwise, the object is reported as an orphan 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 GdUnit3;
    using static GdUnit3.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 stage is executed after each TestCase.
GdUnit3 allows to use asserts within the this stage, occurring errors are reported.

  • 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 GdUnit3;
    using static GdUnit3.Assertions;
    
    namespace ExampleProject.Tests
    {
        [TestSuite]
        public class ExampleTest
        {
            [AfterTest]
            public void TearDownTest()
            {
                // clean up your test data
            }
        }
    }
    

The execution graph of an TestSuite execution

  •  (Run)
       |- func before() -> void: # init the TestSuite
       |    ...
       |
       [...] # loops over all tests
          |     
          |- func before_test() -> void: # init next TestCase
          |    ...
          |
          >--- 
             | func test_1() -> void: # execute TestCase (1-n iterations)
             |   ...
          <---
          |  
          |- func after_test() -> void: # clean-up current TestCase finished
          |     ...
       [...]
       |   
       | - func after() -> void: # clean-up TestSuite finished
       |      ....
     (END)
    
  •  (Run)
       |- [Before] // init the TestSuite
       |  public void Setup() {}
       |
       [...] // loops over all tests
          |     
          |- [BeforeTest] // init next TestCase
          |  public void SetupTest() {}
          |
          >--- 
             | [TestCase] // execute TestCase (1-n iterations)
             | public void TestCase1() {}
          <---
          |  
          |- [AfterTest] // clean-up current TestCase finished
          |  public void TearDownTest() {}
       [...]
       |   
       | - [After] // clean-up TestSuite finished
       |   public void TearDownSuite() {}
     (END)
    

Copyright © 2021-2022 Mike Schulze. Distributed by an MIT license.