Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[maint] Reduce likelihood of creating dependent tests from fixtures #425

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

PGijsbers
Copy link
Collaborator

@PGijsbers PGijsbers commented Dec 19, 2024

Change

Restructure the fixtures around the default json objects to reduce the chance of accidentally creating dependent tests. In the current situation:

  • some of the fixtures are session scoped, which means the same result is used for multiple tests, but
  • that result can be modified since the dictionary is mutable

This means that if you're not careful, you can end up modifying the state for other tests.
Now, we separate out loading from disk (once per session) to creating the dictionary (once per test), removing the ability to persist state across tests. Note that a single test can still (indirectly) request the same fixture twice, in which case there still needs to be a precaution not to modify the dicts directly.

How to Test

Run the tests, think things through. Alternatively, the following tests will pass on develop but not on this branch:

def test_a(body_concept):
    body_concept["marker"] = 1  # marker is not normally present on the body_concept fixture
    
def test_b(body_concept):
    assert "marker" in body_concept

if test_a runs before test_b, then test_b will pass (on develop). If test_a isn't run, or run after test_b, then it fails (on develop). On this branch test_b will always fail, since body_concept is now a function-level fixture.

Checklist

  • Tests have been added or updated to reflect the changes, or their absence is explicitly explained.
  • Documentation has been added or updated to reflect the changes, or their absence is explicitly explained.
  • A self-review has been conducted checking:
    • No unintended changes have been committed.
    • The changes in isolation seem reasonable.
    • Anything that may be odd or unintuitive is provided with a GitHub comment explaining it (but consider if this should not be a code comment or in the documentation instead).
  • All CI checks pass before pinging a reviewer, or provide an explanation if they do not.

Related Issues

with open(path_test_resources() / "schemes" / "aiod" / "aiod_concept.json", "r") as f:
return json.load(f)


@pytest.fixture()
def body_concept(load_body_concept: dict) -> dict:
return copy.deepcopy(load_body_concept)
Copy link
Collaborator Author

@PGijsbers PGijsbers Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deep copy ensures that the loaded json that persists for the session is never edited.
The fact that this is now a function-level fixture in turn means that if the return value of body_concept does get modified, it does not carry over to other tests ( but may carry over to other fixtures which depend on it in the same test).


@pytest.fixture()
def body_resource(body_concept: dict, load_body_resource: dict) -> dict:
body = copy.deepcopy(body_concept)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason that we use deepcopy here, is that otherwise we would edit body_concept directly.
While this would no longer carry over to other tests, it could carry over to other fixtures in the same test.
E.g., if body_resource and contact would both be requested by the same test, then we need to avoid that either of those fixtures is directly modifying the body_concept since it is still persistent within the same test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant