-
Get VS Code
-
Remove using NUnit.Framework;
-
Add using XUnit;
-
Replace all [Test] with [Fact]
-
Replace [SetUp] with a constructor
-
Replace Assert.That(actualValue, Is.EqualTo(value)); with Assert.Equal(expected, actual)
- highlight Is.EqualTo(
- Delete it
- Select to the end of the line
- Ctrl + X
- Finish the line with );
- Go to the start of the line
- Replace Assert.That with Assert.Equal( and CTRL+V
- Remove the ));
-
Then use the LinqPad program below
-
-
Save martinandersen3d/898ad7a72fd4b74411de98cee6f40e8d to your computer and use it in GitHub Desktop.
Converting NUnit to XUnit
This file contains hidden or 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
<Query Kind="Program" /> | |
void Main() | |
{ | |
foreach (string file in Directory.EnumerateFiles(@"~\Documents\GitHub\roadkill\src\Roadkill.Tests\Unit\Text", "*.cs", SearchOption.AllDirectories)) | |
{ | |
string text = File.ReadAllText(file); | |
var translater = new NUnitTranslator(); | |
string classname = translater.GetClassName(text); | |
text = text.Replace("using NUnit.Framework;", "using Xunit;"); | |
text = translater.ReplaceSetupWithCtor(classname, text); | |
text = translater.RemoveTestFixture(text); | |
text = translater.RemoveCategory(text); | |
text = translater.ReplaceTestCaseAttributes(text); | |
text = translater.ReplaceTestAttributes(text); | |
text = translater.ReplaceFactWithTheory(text); | |
text = translater.ReplaceAssertThatWithEquals(text); | |
Console.WriteLine("Wrote {0}", file); | |
File.WriteAllText(file, text); | |
} | |
} | |
public class NUnitTranslator | |
{ | |
public string GetClassName(string text) | |
{ | |
var match = Regex.Match(text, "public class (.+)"); | |
return match.Groups[1].Value; | |
} | |
public string ReplaceSetupWithCtor(string classname, string text) | |
{ | |
text = Regex.Replace(text, @"\s\[SetUp\]\s", ""); | |
text = Regex.Replace(text, @"public void Setup\(\)", "public "+classname+@"()"); | |
return text; | |
} | |
public string RemoveTestFixture(string text) | |
{ | |
text = Regex.Replace(text, @"\s\[TestFixture\]\s", ""); | |
return text; | |
} | |
public string RemoveCategory(string text) | |
{ | |
text = Regex.Replace(text, @"\s\[Category\(""Unit""\)\]\s", ""); | |
return text; | |
} | |
public string ReplaceTestCaseAttributes(string text) | |
{ | |
return Regex.Replace(text, @"\[TestCase\(", "[InlineData("); | |
} | |
public string ReplaceTestAttributes(string text) | |
{ | |
return Regex.Replace(text, @"\[Test\]", "[Fact]"); | |
} | |
public string ReplaceFactWithTheory(string text) | |
{ | |
return Regex.Replace(text, @"\[Fact\]\s+\[InlineData", "[Theory]\n\t[InlineData"); | |
} | |
public string ReplaceAssertThatWithEquals(string text) | |
{ | |
var stringBuilder = new StringBuilder(); | |
foreach (string line in text.Split('\n')) | |
{ | |
if (line.Contains("Is.EqualTo(")) | |
{ | |
var match = Regex.Match(line, @"Is\.EqualTo\((.+)\)\);"); | |
string expectedPart = match.Groups[1].Value; | |
int end = line.IndexOf(","); | |
string newline = line.Substring(0, end); | |
newline = newline.Replace("Assert.That(", $"Assert.Equal({expectedPart}, "); | |
newline += ");"; | |
newline = newline.TrimEnd('\n','\r'); | |
stringBuilder.AppendLine(newline); | |
} | |
else | |
{ | |
string newline = line.TrimEnd('\n', '\r'); | |
stringBuilder.AppendLine(newline); | |
} | |
} | |
return stringBuilder.ToString(); | |
} | |
} | |
// Define other methods and classes here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment