TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Unit, Integration & End-to-End Tests in CI

Unit, Integration & End-to-End Tests in CI

Tests are the heart of continuous integration: they turn "it compiles" into "it still does what it did yesterday". Pipelines run three kinds, and they differ in speed, cost and what they need around them.

Kind Tests Needs In the pipeline
Unit one function or class, dependencies faked nothing every push, first after lint
Integration the code with real dependencies: a database, a queue, another service containers next to the job (GitHub services:, GitLab services:) every push or every PR
End-to-end (E2E) the deployed system through its UI or public API a running environment (preview, staging) after deploy to that environment; often only on main

The test pyramid says to have many unit tests, fewer integration tests and few E2E tests, because each layer up is slower and flakier. The pipeline mirrors it: the wide, fast base runs first and fails fast.

Exit codes and reports

A test runner tells CI the verdict through its exit code, and tells people the details through its output and a report file. Python's built-in unittest shows both:

bash · try it yourself
export PYTHONDONTWRITEBYTECODE=1     # no __pycache__: the edit below happens within the same second
mkdir shop && cd shop
cat > cart.py <<'PY'
def total(prices, discount=0):
    return round(sum(prices) * (1 - discount), 2)
PY
cat > test_cart.py <<'PY'
import unittest
from cart import total

class TotalTest(unittest.TestCase):
    def test_sum(self):
        self.assertEqual(total([1.5, 2.5]), 4.0)
    def test_discount(self):
        self.assertEqual(total([10, 10], discount=0.1), 18.0)
    def test_empty(self):
        self.assertEqual(total([]), 0)

if __name__ == "__main__":
    unittest.main()
PY
python3 -m unittest -q test_cart.py
echo "exit $?"
sed -i 's/(1 - discount)/(1 + discount)/' cart.py        # a bug slips in
python3 -m unittest -q test_cart.py 2>&1 | head -8
echo "exit ${PIPESTATUS[0]}"
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
exit 0
======================================================================
FAIL: test_discount (test_cart.TotalTest.test_discount)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/learner/shop/test_cart.py", line 8, in test_discount
    self.assertEqual(total([10, 10], discount=0.1), 18.0)
AssertionError: 22.0 != 18.0

exit 1

Note ${PIPESTATUS[0]}: piping the runner into head would otherwise report head's exit code (0), and a failing test suite would turn the job green. In a pipeline step, write set -o pipefail or avoid pipes after the test command.

For the details, CI platforms understand JUnit XML reports: GitLab shows failed tests in the merge request when a job publishes artifacts: reports: junit: report.xml, and on GitHub a test-report action or the job summary does the same. pytest writes it with pytest --junitxml=report.xml; most runners for other languages have an equivalent flag.

Integration tests with service containers

Integration tests need real dependencies. Both platforms can start containers next to a job (non-run: needs a runner with Docker):

yaml
# GitHub Actions
jobs:
  integration:
    runs-on: ubuntu-24.04
    services:
      postgres:
        image: postgres:18
        env:
          POSTGRES_PASSWORD: test
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 5s --health-retries 10
    steps:
      - uses: actions/checkout@v7
      - run: make test-integration
        env:
          DATABASE_URL: postgres://postgres:test@localhost:5432/postgres
yaml
# GitLab CI
integration:
  image: python:3.13
  services:
    - name: postgres:18
      alias: db
  variables:
    POSTGRES_PASSWORD: test
    DATABASE_URL: postgres://postgres:test@db:5432/postgres
  script:
    - make test-integration

Use the same major version as production, wait for the service's health check, and keep test data out of shared environments.

Flaky tests and retries: a model

A flaky test passes and fails on the same code. Automatic retries hide flakiness, which is tempting and dangerous: a retry turns a 5 % flaky test green, but it also turns a real intermittent bug green. The model computes how often a suite of independent tests fails without any code change, with and without one retry:

python · try it yourself
def suite_fail(flaky_rates, retries=0):
    p_pass = 1.0
    for p in flaky_rates:
        p_fail_once = p ** (retries + 1)       # must fail every attempt
        p_pass *= 1 - p_fail_once
    return 1 - p_pass

suite = [0.0002] * 200 + [0.05] * 3             # 200 solid tests, 3 badly flaky ones
print(f"no retry : {suite_fail(suite):6.1%} of runs fail for no reason")
print(f"1 retry  : {suite_fail(suite, 1):6.1%}")
print(f"without the 3 flaky tests: {suite_fail(suite[:200]):6.1%}")
print(f"without them, 1 retry:     {suite_fail(suite[:200], 1):6.1%}")
no retry :  17.6% of runs fail for no reason
1 retry  :   0.7%
without the 3 flaky tests:   3.9%
without them, 1 retry:       0.0%

One run in six failing on unchanged code teaches people to press "re-run" without reading the failure. A retry makes the number look fine (0.7 %), but the last two lines show where the failures come from: three badly flaky tests cause more than three quarters of them. Find the few flaky tests, quarantine them (run them in a separate, non-blocking job and file a ticket) and fix them, and the suite is reliable without retries. Track flakiness per test from the JUnit reports; GitLab and most test-analytics tools flag tests whose result changed without a code change.

Test yourself

pytest | tee log.txt fails 3 tests, but the CI step is green. Why?

  1. pytest ignores failures when piped
  2. Without pipefail, the step's exit code is tee's (0), not pytest's
  3. tee deletes the output
  4. CI ignores exit codes

Which tests usually need a running environment and run after a deploy?

  1. Unit tests
  2. Integration tests
  3. End-to-end tests
  4. Lint

What does the model show about automatic retries?

  1. They fix flaky tests
  2. They lower the failure rate but hide flakiness, including real intermittent bugs; quarantining and fixing the few flaky tests is the real cure
  3. They make suites faster
  4. They have no effect