Conversation
Enhance test framework with pytest hooks, fixtures, and utilities. Changes: - Add pytest hooks for automatic attachment on test failure - Add Allure utilities for result verification in tests - Add unit marker to pytest.ini for better test classification - Improve Makefile with separate 'results' target - Add test_per_call_attach.py (was disabled, now enabled) - Add test_manual_attach.py for external API testing - Add test_spotify_attachment_features.py for integration testing New Files: - tests/conftest.py - Pytest hooks and fixtures * api_client fixture with node attachment * pytest_runtest_makereport hook for auto-attachment * require_alluredir fixture * assert_truncated_response_after_test fixture - tests/utils_allure.py - Allure result utilities * wait_for_result_with_label() - Poll for results * find_attachment_path() - Locate attachments * find_attachment_by_name_in_result() - Find specific attachments Benefits: - Automatic failure debugging with HTTP exchange attachments - Better test organization with unit/integration markers - Cleaner test execution workflow - Allure result verification utilities for testing All 20 tests passing (4 new tests added).
Remove test_spotify_attachment_features.py and comment out assert_truncated_response_after_test fixture due to race condition in CI where Allure results may not be flushed to disk before teardown runs. The fixture was demonstrating advanced usage but has timing issues. The same functionality is already tested in test_truncation_and_payload_sanitization.py which works reliably. Changes: - Remove tests/spotify/test_spotify_attachment_features.py - Comment out assert_truncated_response_after_test fixture - Add note about race condition issue All 19 tests passing.
Add Test Infrastructure ImprovementsSummaryThis PR enhances the test framework with pytest hooks, fixtures, and utilities to improve test organization, debugging capabilities, and Allure integration. Problem Statement1. Limited Test Debugging
2. Test Organization
3. Allure Verification
SolutionFeature 1: Pytest Hooks and FixturesNew File: Centralized pytest configuration with powerful fixtures: @pytest.fixture
def api_client(request):
"""
Provides APIClient with automatic node attachment.
Used by pytest hook for failure debugging.
"""
client = APIClient(base_url="https://api.example.com", token=None)
request.node._api_client = client
return client
def pytest_runtest_makereport(item, call):
"""
Auto-attaches HTTP exchanges on test failure.
No manual attach=True needed!
"""
if call.when != "call" or call.excinfo is None:
return
api_client = getattr(item, "_api_client", None)
if isinstance(api_client, APIClient):
api_client._attach_last_exchange_to_allure()Benefits:
Feature 2: Allure UtilitiesNew File: Utilities for Allure result verification: def wait_for_result_with_label(allure_dir, label_name, label_value, timeout=12.0):
"""Poll Allure results for a test with specific label."""
# Useful for testing truncation/attachment features
def find_attachment_path(res, allure_dir, name):
"""Locate attachment files in Allure results."""
# Returns full path to attachment file
def find_attachment_by_name_in_result(res, allure_dir, attachment_name):
"""Find specific attachments in test results."""
# Enables verification of attachment contentBenefits:
Feature 3: Better Test OrganizationModified: markers =
integration: mark test as an integration test (vs. unit)
unit: mark test as a unit test # NEWUsage: # Run only unit tests
pytest -m unit
# Run only integration tests
pytest -m integration
# Run everything except integration
pytest -m "not integration"Feature 4: Improved Build WorkflowModified: # New target: Generate results without running report
results:
poetry run pytest --maxfail=1 --disable-warnings --alluredir=allure-results
# Modified: Separate generation from execution
report:
allure generate allure-results --clean -o allure-report
# Updated: Use results instead of test
all: clean install results reportBenefits:
New Test Files1.
|
Enhance test framework with pytest hooks, fixtures, and utilities.
Changes:
New Files:
tests/conftest.py - Pytest hooks and fixtures
tests/utils_allure.py - Allure result utilities
Benefits:
All 20 tests passing (4 new tests added).