Web development modern style (.NET Core, C#) with unit test and continuous integration

We are continuing Web development, but this time we will use completely different approach and will use new offering from Microsoft – .NET Core
We already met .NET Core, so please take a look at it for instructions how to setup environment.
Open Terminal (or Command Prompt) and let’s create directory structure:
dotnetweb
– Main
– UnitTest

cd
mkdir dotnetweb
cd dotnetweb
mkdir Main
mkdir UnitTest

Create empty solution:

dotnet new sln

Create web application project and add it to the solution:

cd Main
dotnet new web
cd ~/dotnetweb/
dotnet sln add ./Main/Main.csproj

Create unit test project, link it to web project and add it to the solution:

cd UnitTest
dotnet new xunit
dotnet add reference ../Main/Main.csproj
dotnet add package coverlet.msbuild
cd ~/dotnetweb/
dotnet sln add ./UnitTest/UnitTest.csproj

Let’s build it:

dotnet restore
dotnet build

You should see 0 Warning(s), 0 Error(s).

Let’s test it:

cd Main
dotnet run

You should see message that it’s listening on: http://localhost:5000
so open a browser and go to http://localhost:5000
you should see familiar “Hello World!”

Now let’s add some code and add a unit test.
Let’s add a function which returns true if an integer is even:

cd ~/dotnetweb/Main
vi Startup.cs

add the following code to the end of class Startup

public bool IsEven(int candidate)
{
    if (candidate % 2 == 0)
    {
       return true;
    }
    return false;
}

Save and close Vim (use ZZ to be efficient).
Let’s add unit test for this function:

cd ~/dotnetweb/UnitTest
vi UnitTest1.cs
using System;
using Xunit;
using Main;

namespace UnitTest
{
    public class UnitTest1
    {
        private readonly Startup _Startup;

        public UnitTest1()
        {
            _Startup = new Startup();
        }

        [Theory]
        [InlineData(2)]
        [InlineData(4)]
        [InlineData(6)]
        [InlineData(8)]
        public void ReturnTrueGivenEvenLessThan10(int value)
        {
            var result = _Startup.IsEven(value);

            Assert.True(result, $"{value} should be even");
        }

        [Theory]
        [InlineData(1)]
        [InlineData(3)]
        [InlineData(5)]
        [InlineData(9)]
        public void ReturnFalseGivenOddLessThan10(int value)
        {
            var result = _Startup.IsEven(value);

            Assert.False(result, $"{value} should not be even");
        }

        [Fact]
        public void Test1()
        {
	    Assert.True(1==1, $"1=1");
        }
    }
}

Let’s run the test:

dotnet test

We should see Total tests: 9. Passed: 9. Failed: 0. Skipped: 0.
Test Run Successful.

Let’s run in again:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

This time you will see same test results but at the end you will get code coverage analysis. This is a result of a package coverlet.msbuild which we added.

To summarize what we did: we created and tested web application written on C# and .Net Core, which include a unit test.

Next step is to add Continuous integration
But first we have to upload our code to GitHub.

cd ~/dotnetweb/
curl https://raw.githubusercontent.com/github/gitignore/master/VisualStudio.gitignore > .gitignore
git init
git add *
git commit -m "initial commit"

Then create empty repository on GitHub and follow the instructions to push the project to this repository.

To implement Continuous integration we are going to use free service provided by Travis CI.
1. go back to your project and add new file .travis.yml:

cd ~/dotnetweb/
vim .travis.yml
language: csharp

dotnet: 2.1.2
sudo: false

env:
    global:
        - DOTNET_CLI_TELEMETRY_OPTOUT: 1

script:
    - dotnet build dotnetweb.sln -c Release
    - dotnet test -c Release --no-build UnitTest/UnitTest.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

2. add it to your repository:

git add .travis.yml
git commit -m "Travis support"
git push origin master

3. go to Travis CI web site and login with your GitHub account
4. allow Travis CI access your GitHub repositories
5. click on + on the right of “My Repositories” (Add New Repository)
6. find dotnetweb (if you don’t see it click Sync Account) in the list and slide left to enable it
7. click Settings and select “Build only if .travis.yml is present”
9. switch back to the ‘Current’ tab, nothing there so far
9. Travis action is triggered by code commit, so let’s do this:

echo "Hello DotNet" > README
git add README
git commit -m "README"
git push -u origin master

10. go back to the browser and in a few seconds you should see activity on the page and in couple of minutes your project should be built, all tests should be run and you should see all green with ‘#2 passed’.

Please note that email notifications won’t work if you follow typical GitHub setup with anonymous emails.
If you want emails, follow Travis docs and setup alternative email address in travis config file.

Sample repository you can find here.

(As an Amazon Associate I earn from qualifying purchases)

Leave a Reply