Skip to content

Pytest Plugin

Pydust ships with a Pytest plugin that builds, collects and executes your Zig tests. This means testing should work out-of-the-box with your existing Python project and you never need to interact directly with the Zig build system if you don't want to.

The Zig documentation provides an excellent introduction to writing tests.

Note

Zig tests are currently spawned as a separate process. This means you must manually call py.initialize() and defer py.finalize() in order to setup and teardown a Python interpreter.

example/pytest.zig
1
2
3
4
5
6
7
8
9
test "pydust pytest" {
    py.initialize();
    defer py.finalize();

    const str = try py.PyString.create("hello");
    defer str.decref();

    try std.testing.expectEqualStrings("hello", try str.asSlice());
}

Add ziggy-pydust as a dev dependency:

poetry add -G dev ziggy-pydust
pyproject.toml
[tool.poetry.group.dev.dependencies]
+ ziggy-pydust = "0.TODO_SET_MINOR_VERSION.*"

Note

Keep this version the same as build dependency version. There is currently no way to guarantee this with Poetry.

After running poetry run pytest you should see your Zig tests included in your Pytest output:

================================== test session starts ==================================
platform darwin -- Python 3.11.5, pytest-7.4.0, pluggy-1.3.0
plugins: ziggy-pydust-0.2.1
collected 7 items

example/pytest.zig .x                                                             [100%]

============================= 1 passed, 1 xfailed in 0.30s ==============================