DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Most Popular Technologies for Java Microservices Right Now
  • Using Python Libraries in Java
  • Using Lombok Library With JDK 23
  • Mastering Unit Testing and Test-Driven Development in Java

Trending

  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Grafana Loki Fundamentals and Architecture
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Running Unit Tests in GitHub Actions

Running Unit Tests in GitHub Actions

In this article, we'll show you how to add unit tests to a GitHub Actions workflow and configure custom actions to process the results.

By 
Matthew Casperson user avatar
Matthew Casperson
·
Sep. 22, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.7K Views

Join the DZone community and get the full member experience.

Join For Free

Verifying code changes with unit tests is a critical process in typical development workflows. GitHub Actions provides a number of custom actions to collect and process the results of tests allowing developers to browse the results, debug failed tests, and generate reports.

In this article, I show you how to add unit tests to a GitHub Actions workflow and configure custom actions to process the results.

Getting Started

GitHub Actions is a hosted service, so you all need to get started is a GitHub account. All other dependencies like Software Development Kits (SDKs) are installed during the execution of the GitHub Actions workflow.

Selecting an Action

GitHub Actions relies heavily on third-party actions contributed by the community. A quick Google search shows at least half a dozen actions for processing unit test results, including:

  • action-junit-report
  • publish-unit-test-results
  • junit-report-action
  • test-reporter
  • report-junit-annotations-as-github-actions-annotations

To narrow the selection, you need to consider the following functionality:

  • Does the action support your testing framework? For example, some actions only process JUnit test results, while others include additional formats like TRX.
  • Does the action allow you to fail the workflow based on the presence of failed tests?
  • Does the action annotate the source code with details of test results?
  • Does the action generate a useful report?
  • How many stars does the project have?

After some trial and error, I settled on the test-reporter action, which is demonstrated in this post.

Unit Testing in Java

The workflow file shown below runs tests with Maven and processes the results with the test-reporter action:

name: Java

on:
  push:
  workflow_dispatch:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Set up JDK 1.11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'

      - name: Build
        run: mvn --batch-mode -DskipTests package

      - name: Test
        run: mvn --batch-mode -Dmaven.test.failure.ignore=true test

      - name: Report
        uses: dorny/test-reporter@v1
        if: always()
        with:
          name: Maven Tests
          path: target/surefire-reports/*.xml
          reporter: java-junit
          fail-on-error: true

The Build, Test, and Report steps are important to the testing process.

You start by building the application, but skipping the tests:

      - name: Build
        run: mvn --batch-mode -DskipTests package

Next, you run the tests, allowing the command to pass even if there are failing tests. This allows you to defer the response to failed tests to the test processing action:

      - name: Test
        run: mvn --batch-mode -Dmaven.test.failure.ignore=true test

In the final step, you generate a report from the JUnit XML file.

The if property is set to always run this step, allowing you to generate the report even if the Test step above was set to fail in the event of failed tests.

The fail-on-error property is set to true to fail this workflow if there were failed tests. This is an example of deferring the response to failed tests to the test processing action:

      - name: Report
        uses: dorny/test-reporter@v1
        if: always()
        with:
          name: Maven Tests
          path: target/surefire-reports/*.xml
          reporter: java-junit
          fail-on-error: true

The test results are displayed as a link under the original workflow results:

Java Tests Results

Failing tests show additional details such as the name of the test, the test result, and the raw test output:

Unit Testing in DotNET

The workflow file shown below runs tests with the DotNET Core CLI and processes the results with the test-reporter action:

name: .NET Core

on:
  push:
  workflow_dispatch:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout  
      uses: actions/checkout@v1

    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.402

    - name: Build
      run: dotnet build --configuration Release

    - name: Test
      run: dotnet test --logger "trx;LogFileName=test-results.trx" || true

    - name: Test Report
      uses: dorny/test-reporter@v1
      if: always()
      with:
        name: DotNET Tests
        path: "**/test-results.trx"                            
        reporter: dotnet-trx
        fail-on-error: true

The tests are executed by the DotNET Core CLI saving the results as a Visual Studio Test Results (TRX) report file.

The test command returns a non-zero exit code if any tests fail, but you defer responsibility for responding to failed tests to the test processor. By chaining || true to the command you ensure the step always passes:

    - name: Test
      run: dotnet test --logger "trx;LogFileName=test-results.trx" || true

The test-reporter action then processes the report file, and sets fail-on-error to true to fail the build if there are any failed tests:

    - name: Test Report
      uses: dorny/test-reporter@v1
      if: always()
      with:
        name: DotNET Tests
        path: "**/test-results.trx"                            
        reporter: dotnet-trx
        fail-on-error: true

Conclusion

GitHub Actions is primarily a task execution environment designed to verify and build code and publish the resulting artifacts. There are a number of third-party actions that allow you to generate test reports and respond to failed tests, but GitHub Actions has some gaps in terms of tracking test results over time. Still, the reporting functionality available today is useful, and will only improve.

In this post, you learned:

  • Some of the questions to ask when evaluating third-party actions to process test results
  • How to write basic workflows for testing Java and DotNET Core applications
  • How to process test results and display the generated reports

Happy deployments!

Apache Maven GitHub Java (programming language) unit test .NET

Published at DZone with permission of Matthew Casperson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Most Popular Technologies for Java Microservices Right Now
  • Using Python Libraries in Java
  • Using Lombok Library With JDK 23
  • Mastering Unit Testing and Test-Driven Development in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!