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
Practice
test gates in a software delivery pipeline?Solution
Step 1: Understand the role of test gates
Test gates act as checkpoints that verify if tests have passed successfully before allowing the pipeline to continue.Step 2: Differentiate from other pipeline actions
Deploying software or writing tests are separate activities, not the main function of test gates.Final Answer:
To check if tests pass before moving to the next stage -> Option BQuick Check:
Test gates = verify tests pass [OK]
- Confusing test gates with deployment steps
- Thinking test gates create tests
- Assuming test gates handle user feedback
Solution
Step 1: Recall standard pipeline flow
The usual order is to first build the software, then run tests, deploy to an environment, and finally release to users.Step 2: Match options to this order
Build -> Test -> Deploy -> Release matches this sequence exactly.Final Answer:
Build -> Test -> Deploy -> Release -> Option DQuick Check:
Build before Test before Deploy [OK]
- Swapping build and test stages
- Deploying before testing
- Releasing before deployment
stages:
- build
- test
- deploy
test_gate:
stage: test
condition: tests_passed == true
action: continue_pipeline
If tests_passed is false, what happens next?Solution
Step 1: Analyze the test gate condition
The test gate checks iftests_passed == true. If false, the condition fails.Step 2: Understand pipeline behavior on failed test gate
When the condition fails, the pipeline stops and does not continue to the deploy stage.Final Answer:
Pipeline stops and does not proceed to deploy -> Option AQuick Check:
Failed test gate = pipeline stops [OK]
- Assuming pipeline skips tests
- Thinking pipeline restarts automatically
- Believing deploy runs despite failed tests
stages: - build - test - deploy test_gate: stage: test condition: tests_passed action: continue_pipeline But the pipeline always proceeds even when tests fail. What is the likely error?
Solution
Step 1: Examine the test gate condition
The condition usestests_passedalone, which may not evaluate correctly if it's not a boolean.Step 2: Correct the condition syntax
Explicitly checkingtests_passed == trueensures the gate only passes when tests truly pass.Final Answer:
The condition should explicitly check for true, e.g., tests_passed == true -> Option AQuick Check:
Explicit condition check fixes gate logic [OK]
- Ignoring condition syntax errors
- Assuming missing stages cause gate failure
- Confusing action names in test gates
1. Build 2. Unit Test 3. Integration Test 4. Deploy Test gates are set after Unit Test and Integration Test stages. If the Integration Test gate fails, what should happen to ensure safe deployment?
Solution
Step 1: Understand test gate purpose after Integration Test
The gate ensures that integration tests pass before deployment to avoid faulty releases.Step 2: Determine correct pipeline behavior on gate failure
If the Integration Test gate fails, the pipeline must stop to prevent deploying broken software.Final Answer:
Stop the pipeline and prevent deployment -> Option CQuick Check:
Failing test gate = stop deployment [OK]
- Ignoring integration test failures
- Assuming automatic pipeline restart
- Deploying despite failed test gates
Ready to prove it?
Pass the test and this pattern moves unknown
