Skip to content

Instantly share code, notes, and snippets.

@GeorgDangl
Last active October 2, 2017 10:57
Show Gist options
  • Save GeorgDangl/0f3280bfa22b3f5549234f7d57d070c7 to your computer and use it in GitHub Desktop.
Save GeorgDangl/0f3280bfa22b3f5549234f7d57d070c7 to your computer and use it in GitHub Desktop.
PowerShell scripts to run unit tests with xUnit and Dotnet CLI and to include target framework in test name for Jenkins CI visualization
Param(
[string]$globPattern = "results_*.testresults",
[string]$searchDirectory = $PSScriptRoot
)
# Find all files matching the glob pattern
$testResultFiles = Get-ChildItem -Path $searchDirectory -Filter $globPattern -Recurse
function getFrameworkNameFromFilename {
Param($file)
$frameworkName = $testResultFile.Name.Substring(0, $testResultFile.Name.Length - $testResultFile.Extension.Length)
$frameworkName = $frameworkName.Substring($frameworkName.LastIndexOf("-") + 1)
return $frameworkName;
}
function transformTestResultFile {
Param($testResultFile)
$frameworkName = getFrameworkNameFromFilename $testResultFile
$xmlContent = [xml](Get-Content $testResultFile)
$testNodes = $xmlContent.SelectNodes("//test/@type")
foreach ($testNode in $testNodes){
$testNode.Value = $frameworkName + "+" + $testNode.Value
}
$xmlContent.Save($testResultFile.FullName)
}
foreach ($testResultFile in $testResultFiles) {
transformTestResultFile $testResultFile
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;netcoreapp1.1;netcoreapp1.0;net47;net461;net46</TargetFrameworks>
<AssemblyName>Dangl.Common.Tests</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Dangl.Common\Dangl.Common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170923-02" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-rc1-build3808" />
<PackageReference Include="xunit" Version="2.3.0-rc1-build3808" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-rc1-build3808" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
$testProjects = "Dangl.Common.Tests"
$testRuns = 1;
& dotnet restore
$oldResults = Get-ChildItem -Path "$PSScriptRoot\results_$testRuns-*.testresults"
if ($oldResults) {
Remove-Item $oldResults
}
foreach ($testProject in $testProjects){
& cd "$PSScriptRoot\test\$testProject"
& dotnet.exe xunit `
-parallel all `
-xml $PSScriptRoot\results_$testRuns.testresults
$testRuns++
}
& cd "$PSScriptRoot"
"Prepending framework to test method name for better CI visualization"
$resultsGlobPattern = "results_*.testresults"
$prependFrameworkScript = ".\AppendxUnitFramework.ps1"
& $prependFrameworkScript $resultsGlobPattern "$PSScriptRoot"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment