Skip to content

Instantly share code, notes, and snippets.

@ekepes
Forked from yevhen/SpecificationFixture.cs
Last active December 30, 2015 03:39
Show Gist options
  • Save ekepes/7770323 to your computer and use it in GitHub Desktop.
Save ekepes/7770323 to your computer and use it in GitHub Desktop.
Pull in all of the specifications in the test assembly.
[TestFixture]
public class SpecificationFixture
{
[Test, TestCaseSource("GetSpecificationTestCases")]
public void Verify(SpecificationToRun spec)
{
var runner = new SpecificationRunner();
RunResult result = runner.RunSpecifciation(spec);
if (result.Passed)
return;
Assert.Fail(Format(result));
}
public TestCaseData[] GetSpecificationTestCases()
{
RootGenerator generator = new RootGenerator(Assembly.GetExecutingAssembly());
IEnumerable<SpecificationToRun> specs = generator.GetSpecifications();
return specs.Select(AsTestCaseData).ToArray();
}
static TestCaseData AsTestCaseData(SpecificationToRun spec)
{
var data = new TestCaseData(spec);
data.SetName(spec.Specification.GetName());
return data;
}
private static string Format(RunResult result)
{
var ret = "\n\nSPECIFICATION: " + (result.SpecificationName ?? result.FoundOnMemberInfo.Name) + " ";
ret += (result.Passed ? "PASSED" : "FAILED") + " " + result.Message + "\n\n";
if (result.Thrown != null)
ret += result.Thrown + "\n\n";
ret += "\tEXPECTATIONS:\n\t-------------\n";
foreach (var exp in result.Expectations)
{
if (!exp.Passed)
ret += "\n\t<<<----------\n";
ret += "\t" + exp.Text + " " + (exp.Passed ? "PASSED" : "FAILED") + "\n";
if (!exp.Passed)
{
ret += PadMultiLineText(exp.Exception.Message) + "\n\n";
ret += "\t>>>----------\n\n";
}
}
return ret;
}
static string PadMultiLineText(string txt)
{
string[] lines = txt.Split(new[] {"\n"}, StringSplitOptions.None);
return lines.Aggregate("", (current, line) => current + ("\t" + line + "\n"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment