This is a series on the .Net Core 1.0 bits. Looking for .Net Core 2 Series?
- Getting Started
- What’s in the box
- Using Multiple Projects
- Testing <=(We are here)
- NuGet
- Multi-targeting
- Publishing Portable Applications
- Self-contained Applications
We want to be able to test our application code that we build. Let’s get started.
The Setup
Like before, we will rattle off a new project.
mkdir testdemo
cd testdemo
mkdir mylib
cd mylib
dotnet new -t Lib
Recall that we can use the -t
argument switch to generate different project types.
And we will also replace Library.cs:
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 restore and build.
dotnet restore
dotnet build
This puts us in a similar situation to the last post.
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. However, we will have to make some changes to work around some nuances in the Release Preview.
cd ../
mkdir testit
cd testit
dotnet new -t xunittest
Now if we look on the Getting started with xUnit.net (.NET Core / ASP.NET Core) page we will see that we have to change the project.json
file.
Note: Specific versions of xUnit.net only support specific versions of .NET CLI.
This will change again after the RTM I’m sure, but for now, for version 1.0.0-preview2-003121
we need to change the project.json
dependencies.
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.1.1",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-192208-24"
}
This needs to be changed to:
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.1.1",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029"
}
Now seems like a good time to test that this test project works.
Test the project!
If we open up the Tests.cs
file we see:
using System;
using Xunit;
namespace Tests
{
public class Tests
{
[Fact]
public void Test1()
{
Assert.True(true);
}
}
}
Pretty straight forward. So if everything is set up correctly we should be able to just build and run.
dotnet restore
dotnet build
dotnet test
Project testit (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
xUnit.net .NET CLI test runner (64-bit .NET Core win10-x64)
Discovering: testit
Discovered: testit
Starting: testit
Finished: testit
=== TEST EXECUTION SUMMARY ===
testit Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.162s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
A successful test run. Though not doing much for us yet.
A note for next time, calling dotnet test
also causes a build, if it is required, so next time we can skip the call to dotnet build
.
Reference the project
Opening up the project.json
we can add a reference to the mylib
project from the beginning.
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.1.1",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"mylib": {
"target": "project"
}
},
I’ve decided to add it at the top level with the xunit
dependencies since we only have a single framework. It could have also gone into the dependencies inside the netcoreapp1.0
framework as well.
To make sure this has worked, we run restore again.
dotnet restore
log : Restoring packages for C:\dev\temp\testdemo\testit\project.json...
log : Writing lock file to disk. Path: C:\dev\temp\testdemo\testit\project.lock.json
log : C:\dev\temp\testdemo\testit\project.json
log : Restore completed in 1520ms.
To make sure we are in good order:
dotnet build
dotnet test
Remember from last time if you see:
Project mylib does not have a lock file. Please run "dotnet restore" to generate a new lock file.
This means you need to go back and run dotnet restore
on the mylib
project.
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 Test.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
Project mylib (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project testit (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling testit for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.3247869
xUnit.net .NET CLI test runner (64-bit .NET Core win10-x64)
Discovering: testit
Discovered: testit
Starting: testit
Finished: testit
=== TEST EXECUTION SUMMARY ===
testit Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.189s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
We 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.