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:

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

No…


This content originally appeared on DEV Community and was authored by Cinthia Barbosa da Silva

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

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.❤️


This content originally appeared on DEV Community and was authored by Cinthia Barbosa da Silva


Print Share Comment Cite Upload Translate Updates
APA

Cinthia Barbosa da Silva | Sciencx (2024-09-20T23:56:19+00:00) Automating unit tests with xUnit on GitHub Actions. Retrieved from https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/

MLA
" » Automating unit tests with xUnit on GitHub Actions." Cinthia Barbosa da Silva | Sciencx - Friday September 20, 2024, https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/
HARVARD
Cinthia Barbosa da Silva | Sciencx Friday September 20, 2024 » Automating unit tests with xUnit on GitHub Actions., viewed ,<https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/>
VANCOUVER
Cinthia Barbosa da Silva | Sciencx - » Automating unit tests with xUnit on GitHub Actions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/
CHICAGO
" » Automating unit tests with xUnit on GitHub Actions." Cinthia Barbosa da Silva | Sciencx - Accessed . https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/
IEEE
" » Automating unit tests with xUnit on GitHub Actions." Cinthia Barbosa da Silva | Sciencx [Online]. Available: https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/. [Accessed: ]
rf:citation
» Automating unit tests with xUnit on GitHub Actions | Cinthia Barbosa da Silva | Sciencx | https://www.scien.cx/2024/09/20/automating-unit-tests-with-xunit-on-github-actions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.