Continuous Integration (CI) is essential for modern software development, but CI is only as strong as the tests that validate your code. In this tutorial, we’ll explore how to automate unit tests for a .NET 8 project using GitHub Actions, capture test logs, and save them as artifacts for easy access.
Automated tests in CI/CD pipelines help you:
Catch bugs before they reach production
Ensure new code doesn’t break existing functionality
Provide a clear, reproducible record of test results
Allow teams to analyze logs after every run
By combining GitHub Actions and xUnit tests, you can build a robust CI/CD pipeline that not only compiles your code but also validates it automatically.
Here’s what our automated workflow will do:
Checkout the repository
Setup the .NET 8 SDK
Restore project dependencies
Build the solution
Run unit tests using xUnit
Save test results as artifacts
Note: I have already posted a blog about CI steps 1-4, please checkContinuous Integration (CI) for ASP.NET Core 8 API with GitHub Actions
so we will start from Step 5
Step 5: Run Unit Tests- name: Run unit tests run: dotnet test $TEST_PROJECT_PATH \ --no-build \ --configuration Release \ --logger "trx;LogFileName=test_results.trx" \ --verbosity detailed
Here’s what happens:
Your small, automated tests run on the code
A log file is created showing what passed and what failed
Detailed output makes it easier to understand any failures
Step 6: Save Test Logs
- name: Upload Test Results uses: actions/upload-artifact@v4 with: name: Test Results path: '**/test_results.trx'
This keeps a copy of your test results on GitHub.
You can download it anytime to see what tests ran and whether they passed.
This is part of DevOps, which is all about automating software processes to:
Reduce errors
Speed up delivery
Keep the team aligned
Even though this workflow doesn’t deploy your app yet, automated tests are a core part of DevOps, because they make sure your software stays reliable while changes are being made.
Use detailed logs so you can see exactly what happened
Only use diagnostic logs when something really fails and you need more info
Keep your tests and logs organized for easy access
If you have multiple test projects, use a pattern like **/test_results.trx to capture everything
By automating your unit tests:
You save time
Reduce human mistakes
Ensure your code keeps working correctly
And by saving logs as artifacts in GitHub, you always have a record to look at, which helps both developers and managers understand the health of the project.
Sample working code at GitHub
Thanks, for reading the blog, I hope it helps you. Please share this link on your social media accounts so that others can read our valuable content. Share your queries with our expert team and get Free Expert Advice for Your Business today.
Hire me on Linkedin
My portfolio