Skip to content

Creating Tests

bitwes edited this page Dec 2, 2020 · 14 revisions

Sample for Setup

Here's a sample test script. Copy the contents into the file res://test/unit/test_example.gd then run your scene. If everything is setup correctly then you'll see some passing and failing tests. If you don't have "Run on Load" checked in the editor, you'll have to hit the ">" button on the dialog window.

extends "res://addons/gut/test.gd"
func before_each():
	gut.p("ran setup", 2)

func after_each():
	gut.p("ran teardown", 2)

func before_all():
	gut.p("ran run setup", 2)

func after_all():
	gut.p("ran run teardown", 2)

func test_assert_eq_number_not_equal():
	assert_eq(1, 2, "Should fail.  1 != 2")

func test_assert_eq_number_equal():
	assert_eq('asdf', 'asdf', "Should pass")

func test_assert_true_with_true():
	assert_true(true, "Should pass, true is true")

func test_assert_true_with_false():
	assert_true(false, "Should fail")

func test_something_else():
	assert_true(false, "didn't work")

Creating tests

All test scripts must extend the test class.

  • extends "res://addons/gut/test.gd"

Each test script has optional setup and teardown methods that you can provide an implementation for. These are called by Gut at various stages of execution. They take no parameters.

  • before_each(): Runs before each test.
  • after_each(): Runs after each test.
  • before_all(): Runs once before any test starts running.
  • after_all(): Runs once after all tests finish running.

All tests in the test script must start with the prefix test_ in order for them to be run. The methods must not have any parameters.

  • func test_this_is_only_a_test():

Each test should perform at least one assert or call pending to indicate the test hasn't been implemented yet.

A list of all asserts and other helper functions available in your test script can be found in Methods. There's also some helpful methods in the Gut object itself. They are listed in Gut Settings and Methods

Inner Test Classes

You can group tests together using Inner Classes. These classes must start with the prefix 'Test' (this is configurable) and they must also extend res://addons/gut/test.gd. You cannot create Inner Test Classes inside Inner Test Classes. More info can be found at Inner Test Classes.

Simple Example

extends 'res://addons/gut/test.gd'

class TestFeatureA:
	extends 'res://addons/gut/test.gd'

	var Obj = load('res://scripts/object.gd')
	var _obj = null

	func before_each():
		_obj = Obj.new()

	func test_something():
		assert_true(_obj.is_something_cool(), 'Should be cool.')

class TestFeatureB:
	extends 'res://addons/gut/test.gd'

	var Obj = load('res://scripts/object.gd')
	var _obj = null

	func before_each():
		_obj = Obj.new()

	func test_foobar():
		assert_eq(_obj.foo(), 'bar', 'Foo should return bar')

Where to next?

Clone this wiki locally