Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#tool "nuget:?package=Microsoft.TestPlatform&version=15.7.0"
#tool "nuget:?package=NUnitTestAdapter&version=2.1.1"
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var buildDir = Directory("./src/Example/bin") + Directory(configuration);
var slnFile = "./src/Example.sln";
var testDllPattern = "./src/**/*Tests.dll";
Task("Clean").Does(() =>
Task("Restore-NuGet-Packages").IsDependentOn("Clean").Does(() =>
Task("Build").IsDependentOn("Restore-NuGet-Packages").Does(() =>
MSBuild(slnFile, settings => settings.SetConfiguration(configuration));
public string getNUnit2AdapterPath(){
var nugetPaths = GetDirectories("./tools/NUnitTestAdapter*/tools");
if(nugetPaths.Count == 0){
throw new Exception("Cannot locate the NUnit2 test adapter. You might need to add '#tool \"nuget:?package=NUnitTestAdapter&version=2.1.1\"' to the top of your build.cake file.");
return nugetPaths.Last().ToString();
Task("Run-Tests").IsDependentOn("Build").Does(() =>
var testSettings = new VSTestSettings{
ToolPath = Context.Tools.Resolve("vstest.console.exe"),
TestAdapterPath = getNUnit2AdapterPath(),
// use the Trx Logger and a deterministic output file name
// to be able to import test results into a build orchestration tool (VSTS, Teamcity etc.).
ArgumentCustomization = arg => arg.Append("/logger:trx;LogFileName=VsTestResults.xml")
VSTest(testDllPattern, testSettings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment