Link Search Menu Expand Document

Asserts

GdUnit3 is providing a set of assertions where give you helpful error messages and improves yor test code readability. Assertions are type organized and supports fluent syntax writing.
The pattern for using asserts is defined as assert_<type>(<current>).<comparison function>([expected]).
If you don’t know the type of the current value, use the generic assert_that(<current>) instead.


  • Asserts are restricted by GdScript behavior and therefore do not fully support generics, i.e. you should prefer to use assert by type.
    That is, you must use assert_str for string, for example.

    class_name GdUnitExampleTest
    extends GdUnitTestSuite
    
    func test_hello_world() -> void:
        # using type save assert
        assert_str("Hello world").is_equal("Hello world")
        # using the common assert
        assert_that("Hello world").is_equal("Hello world")
    
  • The C# assert are smart and switch to the equivalent assert implementation by auto-typing and should also be used preferentially.
    Alternatively, you can use the typed asserts if you want to
    To use the assert you have to import it via “static GdUnit3.Assertions”.

    using GdUnit3;
    using static GdUnit3.Assertions;
    
    namespace ExampleProject.Tests
    {
        [TestSuite]
        public class ExampleTest
        {
            [TestCase]
            public void HelloWorld()
            {
                AssertThat("Hello world").IsEqual("Hello world");
                // using explicit the typed version
                AssertString("Hello world").IsEqual("Hello world");
            }
        }
    }
    

Advice
You should use the fluent syntax to keep your tests clearer
  • class_name GdUnitExampleTest
    extends GdUnitTestSuite
    
    # A bad example for the multiple use of `assert_str`.
    func test_hello_world() -> void:
        var current := "Hello World"
        assert_str(current).is_equal("Hello World")
        assert_str(current).contains("World")
        assert_str(current).not_contains("Green")
    
    # A good example of using fluent syntax to write more readable tests
    func test_hello_world_fluent() -> void:
        assert_str("Hello World")\
        	.is_equal("Hello World")\
        	.contains("World")\
        	.not_contains("Green")
    
  • using GdUnit3;
    using static GdUnit3.Assertions;
    
    namespace ExampleProject.Tests
    {
        [TestSuite]
        public class ExampleTest
        {
            // A bad example for the multiple use of `AssertThat`.
            [TestCase]
            public void HelloWorld()
            {
                string current = "Hello World";
                AssertThat(current).IsEqual("Hello World");
                AssertThat(current).Contains("World");
                AssertThat(current).NotContains("Green");
            }
    
            // A good example of using fluent syntax to write more readable tests
            [TestCase]
            public void HelloWorldFluent()
            {
                AssertThat("Hello World")
                    .IsEqual("Hello World")
                    .Contains("World")
                    .NotContains("Green");
            }
        }
    }    
    

The Generic Assert

  • Can be used for all types and gives you access to the basic test functions of GdUnit Assert.
    However, I recommend to always use the type-safe Assert, because GdScript is not type-safe.

    Assert Type
    assert_that auto typing
  • The C# assert are smart and switch to the equivalent assert implementation by auto-typing and should also be used preferentially.

    Assert Type
    AssertThat auto typing

The Basic Build-In Type Asserts

For more details about Build-In types click here Godot Build-In Types


Container Built-In Type Asserts

For more details about Build-In types click here Container built-in types


Engine Build-In Type Asserts



Table of contents


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