0
0
Testing Fundamentalstesting~15 mins

Pipeline stages and test gates in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify pipeline stages and test gates enforce quality checks
Preconditions (2)
Step 1: Trigger a pipeline run with a code change
Step 2: Observe the Build stage completes successfully
Step 3: Verify Unit Test stage runs and passes all tests
Step 4: If Unit Test passes, verify Integration Test stage runs
Step 5: If Integration Test passes, verify Deploy stage runs
Step 6: If any test stage fails, verify pipeline stops and does not proceed to next stage
✅ Expected Result: Pipeline stages execute in order with test gates blocking progression on failures. Deployment only occurs if all tests pass.
Automation Requirements - Python with pytest and requests
Assertions Needed:
Build stage status is success
Unit Test stage status is success
Integration Test stage status is success
Deploy stage runs only if all previous stages succeed
Pipeline stops immediately on any test failure
Best Practices:
Use API calls to query pipeline stage statuses
Implement explicit checks for each stage result
Fail test immediately on any stage failure
Use clear and descriptive assertion messages
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 4 Pitfalls
{'mistake': 'Hardcoding stage statuses without querying live pipeline data', 'why_bad': "The test won't reflect the real pipeline state and may pass incorrectly.", 'correct_approach': 'Use API calls to get current pipeline stage statuses dynamically.'}
Not checking if stages exist before asserting status
Ignoring pipeline stop behavior on failure
Using sleep or fixed waits instead of checking stage status
Bonus Challenge

Now add data-driven testing with 3 different pipeline IDs to verify multiple pipelines

Show Hint