Pipeline stages and test gates in Testing Fundamentals - Build an Automation Script
import requests import pytest PIPELINE_API_URL = "https://ci.example.com/api/pipelines/12345/status" @pytest.fixture async def get_pipeline_status(): response = requests.get(PIPELINE_API_URL) response.raise_for_status() return response.json() @pytest.mark.asyncio async def test_pipeline_stages_and_gates(get_pipeline_status): status = get_pipeline_status # Check Build stage build_stage = next((stage for stage in status['stages'] if stage['name'] == 'Build'), None) assert build_stage is not None, "Build stage not found" assert build_stage['status'] == 'success', f"Build stage failed with status {build_stage['status']}" # Check Unit Test stage unit_test_stage = next((stage for stage in status['stages'] if stage['name'] == 'Unit Test'), None) assert unit_test_stage is not None, "Unit Test stage not found" assert unit_test_stage['status'] == 'success', f"Unit Test stage failed with status {unit_test_stage['status']}" # Check Integration Test stage integration_test_stage = next((stage for stage in status['stages'] if stage['name'] == 'Integration Test'), None) assert integration_test_stage is not None, "Integration Test stage not found" assert integration_test_stage['status'] == 'success', f"Integration Test stage failed with status {integration_test_stage['status']}" # Check Deploy stage only runs if all previous stages succeeded deploy_stage = next((stage for stage in status['stages'] if stage['name'] == 'Deploy'), None) assert deploy_stage is not None, "Deploy stage not found" assert deploy_stage['status'] == 'running' or deploy_stage['status'] == 'success', f"Deploy stage did not run properly, status: {deploy_stage['status']}" # Verify pipeline stops on failure (simulate by checking no stages after failure) failed_stages = [stage for stage in status['stages'] if stage['status'] == 'failed'] if failed_stages: failed_index = status['stages'].index(failed_stages[0]) stages_after_failure = status['stages'][failed_index + 1:] assert all(stage['status'] == 'not_run' for stage in stages_after_failure), "Pipeline did not stop after failure"
This test script uses Python with pytest and requests to automate checking a CI/CD pipeline's stages and test gates.
First, it calls the pipeline API to get the current status of all stages.
It then verifies each stage exists and has the expected status. The Build, Unit Test, and Integration Test stages must all succeed.
The Deploy stage should only run if all previous stages succeeded, so its status is checked to be either running or success.
If any stage failed, the test confirms that no subsequent stages ran, ensuring the pipeline stops correctly at the test gate.
Assertions have clear messages to help understand failures.
This approach uses explicit checks and API queries, following best practices for test automation of pipelines.
Now add data-driven testing with 3 different pipeline IDs to verify multiple pipelines