Continuous Integration testing in Testing Fundamentals - Build an Automation Script
import requests import time def trigger_commit_and_check_ci(repo_api_url, ci_status_api_url, commit_payload, headers): # Step 1: Commit code via API (simulate commit) commit_response = requests.post(repo_api_url, json=commit_payload, headers=headers) if commit_response.status_code != 201: raise Exception(f"Commit failed with status {commit_response.status_code}") commit_id = commit_response.json().get('commit_id') # Step 2: Poll CI status API until pipeline completes for _ in range(30): # wait up to ~3 minutes ci_response = requests.get(f"{ci_status_api_url}/{commit_id}", headers=headers) if ci_response.status_code == 200: status = ci_response.json().get('status') if status in ['passed', 'failed']: return status time.sleep(6) raise TimeoutError("CI pipeline did not complete in expected time") # Example usage (replace URLs and payload with real values) # repo_api_url = 'https://api.example.com/repos/myproject/commits' # ci_status_api_url = 'https://ci.example.com/api/status' # commit_payload = {"message": "Test commit", "changes": [{"file": "test.txt", "content": "Hello"}]} # headers = {"Authorization": "Bearer token"} # result = trigger_commit_and_check_ci(repo_api_url, ci_status_api_url, commit_payload, headers) # assert result == 'passed', f"CI tests failed with status {result}"
This script simulates a code commit by sending a POST request to the repository API. It then polls the CI tool's status API to check if the pipeline triggered by that commit has completed. The polling waits up to about 3 minutes, checking every 6 seconds. Once the pipeline finishes, it returns the status, which should be either 'passed' or 'failed'. The assertion at the end ensures the tests passed.
This approach automates the manual test case steps by programmatically committing code and verifying the CI pipeline result via APIs, which is a common practice in Continuous Integration testing.
Now add data-driven testing by committing three different code changes and verifying the CI pipeline for each
Practice
Solution
Step 1: Understand CI testing purpose
Continuous Integration testing runs tests automatically whenever code changes happen.Step 2: Compare options with CI goals
Only To automatically run tests on every code change to find bugs early describes automatic testing on code changes to catch bugs early.Final Answer:
To automatically run tests on every code change to find bugs early -> Option AQuick Check:
CI testing = automatic tests on code changes [OK]
- Thinking CI testing is manual
- Confusing CI with deployment
- Believing testing happens after development only
Solution
Step 1: Identify common CI syntax
CI pipelines often use ascriptkey to run commands likepytestfor tests.Step 2: Check options for valid syntax
Only script: pytest uses correct syntax:script: pytestruns tests properly.Final Answer:
script: pytest -> Option DQuick Check:
CI test command syntax = script: pytest [OK]
- Using invalid keys like 'run_tests' or 'test: run'
- Writing commands without proper keys
- Confusing natural language with config syntax
steps: - script: echo "Run tests" - script: pytest tests/ - script: echo "Tests complete"
What will be the output when this CI pipeline runs?
Solution
Step 1: Analyze each script step output
The first script prints "Run tests", the second runs pytest showing test results, the third prints "Tests complete".Step 2: Combine outputs in order
All outputs appear in sequence: first message, pytest output, then final message.Final Answer:
Run tests\n(pytest runs tests output)\nTests complete -> Option CQuick Check:
CI scripts run sequentially showing all outputs [OK]
- Ignoring pytest output
- Assuming only first or last script runs
- Thinking scripts are skipped
script: pytes instead of pytest. What is the best way to fix this error?Solution
Step 1: Identify the typo in the test command
The commandpytesis a misspelling ofpytest, causing the failure.Step 2: Correct the command to fix the pipeline
Fixing the typo topytestensures the tests run properly.Final Answer:
Changepytestopytestin the script command -> Option AQuick Check:
Fix typos in commands to fix CI errors [OK]
- Removing test commands instead of fixing
- Adding duplicate commands without removing errors
- Ignoring errors and rerunning
Solution
Step 1: Understand selective testing in CI
To save time, CI can detect changed files and run only related tests.Step 2: Identify the correct approach
Use a script step that detects changed files and runs tests only on those describes using a script to detect changes and run targeted tests, which is efficient.Final Answer:
Use a script step that detects changed files and runs tests only on those -> Option BQuick Check:
Selective tests on changed files save CI time [OK]
- Running all tests every time wasting resources
- Disabling tests in CI
- Relying on manual testing after deployment
Ready to prove it?
Pass the test and this pattern moves unknown
