Last active
May 1, 2017 11:16
-
-
Save madrang/f6830b1f1937af35ef3a9eb93caab9c8 to your computer and use it in GitHub Desktop.
Xunit.Sdk testing
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.Generic; | |
using System.Threading.Tasks; | |
using Xunit; | |
using Xunit.Sdk; | |
namespace Linsft.UnitTesting | |
{ | |
public class AssemblyTest | |
{ | |
/// <summary> | |
/// The tested assembly. | |
/// </summary> | |
public readonly Assembly TestedAssembly; | |
public override string Name { | |
get { return this.TestedAssembly.GetName ().Name; } | |
} | |
private bool IsReportDone; | |
public AssemblyTest (Assembly assembly): base(replay:true) | |
{ | |
if (assembly == null) { | |
throw new ArgumentNullException ("assembly"); | |
} | |
this.TestedAssembly = assembly; | |
this.IsReportDone = false; | |
} | |
public void BuildReport() | |
{ | |
foreach (Type type in this.TestedAssembly.GetExportedTypes ()) { | |
var classTest = ClassTest.FromType (type); | |
if (classTest == null) { | |
continue; | |
} | |
classTest.Subscribe (this); | |
} | |
} | |
} | |
public class ClassTest | |
{ | |
public ITestClassCommand TestClassCommand { get; private set; } | |
public Type TestedType { | |
get { return this.TestClassCommand.TypeUnderTest.Type; } | |
} | |
public static ClassTest FromType (Type type) | |
{ | |
ITestClassCommand testClassCommand = TestClassCommandFactory.Make (type); | |
if (testClassCommand == null) { | |
return null; | |
} | |
return new ClassTest (testClassCommand); | |
} | |
private bool IsReportDone; | |
public ClassTest (ITestClassCommand testClassCommand) | |
{ | |
if (testClassCommand == null) { | |
throw new ArgumentNullException("testClassCommand"); | |
} | |
this.TestClassCommand = testClassCommand; | |
this.IsReportDone = false; | |
} | |
public void BuildReport () | |
{ | |
Exception fixtureException = this.TestClassCommand.ClassStart (); | |
if (fixtureException != null) { | |
Exception ex = this.TestClassCommand.ClassFinish () ?? fixtureException; | |
throw new InvalidOperationException ("Error while creating method test.", ex); | |
} | |
List<IMethodInfo> methods = new List<IMethodInfo> (this.TestClassCommand.EnumerateTestMethods ()); | |
while (methods.Count > 0) { | |
int idx = this.TestClassCommand.ChooseNextTest (methods.AsReadOnly ()); | |
IMethodInfo testedMethod = methods [idx]; | |
foreach (MethodTest methodTest in MethodTest.FromMethod (this, testedMethod)) { | |
methodTest.Subscribe (this); | |
} | |
methods.Remove (testedMethod); | |
} | |
Exception exFinish = this.TestClassCommand.ClassFinish (); | |
if (exFinish != null) { | |
throw new InvalidOperationException ("Error while finishing method test.", exFinish); | |
} | |
} | |
} | |
public class MethodTest | |
{ | |
public ITestCommand TestCommand { get; private set; } | |
public object ObjectUnderTest { get; private set; } | |
public string Name { | |
get { return this.TestCommand.DisplayName; } | |
} | |
private bool IsReportDone; | |
public MethodTest (ITestCommand testCommand, object underTest): base(replay:true) | |
{ | |
this.TestCommand = testCommand; | |
this.ObjectUnderTest = underTest; | |
this.IsReportDone = false; | |
} | |
public static IEnumerable<MethodTest> FromMethod (ClassTest classTest, IMethodInfo testedMethod) | |
{ | |
foreach (ITestCommand command in TestCommandFactory.Make(classTest.TestClassCommand, testedMethod)) { | |
yield return new MethodTest (command, classTest.TestClassCommand.ObjectUnderTest); | |
} | |
} | |
public string BuildReport () | |
{ | |
MethodResult methodResult = this.TestCommand.Execute (this.ObjectUnderTest); | |
if (methodResult is PassedResult) { | |
return new Report (this.Name, LogLevel.Informative, "Success"); | |
} | |
var failedResult = methodResult as FailedResult; | |
if (failedResult != null) { | |
return new ExceptionReport (this.Name, LogLevel.Error, new FailedResultException (failedResult)); | |
} | |
var skipResult = methodResult as SkipResult; | |
if (skipResult != null) { | |
return new Report(this.Name, LogLevel.Warning, "Skiped: " + skipResult.Reason); | |
} | |
return new Report(this.Name, LogLevel.Warning, "Unknown test state"); | |
} | |
} | |
public class EnvironmentTest | |
{ | |
[Fact ()] | |
public void PhysicalMemory () | |
{ | |
ulong mem = 2048; | |
Assert.True (mem >= 1240, "TotalPhysicalMemory: " + mem); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment