Skip to content

Collapse repetitive endpoint tests with @pytest.mark.parametrize #643

Description

@nanotaboada

Problem

tests/test_main.py has many near-identical test functions that differ only in
an input value and an expected status code — the "returns 404" cases for GET by
id, GET by squad number, PUT, and DELETE each repeat the same
arrange/act/assert shape; the cache-header and empty-body cases likewise. This
is the xUnit "one method per case" style. pytest provides a table-driven idiom.

Proposed Solution

Group cases that share a body under @pytest.mark.parametrize:

@pytest.mark.parametrize(
    ("method", "url"),
    [
        ("get", f"{PATH}{unknown_player().id}"),
        ("get", f"{PATH}squadnumber/{nonexistent_player().squad_number}"),
        ("delete", f"{PATH}squadnumber/{unknown_player().squad_number}"),
    ],
    ids=["get_by_id", "get_by_squad_number", "delete"],
)
def test_request_unknown_resource_response_status_not_found(client, method, url):
    """Unknown resource returns 404 Not Found."""
    response = getattr(client, method)(url)
    assert response.status_code == 404

Keep cases with bespoke setup/teardown (POST/PUT that create or mutate rows) as
their own functions — parametrising those hurts readability.

Suggested Approach

  1. Identify clusters with identical bodies: the 404 group, the cache-header
    pair (MISS then HIT), the 422 empty-body pair.
  2. Convert each cluster to one parametrised function with explicit ids= so
    failures name the case.
  3. tests/test_main.py is excluded from Black — match the existing manual
    formatting.
  4. Confirm the pytest run still reports a case for every original scenario.

Acceptance Criteria

  • Repetitive status-code tests consolidated with parametrize
  • Each parametrised case has a readable id
  • Tests with bespoke setup/teardown left standalone
  • Coverage does not drop; every case still exercised
  • CHANGELOG.md updated

References

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

    enhancementNew feature or requestpriority:lowNice-to-have improvement. Can be deferred without blocking other work.pythonPull requests that update Python codepython:idiomsRefactors toward idiomatic Python (PEP conventions, stdlib idioms)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions