This is just a quick script that can help convert a .Tests.csproj which was originally written for MS Test over to using xUnit. It probably doesn’t cover every conversion aspect, but it can get you moving in the right direction.
What it will convert:
- Replace using Microsoft.VisualStudio.TestTools.UnitTesting; with using Xunit;
- Remove [TestClass]
- Replace [TestMethod] with [Fact]
- Replace Assert.AreEqual with Assert.Equal
- Replace Assert.IsTrue with Assert.True
- Replace Assert.IsFalse with Assert.False
- Replace Assert.IsNull with Assert.Null
- Replace Assert.IsNotNull with Assert.NotNull
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Convert-VisualStudioTestsToXunit { | |
param( | |
[string] $Path | |
) | |
# input validation | |
Test-ProjFilePath -Path $Path | |
# conversion | |
#. $codeFormatter $Path | |
$parent = Split-Path -Path $Path | |
$csfiles = dir -Path $parent -Recurse -Include *.cs | |
function replaceTestClass($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "\[TestClass\]") { | |
$touched.Value = $true | |
return $null | |
} | |
return $Line | |
} | |
function replaceTestMethod($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)\[TestMethod\](.*)") { | |
$touched.Value = $true | |
$l = "{0}[Fact]{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
function replaceAreEqual($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)Assert\.AreEqual(.*)") { | |
$touched.Value = $true | |
$l = "{0}Assert.Equal{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
function replaceIsTrue($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)Assert\.IsTrue(.*)") { | |
$touched.Value = $true | |
$l = "{0}Assert.True{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
function replaceIsFalse($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)Assert\.IsFalse(.*)") { | |
$touched.Value = $true | |
$l = "{0}Assert.False{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
function replaceIsNull($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)Assert\.IsNull(.*)") { | |
$touched.Value = $true | |
$l = "{0}Assert.Null{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
function replaceIsNotNull($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)Assert\.IsNotNull(.*)") { | |
$touched.Value = $true | |
$l = "{0}Assert.NotNull{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
function replaceUsing($Line, [ref] $touched) { | |
if($Line -eq $null) { return $null } | |
if($Line -match "(.*)using Microsoft\.VisualStudio\.TestTools\.UnitTesting;(.*)") { | |
$touched.Value = $true | |
$l = "{0}using Xunit;{1}" -f $Matches[1], $Matches[2] | |
return $l | |
} | |
return $Line | |
} | |
foreach($f in $csfiles) { | |
$content = Get-Content -Path $f.FullName | |
$touched = $false | |
$converted = @() | |
foreach($line in $content) { | |
$updated = $line | |
$updated = replaceTestClass -Line $updated -Touched ([ref] $touched) | |
$updated = replaceTestMethod -Line $updated -Touched ([ref] $touched) | |
$updated = replaceAreEqual -Line $updated -Touched ([ref] $touched) | |
$updated = replaceIsTrue -Line $updated -Touched ([ref] $touched) | |
$updated = replaceIsFalse -Line $updated -Touched ([ref] $touched) | |
$updated = replaceIsNull -Line $updated -Touched ([ref] $touched) | |
$updated = replaceUsing -Line $updated -Touched ([ref] $touched) | |
if($null -ne $updated) { | |
$converted += @($updated) | |
} | |
} | |
#Write-Host ($f.FullName + ": ") -NoNewline | |
if($touched) { | |
#Write-Host "Updated" -ForegroundColor Green | |
$converted | Set-Content -Path $f.FullName -Force | |
} else { | |
#Write-Host "Untouched" -ForegroundColor Gray | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-ProjFilePath { | |
param( | |
[string] $Path | |
) | |
# input validation | |
if((Test-Path -Path $Path) -eq $false) { | |
throw "Could not find a file at $Path. Please ensure the path was entered correctly." | |
} | |
$fi = Get-Item -Path $Path | |
$ext = $fi.Extension | |
if($null -eq $ext) { | |
throw "No file extension was found for the file at $Path. This can only convert .csproj and .vbproj files at the moment." | |
} | |
$ext = $ext.ToLower() | |
if(@(".csproj", ".vbproj") -notcontains $ext) { | |
throw "Could not find a .csproj/.vbproj file at $Path. This can only convert .csproj and .vbproj files at the moment." | |
} | |
} |
0 comments:
Post a Comment