Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions NSpec/Domain/Formatters/XUnitFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace NSpec.Domain.Formatters
{
[Serializable]
public class XUnitFormatter : IFormatter
{
public void Write(ContextCollection contexts)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xml = new XmlTextWriter(sw);

xml.WriteStartElement("testsuites");
xml.WriteAttributeString("tests", contexts.Examples().Count().ToString());
xml.WriteAttributeString("errors", "0");
xml.WriteAttributeString("failures", contexts.Failures().Count().ToString());
xml.WriteAttributeString("skip", contexts.Pendings().Count().ToString());

contexts.Do(c => this.BuildContext(xml, c));
xml.WriteEndElement();

Console.WriteLine(sb.ToString());
}

void BuildContext(XmlTextWriter xml, Context context)
{
if (context.Level == 1)
{
xml.WriteStartElement("testsuite");
xml.WriteAttributeString("tests", context.AllExamples().Count().ToString());
xml.WriteAttributeString("name", context.Name);
xml.WriteAttributeString("errors", "0");
xml.WriteAttributeString("failures", context.Failures().Count().ToString());
}

context.Examples.Do(e => this.BuildSpec(xml, e));
context.Contexts.Do(c => this.BuildContext(xml, c));

if (context.Level == 1)
{
xml.WriteEndElement();
}
}

void BuildSpec(XmlTextWriter xml, Example example)
{
xml.WriteStartElement("testcase");

string testName = example.Spec;
StringBuilder className = new StringBuilder();
ComposePartialName(example.Context, className);

xml.WriteAttributeString("classname", className.ToString());
xml.WriteAttributeString("name", testName);

if (example.Exception != null)
{
xml.WriteStartElement("failure");
xml.WriteAttributeString("type", example.Exception.GetType().Name);
xml.WriteAttributeString("message", example.Exception.Message);
xml.WriteString(example.Exception.ToString());
xml.WriteEndElement();
}

xml.WriteEndElement();
}

private void ComposePartialName(Context context, StringBuilder contextName)
{
if (context.Level <= 1) { return; }

ComposePartialName(context.Parent, contextName);
if (contextName.Length > 0) { contextName.Append(", "); }

contextName.Append(context.Name);
}
}
}
1 change: 1 addition & 0 deletions NSpec/NSpec.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Domain\Formatters\TiddlyWikiFormatter.cs" />
<Compile Include="Domain\Extensions\DomainExtensions.cs" />
<Compile Include="Domain\Formatters\XmlFormatter.cs" />
<Compile Include="Domain\Formatters\XUnitFormatter.cs" />
<Compile Include="Domain\MethodContext.cs" />
<Compile Include="Domain\ExceptionNotThrown.cs" />
<Compile Include="DefaultConventions.cs" />
Expand Down
63 changes: 58 additions & 5 deletions NSpecRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using NSpec;
using NSpec.Domain;
using NSpec.Domain.Formatters;

namespace NSpecRunner
{
Expand All @@ -22,8 +23,11 @@ static void Main(string[] args)
var argsTags = "";

var failFast = IsFailFast(args);
var formatterClassName = GetFormatterClassName(args);

args = RemoveFailFastSwitch(args);
var formatter = FindFormatter(formatterClassName);

args = RemoveOptionsAndSwitches(args);

if (args.Length > 1)
{
Expand All @@ -38,7 +42,7 @@ static void Main(string[] args)

var specDLL = args[0];

var invocation = new RunnerInvocation(specDLL, argsTags, failFast);
var invocation = new RunnerInvocation(specDLL, argsTags, formatter, failFast);

var domain = new NSpecDomain(specDLL + ".config");

Expand All @@ -54,16 +58,60 @@ static void Main(string[] args)
}
}

public static string[] RemoveFailFastSwitch(string[] args)
public static string[] RemoveOptionsAndSwitches(string[] args)
{
return args.Where(s => s != "--failfast").ToArray();
return args.Where(s => !s.StartsWith("--") || s == "--tag" ).ToArray();
}

public static bool IsFailFast(string[] args)
{
return args.Any(s => s == "--failfast");
}

public static string GetFormatterClassName(string[] args)
{
string formatter = args.FirstOrDefault(s => s.StartsWith("--formatter=") );
if (formatter != null)
{
return formatter.Substring("--formatter=".Length).ToLowerInvariant();
}
else
{
return null;
}
}

/// <summary>
/// Find an implementation of IFormatter with the given class name
/// </summary>
/// <param name="formatterClassName"></param>
/// <returns></returns>
private static IFormatter FindFormatter(string formatterClassName)
{
// Default formatter is the standard console formatter
if (string.IsNullOrEmpty(formatterClassName))
{
return new ConsoleFormatter();
}

Assembly nspecAssembly = typeof(IFormatter).Assembly;

// Look for a class that implements IFormatter with the provided name
var formatterType = nspecAssembly.GetTypes().FirstOrDefault(type =>
(type.Name.ToLowerInvariant() == formatterClassName)
&& typeof(IFormatter).IsAssignableFrom(type) );

if (formatterType != null)
{
return (IFormatter)Activator.CreateInstance(formatterType);
}
else
{
throw new TypeLoadException("Could not find formatter type " + formatterClassName);

}
}

private static void ShowUsage()
{
Console.WriteLine("VERSION: {0}".With(Assembly.GetExecutingAssembly().GetName().Version));
Expand All @@ -82,13 +130,18 @@ private static void ShowUsage()
Console.WriteLine();
Console.WriteLine("nspecrunner path_to_spec_dll --tag tag1,tag2,tag3");
Console.WriteLine();
Console.WriteLine("This wil run all tests under tags specified. A test class's name is automatically considered in tagging.");
Console.WriteLine("This will run all tests under tags specified. A test class's name is automatically considered in tagging.");
Console.WriteLine();
Console.WriteLine("Example usage (failfast):");
Console.WriteLine();
Console.WriteLine("nspecrunner path_to_spec_dll [classname] --failfast");
Console.WriteLine();
Console.WriteLine("Adding --failfast to any of the commands above will stop execution immediately when a failure is encountered.");
Console.WriteLine();
Console.WriteLine("nspecrunner path_to_spec_dll [classname] --formatter=formatterClass");
Console.WriteLine();
Console.WriteLine("You can optionally specify a formatter for the output by providing the class name of the desired formatter.");

}
}
}
9 changes: 8 additions & 1 deletion NSpecRunner/app.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<!-- Run partial trust V2 assemblies in full trust under .NET 4.0 -->
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>