Skip to content

Instantly share code, notes, and snippets.

@mhinze
Created January 11, 2012 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhinze/1596038 to your computer and use it in GitHub Desktop.
Save mhinze/1596038 to your computer and use it in GitHub Desktop.
Sample solution script for running MStest tests
function outputResults($resultsFile, $verbose)
{
$list = @()
$xml = [xml](get-content $resultsFile)
$failures = $xml.TestRun.Results.UnitTestResult | where {$_.outcome -eq "Failed" } | select testId
if ($failures.Count -gt 0)
{
foreach ($test in $failures)
{
$name = ""
$failure = $xml.TestRun.TestDefinitions.UnitTest | where { $_.id -eq $test.testId }
if ($verbose)
{
$name = "." + $failure.TestMethod.name
}
$list += ($failure.TestMethod.className.split(',')[0] + $name)
}
if ($list.Count -gt 0)
{
$desc = "Classes"
if ($verbose)
{
$desc = "Methods"
}
write-host "MSTest: Test $desc With Failures" -foregroundcolor red
$list | sort-object | get-unique | write-host
}
}
write-host "MSTest Run " -nonewline
$summary = $xml.TestRun.ResultSummary.outcome
$color = "green"
if ($summary -eq "Failed")
{
$color = "red"
}
write-host $summary -foregroundcolor $color
$total = $xml.TestRun.ResultSummary.Counters.total
$passed = $xml.TestRun.ResultSummary.Counters.passed
$failed = $xml.TestRun.ResultSummary.Counters.failed
write-host "$total Total, $passed Pass, $failed Fail"
}
function getRandomDirectory()
{
$randomDirName = [System.IO.Path]::GetRandomFileName()
$tempPath = [System.IO.Path]::GetTempPath()
$randomPath = Join-Path $tempPath $randomDirName
if (!(test-path $randomPath))
{
return $randomPath
}
return getRandomDirectory
}
function runtests([switch]$v)
{
$verbose = $v
Write-host "Running tests... verbose is $verbose"
#create temp dir
$temp = getRandomDirectory
md $temp | out-null
#store the pwd so we can return to it
$origwd = $pwd
sl $temp
$mstest = Join-Path $env:VS100COMNTOOLS "..\IDE\mstest.exe" -Resolve
$testDll = Join-Path $solutionScriptsContainer "..\UnitTests\bin\Debug\UnitTests.dll" -Resolve
& $mstest /testcontainer:$testDll /resultsfile:results.trx | out-null
$resultsFile = Join-Path $temp "results.trx"
outputResults $resultsFile $verbose
sl $origwd
ri $temp -force -recurse | out-null
}
Export-ModuleMember -Function runtests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment