Skip to content

Getting Started

Adrien Quillet edited this page Feb 16, 2023 · 1 revision

In this section, you will learn how to create and run your first test, in your project, from scratch !

Install STH in your project

From Godot Asset library

This is the prefered way to install and deploy STH addon in your project since it's automatized by Godot Engine.

  1. From your editor, in AssetLib window, look for Simple Test Harness addon,
  2. Click on the addon result, and press Download,
  3. Once download is finished, valide by clinking on Install,
  4. Check that the addon is enabled in Project Settings, in Plugins section,
  5. Restart Godot Engine

From Github repository

In Godot Asset library, you may not get the latest version of the addon, nor a beta version containing a new feature. To get latest version :

  1. Download latest version from releases pages, or directly download the source code from repository main page,
  2. Unzip content, and copy it into your project folder
    • Take care not overriding your project files (like project.godot)
  3. Check that the addon is enabled in Project Settings, in Plugins section,
  4. Restart Godot Engine

Create your first test case

In STH, a test is just à GDScript with that extends a particular class : TestCase. This class gives you all necessary tools to write your own tests. STH runner can run any test in classes that extends TestCase. To go deeply in test cases, please refer to Writing tests.

Storing test

Broadly speaking, it is a good practice to separate your production code (your game, your program) from your test code. Since test code does not run in released productions, they should not be packaged into the generated executable. To ease packaging, tests should be stored in another directory.

If you store your production code into the src folder, a good practice can be to store tests and test resources into src-test folder.

Creating your first test case

STH provides a script template to easily create a TestCase. To create a test case, open Create script dialog, give a name to your script, and make it inherits from TestCase class. In Template combo, you should be able to use the provided test case template.

image

You should have a test case skeleton that looks like this :

extends TestCase

#------------------------------------------
# Test setup
#------------------------------------------

# Called once per test case, before any test
static func beforeAll() -> void:
    pass

# Called once per test case, after any test
static func afterAll() -> void:
    pass

# Called before each test
func beforeEach() -> void:
    pass

# Called after each test
func afterEach() -> void:
    pass

#------------------------------------------
# Tests
#------------------------------------------

func test() -> void:
    pass

From now, you can run your test case ! To do that, right clic in script editor, and select Run test case. The the Simple Test Harness dock (by default, with Scene and Import dock), you should so your test case result :

image

🎉 Congratulations ! You run your first test case !

Writing your own tests

For the time beeing, you just run the default test provided by test case script template. That's nice, but not very useful.

Your goal is now to replace this dummy test by your own ! Fro example, you can write a simple test by writing something like

func test() -> void:
    name = "Say My Name"
    assert_equals("Say My Name", get_name())

Here, we simply set the node name, and check that it is as expected using the assertion assert_equals. To learn more about assertions, please refer to the writing tests section.

The above test will run successfully, since the expected name is the same as the tested name. You can change the expected name to something else that the actual name ; as a result, the test case will failed.

Clone this wiki locally