Tests and Workflow Logic

By Syncfusion®, Inc. · Last updated

Show test failures on the pull request, run tests against a real database, pass values between jobs, and skip runs for docs-only changes.

What you will learn

  • Publish a test report that shows the failing test
  • Run a step even after a failure
  • Give tests a database with a service container
  • Pass a value from one job to the next
  • Skip runs for docs-only changes

Tests and Workflow Logic

Your pipeline runs tests. But real teams hit these problems next:

  • A test fails, and you must dig through a long log to find which one.
  • Some tests need a real database.
  • One job makes a value, like a version number, and the next job needs it.
  • A one-word fix to the README starts a 9-minute run.

You will fix each one, on both platforms.

Simple lesson: a good pipeline tells you what broke, fast, and runs only when it should.

A Real-World Example

Think about a school exam result.

  • A bad result card says only "Fail". You do not know which subject.
  • A good card lists each subject with marks. You see "Maths: 31" in one second.
  • The practical exam needs a real lab, not a picture of one.
  • If only the cover page of a notebook changed, nobody re-checks the whole notebook.

Now map it:

  • Marks per subject = a test report.
  • Real lab = a service container, like a database.
  • Cover page only = a docs-only change, which can skip the run.

Simple lesson: show results per test, give tests what they need, skip work that does not matter.

Test Reports

  • Your test tool can write results to a file. A common format is JUnit XML.
  • A report step reads that file and shows each test on the run page.
  • Failing tests show up with their name and message. No log digging.
StackTest command that writes a report
React (Vitest)npx vitest run --reporter=junit --outputFile=test-results.xml
.NET (xUnit)dotnet test --logger trx --results-directory test-results
PlatformStep that shows the report
GitHub Actionsdorny/test-reporter
Azure PipelinesPublishTestResults@2 (shows a Tests tab)

Simple lesson: write the results to a file, then publish that file.

Run a Step Even After a Failure

Normal rule: when one step fails, the rest of the job is skipped.

  • That is right for build and deploy steps.
  • It is wrong for the report step. You need the report most when tests fail.

Change the rule for one step with a condition:

PlatformRun after a failure tooRun only on failure
GitHub Actionsif: ${{ !cancelled() }}if: failure()
Azure Pipelinescondition: succeededOrFailed()condition: failed()

GitHub also has if: always(). It even runs when someone cancels the run. !cancelled() is usually what you want.

Simple lesson: a condition decides if a step runs. Reports and clean-up steps run even after a failure.

A Database for the Tests

Some tests talk to a real database. The runner starts empty, so you add one.

  • A service container is an extra container that runs next to your job.
  • It starts before your steps and stops after the job.
  • Your tests reach it at localhost on the port you open.
  • The health options make the job wait until Postgres is ready.
  • Without them, the first test may run before the database can accept connections.

Simple lesson: a service container gives each run its own fresh database.

Pass a Value to the Next Job

Each job runs on a fresh machine. A variable set in one job is gone in the next.

To pass a value:

  1. A step writes it as an output.
  2. The job lists that output.
  3. The next job waits for it and reads it.
PlatformWrite itRead it in the next job
GitHub Actionsecho "version=1.4.42" >> "$GITHUB_OUTPUT"${{ needs.version.outputs.version }}
Azure Pipelinesecho "##vso[task.setvariable variable=version;isOutput=true]1.4.42"$[ dependencies.version.outputs['v.version'] ]

The names must match all the way: the step id, the output name and the job name. One typo gives an empty value, not an error.

Simple lesson: to share a value, make it an output, then read it through needs or dependencies.

Skip Runs for Docs-Only Changes

  • A path filter says which file changes start a run.
  • paths-ignore (GitHub) or paths: exclude (Azure) lists files that do not start one.
  • A change to code still runs everything.

Careful with required checks. If a check is required on main but the run is skipped, GitHub can wait for it forever. Many teams use path filters only on extra workflows, not the main required one.

Simple lesson: skip runs that cannot change the result.

  1. Open your copy of the starter repo (see Your First Pipeline).
  2. Change the test command to write a JUnit report, and add the report step with if: ${{ !cancelled() }}.
  3. Break one test on purpose: change an expected value in price.test.ts (or PriceTests.cs).
  4. Push to a new branch and open a pull request.

What you should see: a red run, and a "Test results" check that names the broken test. Undo your change after.

Quick Check

Cheat Sheet

GitHub Actions:

on:
  push:
    branches: [main]
    paths-ignore: ['docs/**', '**.md']

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_PASSWORD: postgres
        ports: ['5432:5432']
        options: >-
          --health-cmd pg_isready --health-interval 5s --health-retries 10
    outputs:
      version: ${{ steps.v.outputs.version }}
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npx vitest run --reporter=junit --outputFile=test-results.xml
      - name: Publish the test report
        if: ${{ !cancelled() }}
        uses: dorny/test-reporter@v1
        with:
          name: Test results
          path: test-results.xml
          reporter: java-junit
      - id: v
        run: echo "version=1.4.${{ github.run_number }}" >> "$GITHUB_OUTPUT"

  deploy:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying ${{ needs.test.outputs.version }}"

Azure Pipelines:

trigger:
  branches:
    include: [main]
  paths:
    exclude: ['docs/*', '*.md']

resources:
  containers:
    - container: postgres
      image: postgres:17
      env:
        POSTGRES_PASSWORD: postgres
      ports: ['5432:5432']

jobs:
  - job: test
    services:
      postgres: postgres
    steps:
      - script: dotnet test --logger trx --results-directory test-results
      - task: PublishTestResults@2
        condition: succeededOrFailed()
        inputs:
          testResultsFormat: VSTest
          testResultsFiles: 'test-results/*.trx'
      - script: echo "##vso[task.setvariable variable=version;isOutput=true]1.4.$(Build.BuildId)"
        name: v

  - job: deploy
    dependsOn: test
    variables:
      version: $[ dependencies.test.outputs['v.version'] ]
    steps:
      - script: echo "Deploying $(version)"

Common mistakes

MistakeWrong ideaCorrect ideaWhat goes wrong
Report step with no conditionSteps always runA failure skips later stepsNo report when you need it
continue-on-error on testsIt lets the report runPut the condition on the report stepFailing tests turn green
No health check on the serviceThe database starts at onceIt needs a few seconds"Connection refused"
Output name typoYAML would warn meIt gives an empty valueDeploys "version " with nothing
Path filter on a required checkSkipped means passedA skipped required check can block mergesThe pull request waits forever

What to Remember

  • Write test results to a file. Publish them with a report step.
  • Conditions decide if a step runs: !cancelled(), failure(), succeededOrFailed(), failed().
  • Service containers give tests a real database at localhost.
  • Outputs carry a value from one job to the next. Names must match.
  • Path filters skip runs that cannot change the result.

Exercises

Next: Faster Pipelines