Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Powershell Build and Run MSTests
Clear
$testsFile = .\ArgsTests\bin\Debug\ArgsTests.dll
$resultsFile = 'testresults.txt'
function BuildSolution()
{
Write-Host Building solution...
& c:\windows\microsoft.net\framework\v4.0.30319\msbuild.exe
}
function RunTests()
{
$mstest = 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe'
if(test-path $resultsFile) { DEL $resultsFile }
& $mstest /testcontainer:$testsFiles /detail:errormessage /resultsfile:$resultsFile
}
function ParseTestResults()
{
$results = [xml](GC $resultsFile)
$outcome = $results.TestRun.ResultSummary.outcome
$fgColor = if($outcome -eq "Failed") { "Red" } else { "Green" }
$total = $results.TestRun.ResultSummary.Counters.total
$passed = $results.TestRun.ResultSummary.Counters.passed
$failed = $results.TestRun.ResultSummary.Counters.failed
$failedTests = $results.TestRun.Results.UnitTestResult | Where-Object { $_.outcome -eq "Failed" }
Write-Host Test Results: $outcome -ForegroundColor $fgColor -BackgroundColor "Black"
Write-Host Total tests: $total
Write-Host Passed: $passed
Write-Host Failed: $failed
Write-Host
$failedTests | % { Write-Host Failed test: $_.testName
Write-Host $_.Output.ErrorInfo.Message
Write-Host $_.Output.ErrorInfo.StackTrace }
Write-Host
}
function ProcessFileChange()
{
Clear
BuildSolution
RunTests
ParseTestResults
}
function SetupFileWatcher()
{
# Not using this right now because of the double fire of file change from visual studio.
Write-Host Setting up file watcher
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = get-location
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $false
$watcher.Filter = "*.cs"
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
$changed = Register-ObjectEvent $watcher "Changed" -Action {
$timer = new-object system.timers.timer
$timer.interval = 500
$timer.AutoReset = $false
$timerElapsed = Register-ObjectEvent $timer "Elapsed" -Action {
Write-Host timer fire
Unregister-Event $timerElapsed.id
}
Write-Host "File change detected"
Write-Host $Event.SourceEventArgs.Name
Write-Host $Event.SourceEventArgs.ChangeType
Write-Host
$timer.Enabled = $true
}
Write-Host "Watching solution for file changes"
do {
Start-Sleep -milliseconds 100
} until ($Host.UI.RawUI.KeyAvailable)
Unregister-Event $changed.Id
}
ProcessFileChange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment