Why CI/CD integrates testing into delivery in Testing Fundamentals - Automation Benefits in Action
import pytest # Sample function to test def add(a, b): return a + b # Test function for add def test_add_positive_numbers(): assert add(2, 3) == 5 def test_add_negative_numbers(): assert add(-1, -1) == -2 def test_add_zero(): assert add(0, 0) == 0 if __name__ == '__main__': pytest.main(['-v', '--cov=.', '--cov-report=term-missing'])
This script defines a simple function add and three test functions to verify its behavior with positive numbers, negative numbers, and zero.
We use pytest as the test framework because it is easy to write and read tests. The assert statements check that the function returns expected results.
The pytest.main() call runs the tests with verbose output and coverage reporting, which helps verify that tests are executed and code coverage is measured.
In a CI/CD pipeline, this script would be triggered automatically after code changes, ensuring tests run every time code is delivered.
Now add data-driven testing with 3 different input pairs for the add function
Practice
Solution
Step 1: Understand the purpose of CI/CD testing
Testing in CI/CD helps catch bugs early before software reaches users.Step 2: Recognize the benefit of early problem detection
Finding issues early improves software quality and reduces costly fixes later.Final Answer:
To find problems early and improve software quality -> Option DQuick Check:
Early testing = better quality [OK]
- Thinking testing slows delivery
- Believing testing is optional
- Assuming testing increases manual work
Solution
Step 1: Identify correct CI/CD pipeline syntax
Most CI/CD tools use 'stages' to define order: build, test, deploy.Step 2: Check the order of stages
Testing should come after build and before deploy for proper flow.Final Answer:
stages: - build - test - deploy -> Option BQuick Check:
Stages order: build, test, deploy [OK]
- Mixing steps and stages keywords
- Placing test after deploy
- Using unsupported keywords like phases
Solution
Step 1: Understand the role of automated tests in CI/CD
Automated tests verify code correctness quickly and reliably before deployment.Step 2: Recognize the impact on software quality and user experience
Running tests early prevents bugs from reaching users, improving reliability.Final Answer:
To catch bugs early and prevent faulty code from reaching users -> Option CQuick Check:
Tests before deploy = fewer bugs in production [OK]
- Thinking tests delay deployment on purpose
- Confusing automated tests with manual effort
- Believing tests replace code reviews
stages:
- build
- test
build_job:
stage: build
script:
- echo "Building..."
test_job:
stage: test
script:
- exit 1What will happen when this pipeline runs?
Solution
Step 1: Analyze job order by stages
Build stage runs first, then test stage runs after build completes.Step 2: Check test job script
Test job script exits with code 1, which means failure.Final Answer:
The build job runs, then the test job fails, stopping the pipeline -> Option AQuick Check:
Failing test stops pipeline [OK]
- Assuming test runs before build
- Thinking pipeline ignores failures
- Believing all jobs always succeed
test_job:
stage: test
script:
- pytest tests/
only:
- main
- developWhy might tests not run on a feature branch?
Solution
Step 1: Understand 'only' keyword effect
'only' restricts job runs to specified branches: main and develop.Step 2: Check branch context
Feature branches are not listed, so tests won't run there.Final Answer:
Because the 'only' keyword limits runs to 'main' and 'develop' branches -> Option AQuick Check:
'only' limits branches [OK]
- Assuming tests run on all branches
- Blaming missing pytest without checking config
- Ignoring branch filters in pipeline
Ready to prove it?
Pass the test and this pattern moves unknown
