Skip to content

[Feat] Add a testing guide sec for C functions  #7862

Description

@wenzeslaus

Add a testing guide for tests which call/test C functions through ctypes or test their wrappers (e.g., grass.pygrass). Raising the issue and pointing to an example is the minimal addition.

However, this may, or rather should, trigger a review of current techniques and compare those to the preferred practice, whatever that is. I tried couple of approaches, although I think I had to clean up some of that because only basic subprocess and code in a string actually worked reliably.

Sadly for pytest, grass.gunittest framework does handle these fairly well since a test runs in a subprocess in an exiting GRASS session which has the environment set up and it robust to crashes and exits since a test is a subprocess (if I recall correctly, this was indeed intentional design for grass.gunittest and one of the reasons to have a customized test framework, so no surprise this works out of the box there). Other than that, pytest is a better match for tests of individual, small functions.

One case the guide does not cover, and it bit me on #7835: a test that calls into C through ctypes should make those calls in a subprocess.

My regression test there called Vect_cat_list_to_array() through ctypes in the pytest process. The bug under test was a null dereference, so on Windows the call died — and because a segfault ends the process, pytest went with it. The Windows suite stopped at 8% with Process completed with exit code 1 and no traceback at all, since pytest's captured output dies with it. @echoix restarted the run twice before I worked out that it was mine and not flakiness.

Running the ctypes calls in a subprocess (subprocess.run([sys.executable, "-c", SCRIPT], env=env, ...) with the env from the standard session fixture, asserting on the return code) turns the same crash into return code -11 in one ordinary test failure, and the rest of the suite survives. That is better even when the test passes, because a suite-killer becomes something you can diagnose.

Worth a short subsection if you agree — the failure mode gives you nothing to go on, so it is hard to work out from first principles. Happy to draft the wording.

Originally posted by @Pranav-error in #7851 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions