This is a series on the latest 2.* .Net Core bits, Following on from the original .Net Core Series

(At the time of writing, 2.1.4. I use windows, you don't have to!)

The Setup

Like before, we will rattle off a new project.

mkdir MyTestApplicationRepo
cd MyTestApplicationRepo
mkdir MyLib
cd MyLib
dotnet new classlib
cd ../
dotnet new sln
dotnet sln add ./MyLib/MyLib.csproj

(Refer back to Using Multiple Projects where we learned these commands)

And we will also replace .\MyLib\Class1.cs content:

using System;

namespace MyLib  
{
    public class Calculator
    {
        public int Add(int first, int second)
        {
            return first + second;
        }
    }
}

And to be sure that everything is working, we can do a build.

dotnet build

This puts us in a similar situation to the last post (minus the application).

Add a test project

On with the testing. Getting the project set up is not much different to the way we linked a library and app last time.

mkdir MyLib.Tests
cd MyLib.Tests
dotnet new xunit

For good measure, we will add this to our solution as well.

cd ../
dotnet sln add MyLib.Tests/MyLib.Tests.csproj

Test the project!

If we open up the .\MyLib.Tests\UnitTest1.cs file we see:

using System;
using Xunit;

namespace MyLib.Tests
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {

        }
    }
}

Pretty straightforward. So if everything is set up correctly we should be able to just run the test.

dotnet test
Build started, please wait...
Build started, please wait...
Build completed.

Test run for C:\dev\MyTestApplicationRepo\MyLib\bin\Debug\netstandard2.0\MyLib.dll(.NETStandard,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
No test is available in C:\dev\MyTestApplicationRepo\MyLib\bin\Debug\netstandard2.0\MyLib.dll. Make sure test project has a NuGet reference for the package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate and try again.

Test Run Aborted.
Build completed.

Test run for C:\dev\MyTestApplicationRepo\MyLib.Tests\bin\Debug\netcoreapp2.0\MyLib.Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.3854024]   Discovering: MyLib.Tests
[xUnit.net 00:00:00.4414056]   Discovered:  MyLib.Tests
[xUnit.net 00:00:00.4471599]   Starting:    MyLib.Tests
[xUnit.net 00:00:00.5721680]   Finished:    MyLib.Tests

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.3026 Seconds

A successful test run. Though there were some errors at the start. Something to keep in mind when you run tests from the solution, it will try and run tests from every project in the solution. This may not be a problem for you, but if it annoys you, you can just test the one project using:

dotnet test .\MyLib.Tests\MyLib.Tests.csproj

You can also just run dotnet test from inside the project folder instead.

As usual, calling dotnet test can also cause a restore and/or a build, if it is required.

Reference the project

We need to reference the library from the test project. Just like last time, we can use the add reference CLI functionality.

cd ./MyLib.Tests
dotnet add reference ..\MyLib\MyLib.csproj
Reference `..\MyLib\MyLib.csproj` added to the project.

To make sure we are in good order:

dotnet test
Build started, please wait...
Build completed.

Test run for C:\dev\MyTestApplicationRepo\MyLib.Tests\bin\Debug\netcoreapp2.0\MyLib.Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:01.5799270]   Discovering: MyLib.Tests
[xUnit.net 00:00:01.6447464]   Discovered:  MyLib.Tests
[xUnit.net 00:00:01.6504343]   Starting:    MyLib.Tests
[xUnit.net 00:00:01.8191177]   Finished:    MyLib.Tests

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 2.6617 Seconds

Test the library

It's one thing to reference the project, it is another to actually use its code.

First, we will add a using to our UnitTest1.cs file:

using MyLib;

And add another test:

[Fact]
public void OnePlusOneIsTwo()
{
    var calculator = new Calculator();
    var result = calculator.Add(1, 1);
    Assert.Equal(2, result);
}

Since we can build and test with one command, run:

dotnet test
Build started, please wait...
Build completed.

Test run for C:\dev\MyTestApplicationRepo\MyLib.Tests\bin\Debug\netcoreapp2.0\MyLib.Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.4541867]   Discovering: MyLib.Tests
[xUnit.net 00:00:00.5220094]   Discovered:  MyLib.Tests
[xUnit.net 00:00:00.5290939]   Starting:    MyLib.Tests
[xUnit.net 00:00:00.6742797]   Finished:    MyLib.Tests

Total tests: 2. Passed: 2. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.4901 Seconds

We now have a successful test passing in the xUnit.net CLI test runner using code from our own .Net Core library.

Stay Tuned...

Before we can think about publishing our awesome library we need a way to pack it up. Tune in next week.