Skip to content

xUnit Test Discovery Crash with Godot Types ​

Godot values in xUnit [MemberData] can crash the test host during discovery (issue #16):

csharp
public static IEnumerable<object[]> Paths = [[new NodePath("/root")]];

[Theory]
[MemberData(nameof(Paths))]
public void CanLogNodePath(NodePath path)
{
    GD.Print(path);
}

xUnit enumerates member data before fixtures run. At that point, the engine has not started yet, so constructors that call Godot native code fail.

Workarounds ​

Workaround 1: Disable Discovery Enumeration ​

Set DisableDiscoveryEnumeration = true so that xUnit enumerates the member data only when the tests run:

csharp
[Theory]
[MemberData(nameof(Paths), DisableDiscoveryEnumeration = true)]
public void CanLogNodePath(NodePath path)
{
    GD.Print(path);
}

The test explorer then shows one theory entry, not one entry for each data row.

Workaround 2: Use Primitive Values ​

If you need one test explorer entry for each row, pass primitive values. Then create the Godot value inside the test, after its fixture has started:

csharp
public static IEnumerable<object[]> Paths = [["/root"], ["/root/Main"]];

[Theory]
[MemberData(nameof(Paths))]
public void CanLogNodePath(string value)
{
    var path = new NodePath(value);
    GD.Print(path);
}

Prefer this workaround.

Affected Types ​

Any GodotSharp type whose constructor calls native code is affected: NodePath, StringName, Rid, GodotObject subclasses, and some math value types. Primitive C# values such as strings, numbers, booleans, and their arrays are safe for discovery.

2dog is Free and Libre Open Source under the MIT License.
2dog is not affiliated with or endorsed by the Godot Foundation.
Godot is licensed under the MIT License; the Godot logo is by Andrea Calabró under CC BY 4.0. @icons made by Voxy under the MIT License.
Continuous Integration compute graciously supported by Blacksmith.