测试: 在 pytest 下,严格的位置参数强制执行会被忽略
作者: Capstan创建于 2026年5月31日更新于 2026年5月31日
Steps to reproduce
- Check out the
mainbranch ofgoogle-api-Python-client. - Set up a clean virtual environment and install test dependencies (
pytest,mock,parameterized). - Run the strict positional parameters enforcement test directly using
pytest:
pytest tests/test_discovery.py -k test_tests_should_be_run_with_strict_positional_enforcementCode example
The bug lies in how strict argument enforcement is configured for the test suite in tests/__init__.py.
Historically, the enforcement was configured inside the nose-legacy setup_package() hook:
# tests/__init__.py
from googleapiclient import _helpers as util
def setup_package():
"""Run on testing package."""
util.positional_parameters_enforcement = "EXCEPTION"
Because modern test runs in this repository (including via `nox` sessions) run under **`pytest`**, the `setup_package()` package-level hook is ignored. As a result, the tests run in warning-only mode, causing strict parameter assertion tests to be bypassed and fail with an `AssertionError` instead of raising `TypeError`.
Since the repository has migrated to `pytest`, this legacy hook is dead code. We should configure the variable directly at the package module level and completely remove the unused `setup_package` hook:
```diff
# tests/__init__.py
from googleapiclient import _helpers as util
+util.positional_parameters_enforcement = "EXCEPTION"
- def setup_package():
- """Run on testing package."""
- util.positional_parameters_enforcement = "EXCEPTION"
#### Stack trace
When running the tests using `pytest`, the test fails with the following output:内容来源: googleapis/google-api-python-client