Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Test reporting in pipelines in Testing Fundamentals - Build an Automation Script

Choose your learning style10 modes available
Verify test report generation in CI pipeline
Preconditions (3)
Step 1: Trigger the CI pipeline to run automated tests
Step 2: Wait for the tests to complete
Step 3: Locate the generated test report file in the pipeline artifacts
Step 4: Open the test report file
Step 5: Verify the report contains the test suite name
Step 6: Verify the report lists all executed test cases
Step 7: Verify the report shows the pass/fail status for each test case
Step 8: Verify the report includes summary statistics (total tests, passed, failed)
✅ Expected Result: The test report file is generated and contains accurate details of all executed tests with correct pass/fail status and summary statistics.
Automation Requirements - Python unittest with xmlrunner
Assertions Needed:
Test report file exists after test run
Test report XML contains test suite name
Test report XML lists all test cases executed
Test report XML shows correct pass/fail status for each test
Test report XML includes summary with total, passed, and failed counts
Best Practices:
Use explicit waits or polling to ensure test report file is generated before reading
Parse XML report using standard XML libraries for validation
Keep test names descriptive for clear reporting
Integrate test report generation as part of the test run command
Store test reports as pipeline artifacts for later inspection
Automated Solution
Testing Fundamentals
import unittest
import xml.etree.ElementTree as ET
import os
import time

import xmlrunner

class SampleTests(unittest.TestCase):
    def test_pass(self):
        self.assertTrue(True)

    def test_fail(self):
        self.assertTrue(False)


def run_tests_and_generate_report(report_path):
    with open(report_path, 'wb') as output:
        unittest.main(testRunner=xmlrunner.XMLTestRunner(output=output), exit=False)


def wait_for_file(file_path, timeout=10):
    start = time.time()
    while time.time() - start < timeout:
        if os.path.exists(file_path):
            return True
        time.sleep(0.5)
    return False


def validate_report(report_path):
    assert os.path.exists(report_path), f"Report file {report_path} does not exist"

    tree = ET.parse(report_path)
    root = tree.getroot()

    # Check test suite name
    suite_name = root.attrib.get('name')
    assert suite_name == 'SampleTests', f"Expected suite name 'SampleTests', got '{suite_name}'"

    # Collect test cases
    testcases = root.findall('testcase')
    testcase_names = {tc.attrib['name'] for tc in testcases}
    expected_tests = {'test_pass', 'test_fail'}
    assert testcase_names == expected_tests, f"Test cases mismatch. Expected {expected_tests}, got {testcase_names}"

    # Check pass/fail status
    failures = root.findall('testcase/failure')
    failed_tests = {f.getparent().attrib['name'] for f in failures} if failures else set()
    assert 'test_fail' in failed_tests, "Failed test 'test_fail' not found in report"
    assert 'test_pass' not in failed_tests, "Passed test 'test_pass' incorrectly marked as failed"

    # Check summary counts
    tests = int(root.attrib.get('tests', 0))
    failures_count = int(root.attrib.get('failures', 0))
    assert tests == 2, f"Expected 2 tests, got {tests}"
    assert failures_count == 1, f"Expected 1 failure, got {failures_count}"


if __name__ == '__main__':
    report_file = 'test-reports/sample-tests.xml'
    os.makedirs(os.path.dirname(report_file), exist_ok=True)

    run_tests_and_generate_report(report_file)

    if wait_for_file(report_file):
        validate_report(report_file)
        print('Test report validation passed.')
    else:
        raise FileNotFoundError(f'Test report {report_file} was not generated in time.')

This script defines two simple tests: one that passes and one that fails.

The run_tests_and_generate_report function runs these tests and generates an XML report using xmlrunner.

The wait_for_file function waits up to 10 seconds for the report file to appear, simulating waiting for pipeline artifact generation.

The validate_report function parses the XML report and checks:

  • The test suite name matches the test class name.
  • All expected test cases are listed.
  • The pass/fail status is correct for each test.
  • The summary counts for total tests and failures are accurate.

This approach ensures the test report generated in a pipeline is complete and accurate.

Common Mistakes - 4 Pitfalls
Not waiting for the test report file before reading it
Hardcoding file paths without creating directories
Parsing test report as plain text instead of XML
Not verifying summary statistics in the report
Bonus Challenge

Now add data-driven testing with 3 different inputs and verify the test report reflects all runs.

Show Hint

Practice

