Skip to content

test: fix strict positional parameter enforcement under pytest#2756

Open
Capstan wants to merge 1 commit into
googleapis:mainfrom
Capstan:fix-positional-enforcement
Open

test: fix strict positional parameter enforcement under pytest#2756
Capstan wants to merge 1 commit into
googleapis:mainfrom
Capstan:fix-positional-enforcement

Conversation

@Capstan
Copy link
Copy Markdown

@Capstan Capstan commented May 31, 2026

Description

This PR fixes a test suite configuration bug where strict positional parameter checking was silently bypassed when running the tests under pytest.

Problem

Historically, strict positional parameter checking was enabled during test setup by setting util.positional_parameters_enforcement = "EXCEPTION" inside a setup_package() hook inside tests/__init__.py.

However, modern test runs in this repository (including through nox sessions) run under pytest. Unlike the legacy nose test runner, pytest does not automatically execute package-level setup_package() hooks.

Because this hook was ignored:

  1. positional_parameters_enforcement remained in its default warning-only state.
  2. Tests specifically designed to assert strict positional parameter failures (which expect TypeErrors to be thrown) were being bypassed or would fail if executed directly.

Solution

  1. Moved the setting of util.positional_parameters_enforcement = "EXCEPTION" to the module level of tests/__init__.py. This ensures that the strict check environment is correctly initialized the moment pytest imports the tests package.
  2. Completely removed the dead nose-legacy setup_package() hook to keep the codebase clean and free of deprecated test runner baggage.

Verification

Ran the strict positional test case directly using pytest on a clean virtual environment:

pytest tests/test_discovery.py -k test_tests_should_be_run_with_strict_positional_enforcement
  • Before fix: Test failed with AssertionError (only warning log generated, no exception thrown).
  • After fix: Test successfully passes (raises expected TypeError and asserts it correctly).

Fixes #2755

@Capstan Capstan requested a review from a team as a code owner May 31, 2026 01:32
@product-auto-label product-auto-label Bot added the size: xs Pull request size is extra small. label May 31, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request simplifies the test initialization in tests/__init__.py by removing the setup_package function and executing the configuration directly at the module level. The review feedback suggests using a predefined constant instead of a hardcoded string to prevent typos and improve maintainability.

Comment thread tests/__init__.py Outdated
Historically, `positional_parameters_enforcement = "EXCEPTION"` was set inside `setup_package()` hook in `tests/__init__.py`. However, this hook is not automatically called by modern test runners like `pytest` when running tests, causing strict positional argument validation tests to be silently bypassed.

Moving this setup to the module level in `tests/__init__.py` ensures that strict positional parameter enforcement is active whenever the tests package is imported, correctly running tests under pytest with positional enforcement enabled.

#jetski

AI_USAGE=true
@Capstan Capstan force-pushed the fix-positional-enforcement branch from dee0648 to 19b52f9 Compare May 31, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: xs Pull request size is extra small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: strict positional parameter enforcement is ignored under pytest

1 participant