While writing a fairly simple RESTful service with ASP.NET 5, I started to leverage the ease of self-hosting with the .NET Execution Environment (DNX) by executing service-level tests as part of the automated build. I use Powershell for the build script. I first publish the service to a temporary directory, start it in a background thread, and perform the actual tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function Invoke-Build { dnu publish .\src\Service\project.json --out .\tmp\service --no-source --configuration Release --quiet } function Run-ServiceTests { # start the service in the background $cwd = Get-Location Start-Job -Name MyServiceName -ScriptBlock ({param($path) Set-Location -Path $path .\tmp\service\web.cmd }) -ArgumentList $cwd # wait until the service is started; instead of waiting for a random time, ping the service until it starts responding Start-Sleep -Seconds 5 # run the service tests dnx -p .\test\ServiceTests\project.json test # stop the job and also write the output Get-Job -Name LabelPrintingService | Stop-Job Get-Job -Name LabelPrintingService | Receive-Job } Invoke-Build Run-ServiceTests |
With these few lines of code I am able to validate the service’s health after every commit, and notably running these set of tests as part of a pull-request build before being merged to the main line.
One example where I used such a build script is the xUnit2NUnit web service. The source code can be found on github, and a description in a separate blog post.