(1/5)
1. What is the main purpose of test reporting in pipelines?
easy
A. To increase the size of the codebase
B. To write code faster
C. To automatically show test results after code changes
D. To deploy software without testing

Solution

  1. Step 1: Understand the role of test reporting

    Test reporting in pipelines is designed to show test results automatically after code changes are made.
  2. Step 2: Eliminate unrelated options

    Writing code faster, deploying without testing, or increasing code size are not goals of test reporting.
  3. Final Answer:

    To automatically show test results after code changes -> Option C
  4. Quick Check:

    Test reporting = automatic test results display [OK]
Hint: Test reports show results automatically after code changes [OK]
Common Mistakes:
  • Thinking test reports speed up coding
  • Confusing deployment with reporting
  • Assuming reports increase code size
2. Which of the following is a common format used for test reports in pipelines?
easy
A. JUnit XML
B. CSV
C. Markdown
D. Plain text without structure

Solution

  1. Step 1: Identify standard test report formats

    JUnit XML is a widely accepted format for test reports in pipelines because it is structured and easy to integrate.
  2. Step 2: Compare other options

    CSV and Markdown are not standard for test reporting, and plain text without structure is not useful for automated tools.
  3. Final Answer:

    JUnit XML -> Option A
  4. Quick Check:

    Standard test report format = JUnit XML [OK]
Hint: JUnit XML is the standard for test reports in pipelines [OK]
Common Mistakes:
  • Choosing CSV or Markdown as test report formats
  • Ignoring structured formats
  • Assuming plain text is enough
3. Consider this pipeline step output snippet:
<testsuite tests="3" failures="1" errors="0">
  <testcase classname="LoginTests" name="test_valid_login"/>
  <testcase classname="LoginTests" name="test_invalid_login">
    <failure message="AssertionError"/>
  </testcase>
  <testcase classname="LoginTests" name="test_logout"/>
</testsuite>

What does this report indicate?
medium
A. All tests passed successfully
B. Two tests failed, one passed
C. No tests were run
D. One test failed, two passed

Solution

  1. Step 1: Analyze the testsuite attributes

    The attribute tests="3" means 3 tests ran; failures="1" means 1 test failed; errors="0" means no errors.
  2. Step 2: Check individual testcases

    One testcase has a failure tag, confirming one failure; the other two have no failure tags, so they passed.
  3. Final Answer:

    One test failed, two passed -> Option D
  4. Quick Check:

    Failures = 1, Tests = 3 means 1 fail, 2 pass [OK]
Hint: Check failures attribute to count failed tests [OK]
Common Mistakes:
  • Assuming all tests passed
  • Confusing failures with errors
  • Misreading the number of tests run
4. A pipeline test report shows no test results after a run. What is the most likely cause?
medium
A. Test report file path is incorrect or missing
B. All tests passed perfectly
C. Pipeline skipped the test stage intentionally
D. Test report format is too detailed

Solution

  1. Step 1: Understand why no test results appear

    If no test results show, often the report file was not found or generated due to wrong path or missing file.
  2. Step 2: Evaluate other options

    All tests passing would still produce a report; skipping tests would be visible in logs; format detail does not prevent report display.
  3. Final Answer:

    Test report file path is incorrect or missing -> Option A
  4. Quick Check:

    Missing report file = no results shown [OK]
Hint: Check report file path if no results appear [OK]
Common Mistakes:
  • Assuming no results means all tests passed
  • Ignoring pipeline logs for skipped tests
  • Blaming report format detail
5. You want to improve your pipeline's test reporting to help the team fix problems faster. Which approach is best?
hard
A. Only run tests manually and email results to the team
B. Use a standard format like JUnit XML and integrate reports with the pipeline dashboard
C. Skip test reporting to save pipeline time
D. Write test results in plain text files without structure

Solution

  1. Step 1: Identify best practices for test reporting

    Using a standard format like JUnit XML allows easy integration with pipeline dashboards, making results clear and accessible.
  2. Step 2: Compare other options

    Manual tests and emails slow feedback; skipping reports hides problems; plain text lacks structure for automation.
  3. Final Answer:

    Use a standard format like JUnit XML and integrate reports with the pipeline dashboard -> Option B
  4. Quick Check:

    Standard format + integration = faster fixes [OK]
Hint: Integrate standard reports with dashboard for quick fixes [OK]
Common Mistakes:
  • Relying on manual test runs
  • Skipping reports to save time
  • Using unstructured plain text files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard