GitHub Action - Build and Run .NET Tests on new pull requests

Validate your dotnet code by building and running tests on every pull request.

Luke Lowrey
Luke Lowrey
- 1 min read
GitHub Action - Build and Run .NET Tests on new pull requests

Pull request validation saves time and encourages better coding practices. You can easily set up a GitHub action to validate new code by building and running tests when a new PR is created.

Create a GitHub Action to run on new pull requests

Add an action to your repository using the "Actions" tab -> New workflow on or by creating a build-and-test.yml file in the .github/workflows/ directory.

Example dotnet-core.yml from FluentEmail

name: Build & Test

    branches: [ master ]

    runs-on: ubuntu-latest

    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
        dotnet-version: 3.1.101
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal

  • The action uses the on: pull_request trigger which accepts the branches you want to run the workflow
  • actions/checkout@v2 and actions/setup-dotnet@v1 are community actions that will get your code and setup dotnet
  • The run steps make the calls the the dotnet cli to restore, build and test
  • Be sure to run on ubuntu rather than Windows for better stability and more minutes

View status of validation on pull requests

When the action is in place you will see the validation status of all pull requests on the pull request tab. You will also see the checks inside the PR details and can prevent contributors merging pull requests with failing checks.

Pull request tab status

Check out these other GitHub Actions 👇