Automating Unit Tests in .NET 8 with GitHub Actions

Automating Unit Tests in .NET 8 with GitHub Actions

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.


Why Automate Unit Tests?

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.


Workflow Overview

Here’s what our automated workflow will do:

  1. Checkout the repository

  2. Setup the .NET 8 SDK

  3. Restore project dependencies

  4. Build the solution

  5. Run unit tests using xUnit

  6. 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.

Why This Matters (DevOps Connection)

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.


Tips for Best Results

  • 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


Conclusion

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

 


Writer: Ravinder Singh

Full Stack Developer

I have 15+ years of experience in commercial software development. I write this blog as a kind of knowledge base for myself. When I read about something interesting or learn anything I will write about it. I think when writing about a topic you concentrate more and therefore have better study results. The second reason why I write this blog is, that I love teaching and I hope that people find their way on here and can benefit from my content.

Ready to Transform Your Digital Presence?

Let's discuss how these trends can benefit your business

Free Consultation