Gdextension Unit tests

Godot Version



Hi I am trying to create a Unit test for my gdextension. For this i am using doctest.h. I added everything and was able to test simple things like:

TEST_CASE("Example test") {
    CHECK(1 == 1);

Of course I want to test more complex stuff like for example the class Summator : public RefCounted example → Custom modules in C++ — Godot Engine (stable) documentation in English
In this tutorial they also create a unit test:

// test_summator.h

#include "tests/test_macros.h"

#include "modules/summator/summator.h"

namespace TestSummator {

TEST_CASE("[Modules][Summator] Adding numbers") {
	Ref<Summator> s = memnew(Summator);
	CHECK(s->get_total() == 0);

	CHECK(s->get_total() == 10);

	CHECK(s->get_total() == 30);

	CHECK(s->get_total() == 60);

	CHECK(s->get_total() == 0);

} // namespace TestSummator


however they use #include “tests/test_macros.h”. Which i do not have. Also they call the test by using ./bin/<godot_binary> --test --source-file=“test_summator” --success which i do not do.

I created defined my own program for testing in the Sconstruct file called MyGodotTest so if i want to build and test i do scons and then just start ./MyGodotTest . However if i do this i get an error with Ref s = memnew(Summator); → FATAL ERROR: test case CRASHED: SIGSEGV - Segmentation violation signal I think this is duo to me not initialising godot or something?
Does anybody have some tipps. Or should I just copy all the files from "tests/ " into my sourcecode so i can use test_macros.h ? I feel like everything these tutorials show is wrong if you use Godotcpp :frowning:

The example you’re looking at is for modules, not extensions, the tests in the main repo are designed to be used only with the engine itself, and can’t be run via extensions as far as I know, don’t know how to work around it unfortunately

Thank you for the fast reply. So I just have to extract all the non engine stuff to test it… :confused: thats lame^^

I don’t think that will necessarily work, I’d suggest just writing tests with the existing doctest systems as they are, and run them independently the built in tests are compiled with the engine so won’t run any extension code that’s linked in after compiling, and I don’t think the test suite will even load extensions at all

I’d suggest opening a proposal to discuss integrating tests or creating a framework specifically for godot-cpp