DEV Community

Cinthia Barbosa da Silva
Cinthia Barbosa da Silva

Posted on

Automating unit tests with xUnit on GitHub Actions

Hi there!

In this article, I will share how you can run your unit tests using GitHub Actions!

Let's go!🚀

Before we start, make sure you have the following two basic steps:

  1. A GitHub repository with your project.
  2. Unit tests already implemented.

Now, let's implement CI on GitHub Actions. Here’s what you need to do:

  1. In your repository, create two folders with the names .github/workflows.
  2. Inside this folder workflows, create a file with the extension .yml, for example: run-tests.yml.

Next, it's time to define the steps to run your CI pipeline. Below is an example of how to set up your GitHub Actions workflow to run your unit tests.

name: CI - Unit Test

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Set up .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '8.x'

      - name: Restore dependencies
        run: dotnet restore ./src/sales-management-api.sln

      - name: Build the solution
        run: dotnet build ./src/sales-management-api.sln --configuration Release --no-restore

      - name: Run unit tests with report
        run: dotnet test ./src/Test/Test.csproj --configuration Release --no-build --logger "trx;LogFileName=TestResults.trx"

      - name: Upload test results
        uses: actions/upload-artifact@v4
        with:
          name: TestResults
          path: ./src/Test/TestResults/TestResults.trx

Enter fullscreen mode Exit fullscreen mode

Explaining the steps:

  • Check out repository code: This step clones your GitHub repository so that the actions can run on the source code.
  • Set up .NET: This configures the .NET SDK version you'll use to build and run tests, in my case, I am using version 8.
  • Restore dependencies: This restores all the necessary NuGet packages required by your project.
  • Build the solution: This compiles the code with the command dotnet build + path
  • Run unit tests: Runs the unit tests and generates a .trx report file with the command dotnet test + path
  • (Optional) Upload test results: This step uploads the test results as an artifact, allowing you to download and view the test output.

By following this setup, you’ll have continuous integration running your unit tests automatically when you push to the main branch or submit a pull request.

Now, every push will trigger the tests, and the results will be available in the Actions tab on GitHub.

Image description

We can see the result in “Run unit tests with report” how many tests passed or failed, as well as their duration.

Image description

My repository with this implementation: my-github

This is for today! If you have any questions, leave me a comment!

If you found this article helpful, please like the post.❤️

Top comments (0)