Profile picture of Liam Moat

Liam Moat

Principal Engineer at Microsoft

Bitbucket Pipelines and Unit Testing .NET Core

By Liam Moat. . 2 minutes read.

Atlassian recently announced continuous delivery inside Bitbucket with Pipelines - build, test and deploy from Bitbucket. Shortly after Microsoft announced the release of .NET Core 1.0. Pipelines can build Node, Ruby, Python, PHP and anything else you can run in a Docker image - including .NET Core. Take a look how to use Pipelines to not only build a .NET Core project, but test it as well!

The Solution

The solution comprises of two projects MathDemo and MathDemo.Tests. I have adopted Microsoft’s recommended directory structure with two parent directories - src and test.

You can take a look at the working example here on Bitbucket.

      |__Source Files
      |__Test Files

Take a look at Microsoft’s documentation, to understand Unit Testing in .NET Core.

For this example, I have created a simple, and straightforward portable class library in .NET Core and a corresponding test class using Xunit. You can take a look at the code here:

Pipelines Configuration

To run a build, you need a valid bitbucket-pipelines.yml file with a branch-specific or default pipeline configuration in the root of your repository.

I used the following…

image: microsoft/dotnet:onbuild

    - step:
          - dotnet restore
          - dotnet test test/MathDemo.Tests

The first line tells Pipelines which image to use as the runtime environment - in this case Microsoft’s official .NET Core image. This can, of course, be any image from Docker’s registry or your own.

Rather than a branch-specific configuration, I used default. Using the dotnet CLI the script runs dotnet restore to restore the dependencies and tools and dotnet test to build the necessary projects and execute the unit tests.

And that’s it! Now, every push to Bitbucket triggers a build. You can see an example here.

Pipelines Builds

You also get a really nice summary of each build.

Pipelines Build Summary

And a detailed output from the build log.

MathDemo.Tests  Total: 4, Errors: 0, Failed: 0, Skipped: 0, Time: 0.181s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

I can’t wait to see what Atlassian do with Pipelines next. For example, I’m hoping for the ability to publish build artifacts or indeed a Docker image ready for deployment.