Created
July 25, 2014 09:51
-
-
Save vendettamit/23628bdfe1410c3e697a to your computer and use it in GitHub Desktop.
InMemory NunitTestRunner
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Xml; | |
using System.Xml.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
using NUnit.ConsoleRunner; | |
using NUnit.ConsoleRunner.Tests; | |
using NUnit.Engine; | |
using NUnit.Framework; | |
using NUnit.Framework.Interfaces; | |
using NUnit.Framework.Internal; | |
using NUnit.Framework.Internal.Builders; | |
using InternalTraceLevel = NUnit.Engine.InternalTraceLevel; | |
using TestFilter = NUnit.Engine.TestFilter; | |
using TestListener = NUnit.Framework.Internal.TestListener; | |
namespace SampleUnitTest | |
{ | |
class Program | |
{ | |
private SchemaValidator validator; | |
private static readonly string schemaFile = "NUnit2TestResult.xsd"; | |
private ITestEngine engine; | |
private string localDirectory; | |
protected TestEngineResult EngineResult { get; private set; } | |
protected string GetLocalPath(string fileName) | |
{ | |
return Path.Combine(localDirectory, fileName); | |
} | |
public void doSomethign() | |
{ | |
Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase); | |
localDirectory = Path.GetDirectoryName(uri.LocalPath); | |
engine = TestEngineActivator.CreateInstance(null, InternalTraceLevel.Off); | |
var assemblyPath = GetLocalPath("mock-assembly.dll"); | |
var settings = new Dictionary<string, object>(); | |
var runner = new DefaultTestAssemblyRunner(new DefaultTestAssemblyBuilder()); | |
Assert.True(runner.Load(Assembly.GetExecutingAssembly(), settings), "Unable to load mock-assembly.dll"); | |
// Convert our own framework XmlNode to a TestEngineResult | |
var package = new TestPackage(assemblyPath); | |
this.EngineResult = TestEngineResult.MakeTestRunResult( | |
package, | |
DateTime.Now, | |
new TestEngineResult( | |
runner.Run(TestListener.NULL, NUnit.Framework.Internal.TestFilter.Empty).ToXml(true).OuterXml)); | |
} | |
static void Main(string[] args) | |
{ | |
CoreExtensions.Host.InstallBuiltins(); | |
var testing = new Program(); | |
testing.doSomethign(); | |
Console.WriteLine(testing.SummaryTransformTest()); | |
Console.Read(); | |
} | |
public string SummaryTransformTest() | |
{ | |
var transformPath = GetLocalPath("Summary.xslt"); | |
StringWriter writer = new StringWriter(); | |
new XmlTransformOutputWriter(transformPath).WriteResultFile(EngineResult.Xml, writer); | |
string summary = string.Format( | |
"Tests Run: {0}, Passed: {1}, Failed: {2}", | |
this.EngineResult.Xml.Attributes["total"].Value, | |
this.EngineResult.Xml.Attributes["passed"].Value, | |
this.EngineResult.Xml.Attributes["failed"].Value); | |
var xml = XDocument.Load(new XmlNodeReader(this.EngineResult.Xml)); | |
string details = string.Empty; | |
IEnumerable<XElement> textSegs = xml.Descendants("test-case") | |
.Where(x => x.Attribute("result").Value.Equals("Failed", StringComparison.InvariantCultureIgnoreCase)); | |
if (textSegs.Any()) | |
{ | |
foreach (var seg in textSegs) | |
{ | |
var testCaseDescription = seg.Descendants("property") | |
.FirstOrDefault(x => x.Attribute("name") != null && x.Attribute("name").Value == "Description"); | |
details = string.Format( | |
"\n\n\n TestCase: {0} \n Failing message: {1}", | |
testCaseDescription != null | |
? testCaseDescription.Attribute("value").Value | |
: seg.Attributes("name").First().Value, | |
seg.Descendants("message").First().Value); | |
} | |
} | |
// string output = writer.GetStringBuilder().ToString(); | |
// Assert.That(output, Contains.Substring(summary)); | |
return summary + details; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment