6 min readRishi

The Pipeline Was Green Because the Test Stage Never Ran

The Pipeline Was Green Because the Test Stage Never Ran

The build was green. Production had a null reference the tests cover. The test stage had not run. Azure DevOps treats a skipped stage as a successful pipeline unless you tell it otherwise. A condition that was true on the Tuesday you wrote it — "only test on main" — is false on the pull request, the stage skips, and the PR policy sees green.

This is a different failure from a flaky test. A flaky test is red sometimes. A skipped stage is never red. Nobody gets paged. The only signal is a stage in the timeline with a hollow circle, and people who look at the badge do not open the timeline.

Microsoft's conditions docs are the source for succeeded(), always(), and stageDependencies. The trap is not syntax. It is a condition that is false on the pipeline you use to merge.

How a stage skips without failing

A stage skips when its condition is false, or when a dependency was skipped and the default condition (succeeded()) is not met, or when a runtime parameter said so. The pipeline result stays succeeded.

On a pull request, Build.SourceBranch is refs/pull/.../merge, not refs/heads/main. A stage gated on main therefore does not run on the PR. The author thinks tests ran because the pipeline is required and it passed.

The condition you usually want on a PR validation pipeline is succeeded(). Branch filters belong on the trigger, or on a stage that is explicitly optional, such as a production deploy.

A skeleton that cannot skip Test on a PR

One pipeline, two entry points. pr runs Build → Test → Gate. trigger on main does the same, and you may add a deploy stage after Gate that is allowed to be main-only.

trigger:
  branches:
    include:
      - main

pr:
  branches:
    include:
      - main

stages:
  - stage: Build
    jobs:
      - job: Compile
        pool:
          vmImage: ubuntu-latest
        steps:
          - script: dotnet build --configuration Release
            displayName: Build

  - stage: Test
    dependsOn: Build
    condition: succeeded()
    jobs:
      - job: Unit
        pool:
          vmImage: ubuntu-latest
        steps:
          - script: dotnet test --no-build --configuration Release
            displayName: Unit tests

  - stage: Gate
    dependsOn: Test
    condition: always()
    jobs:
      - job: RequireTests
        pool:
          vmImage: ubuntu-latest
        variables:
          testResult: $[ stageDependencies.Test.result ]
        steps:
          - script: |
              if [ "$(testResult)" != "Succeeded" ]; then
                echo "Test stage result is $(testResult)"
                exit 1
              fi
            displayName: Fail the run if Test did not succeed

Build has no extra condition. Test runs if Build succeeded — it does not look at the branch. Gate uses always() so it still exists when Test was skipped, reads stageDependencies.Test.result, and exits 1 unless that value is Succeeded. Skipped, Canceled, and Failed all fail the pipeline. Without always(), Gate skips too, and you are back to a green run.

Do not put eq(variables['Build.SourceBranch'], 'refs/heads/main') on Test. If tests are expensive, use a path filter on pr or a parameter that Gate still enforces. A skipped required Test stage is how this bug returns.

PR vs main: put the filter on the trigger

IntentWhere it goesWhat not to do
Run CI on PRs into mainpr: branches: include: [main]Condition Test on Build.SourceBranch
Run CI on pushes to maintrigger: branches: include: [main]Same as above
Deploy only from mainA later stage with a branch condition, after GateReuse that condition on Test
Skip the whole pipeline for docs-only PRspr: paths: exclude: [...]Skip only the Test stage

If the pipeline should not start, do not start it. If it starts, Test should run.

YAML templates copied from a release pipeline often bring a SourceBranch == main condition into CI. Search the repo for SourceBranch and Build.Reason in conditions. Every hit is a candidate for a skipped Test stage on PRs.

A deploy stage after Gate may use and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main')). On a PR, Deploy skips. That skip is optional work. Gate already proved tests ran. Do not make Deploy the required check name unless you also require Gate.

GitHub Actions is not a free pass

GitHub Actions has the same class of bug with if: github.ref == 'refs/heads/main' on a test job listed in required checks — a skipped required job can fail the check or pass depending on the settings. Azure DevOps skipped stages pass by default. That difference is why a team that moved from GitHub assumes the badge means tests ran.

On a pull_request event, github.ref is not refs/heads/main. The "only test on main" condition skips test on every PR. A GitHub-shaped Gate is a final job with if: always() that fails unless needs.test.result is success. Same skeleton, different YAML. Skipped tests are not green in either system.

Branch policies hide this

A required pipeline that is green satisfies the policy. The policy does not know which stages ran. If Test is optional in YAML, the policy cannot save you. Either:

  • Put tests in the same job as compile, so they cannot skip independently, or
  • Keep the Gate stage and make that the required check name in the branch policy (the pipeline still runs; the policy waits for the run to succeed, and Gate fails the run).

Same-job tests are the blunt version. You lose rerunning only Test. Use Gate when stages are already split. Slot swaps and smoke tests, covered in the slot-swap pipeline, have the same shape one stage later. A skipped smoke test is not a smoke test.

Skip Gate if Test is in the same job as Build with no independent skip condition, or if the pipeline is not a merge requirement. Do not add Gate to a release pipeline whose Test stage is supposed to skip unless Gate reads that parameter. Silent skip is the bug.

What to click before you trust the badge

Open the run. Every required stage should be filled green, not outlined. A hollow stage is a stage that did not run. Add that glance to the PR checklist for one week and you will find the conditions that were copied from a deploy pipeline into a CI pipeline.

  1. Search YAML for SourceBranch, Build.Reason, and condition: on Test.
  2. Open the latest PR run. Confirm Test is solid green, not hollow.
  3. Confirm Gate (or the job that contains tests) is the check the branch policy requires.
  4. Push a PR that fails a unit test and confirm the pipeline is red. Then push a PR that would skip Test under the old condition and confirm Gate is red.

If step 4 is green, you are looking at this bug.

Keep reading

Newsletter

New posts, straight to your inbox

One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.

Comments

  • No comments yet. Be the first.