Skip to content

Instantly share code, notes, and snippets.

@GaProgMan GaProgMan/bash.sh Secret
Last active Nov 19, 2016

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
You can’t perform that action at this time.