Automatically enrich pytest JUnit XML reports with AI failure analysis
Jenkins Job Insight includes a pytest integration that automatically enriches JUnit XML reports with AI-powered failure analysis. When your tests fail, the analysis is injected directly into the XML as structured properties.
The AI analysis is injected as <property> elements under each failed <testcase>:
<testcase classname="tests.test_api" name="test_login" time="1.234"> <failure message="AssertionError: Invalid credentials"> Traceback (most recent call last): ... </failure> <properties> <property name="ai_classification" value="CODE ISSUE"/> <property name="ai_details" value="The test expects username 'admin' but the code validates against 'administrator'. This is a hardcoded credential mismatch."/> <property name="ai_code_fix_file" value="src/auth.py"/> <property name="ai_code_fix_line" value="45"/> <property name="ai_code_fix_change" value="Change VALID_USERNAME from 'administrator' to 'admin'"/> </properties> <system-out>Classification: CODE ISSUEThe test expects username 'admin' but the code validates against 'administrator'.This is a hardcoded credential mismatch.Code Fix: File: src/auth.py Line: 45 Change: Change VALID_USERNAME from 'administrator' to 'admin' </system-out></testcase>
def pytest_sessionstart(session): """Set up AI analysis if --analyze-with-ai is passed.""" if session.config.option.analyze_with_ai: setup_ai_analysis(session)@pytest.hookimpl(trylast=True)def pytest_sessionfinish(session, exitstatus): """Enrich JUnit XML with AI analysis when tests fail. Only runs when exitstatus indicates test failures (exit code != 0). Skips enrichment when all tests pass or execution was interrupted. """ if session.config.option.analyze_with_ai: if exitstatus == 0: logger.info( "No test failures (exit code %d), skipping AI analysis", exitstatus ) else: try: enrich_junit_xml(session) except Exception: logger.exception("Failed to enrich JUnit XML, original preserved")
post { always { junit 'report.xml' script { def report = readFile('report.xml') if (report.contains('ai_classification')) { echo "AI analysis available in test report" } } }}