Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Last active November 19, 2016 00:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Code snippets for the blog article at 2016/10/20/unit-tests-in-net-core-an-introduction
dotnet new -t xunittest
dotnet test
Project library (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project test-library (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
xUnit.net .NET CLI test runner (64-bit osx.10.12-x64)
Discovering: test-library
Discovered: test-library
Starting: test-library
Finished: test-library
=== TEST EXECUTION SUMMARY ===
test-library Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.288s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.program.cs
using static System.Console;
using Library;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
// Basic use of the Thing class Get method
var thing = new Thing();
WriteLine($"The answer is {thing.Get(19, 23)}");
}
}
}
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
},
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.1.1",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-192208-24"
},
"testRunner": "xunit",
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
}
}
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.1.1",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-192208-24",
"library": {
"target": "project"
}
}
using Library;
using Xunit;
namespace TestApp
{
public class LibraryTests
{
// A stupid test class which assets that 19 + 23
// is 42, via the Thing class' Get method
// Note: The Fact attribute tells the xunit
// that TestThing is a single test.
[Fact]
public void TestThing()
{
Assert.Equal(42, new Thing().Get(19, 23));
}
}
}
using static Newtonsoft.Json.JsonConvert;
namespace Library
{
public class Thing
{
// Takes two ints, adds them, stores them in
// a string (using string interpolation),
// then deserialises (via Json.Convert) back
// to an int and returns that value
public int Get(int left, int right) =>
DeserializeObject<int>($"{left + right}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment