Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created August 1, 2018 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yetanotherchris/73f3b070586f7b48cae8b6f92c68df51 to your computer and use it in GitHub Desktop.
Save yetanotherchris/73f3b070586f7b48cae8b6f92c68df51 to your computer and use it in GitHub Desktop.
Converting NUnit to XUnit
  • 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

<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