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:
- A GitHub repository with your project.
- Unit tests already implemented.
Now, let's implement CI on GitHub Actions. Here’s what you need to do:
- In your repository, create two folders with the names
.github/workflows
. - 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.
We can see the result in “Run unit tests with report” how many tests passed or failed, as well as their duration.
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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.