Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ITestContext hook #407

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using Machine.Specifications.Explorers;
using Machine.Specifications.Model;
using Machine.Specifications.Runner;
using Machine.Specifications.Specs.Runner;

namespace Machine.Specifications.Specs.Explorers
Expand Down Expand Up @@ -181,4 +182,43 @@ public void OnAssemblyComplete()
{
}
}

public class TestContext : ITestContext
{
public void OnAssemblyStart(AssemblyInfo assembly)
{
}

public void OnAssemblyEnd(AssemblyInfo assembly)
{
}

public void OnRunStart()
{
}

public void OnRunEnd()
{
}

public void OnContextStart(ContextInfo context)
{
}

public void OnContextEnd(ContextInfo context)
{
}

public void OnSpecificationStart(SpecificationInfo specification)
{
}

public void OnSpecificationEnd(SpecificationInfo specification, Result result)
{
}

public void OnFatalError(ExceptionResult exception)
{
}
}
}
11 changes: 9 additions & 2 deletions src/Machine.Specifications/Explorers/AssemblyExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ public IEnumerable<ISupplementSpecificationResults> FindSpecificationSupplements
public IEnumerable<IAssemblyContext> FindAssemblyContextsIn(Assembly assembly)
{
return assembly.GetExportedTypes()
.Where(x => x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract && x.GetInterfaces().Contains(typeof(IAssemblyContext)))
.Select(x => (IAssemblyContext)Activator.CreateInstance(x));
.Where(x => x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract && x.GetInterfaces().Contains(typeof(IAssemblyContext)))
.Select(x => (IAssemblyContext)Activator.CreateInstance(x));
}

public IEnumerable<ITestContext> FindTestContextsIn(Assembly assembly)
{
return assembly.GetExportedTypes()
.Where(x => x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract && x.GetInterfaces().Contains(typeof(ITestContext)))
.Select(x => (ITestContext)Activator.CreateInstance(x));
}

Context CreateContextFrom(Type type)
Expand Down
8 changes: 8 additions & 0 deletions src/Machine.Specifications/ITestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Machine.Specifications.Runner;

namespace Machine.Specifications
{
public interface ITestContext : ISpecificationRunListener
{
}
}
16 changes: 11 additions & 5 deletions src/Machine.Specifications/Runner/Impl/AssemblyRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ internal class AssemblyRunner

readonly IList<IAssemblyContext> _executedAssemblyContexts;
readonly AssemblyExplorer _explorer;
readonly TestContextListener _testContext;

public AssemblyRunner(ISpecificationRunListener listener, RunOptions options)
{
RedirectOutputState state = new RedirectOutputState();
var state = new RedirectOutputState();
_testContext = new TestContextListener();
_listener = new AggregateRunListener(new[]
{
new AssemblyLocationAwareListener(),
_testContext,
new SetUpRedirectOutputRunListener(state),
listener,
new TearDownRedirectOutputRunListener(state),
Expand Down Expand Up @@ -77,9 +80,12 @@ void OnAssemblyStart(Assembly assembly)
{
try
{
var testContexts = _explorer.FindTestContextsIn(assembly);
_testContext.SetTestContexts(testContexts.ToList());

_listener.OnAssemblyStart(assembly.GetInfo());

IEnumerable<IAssemblyContext> assemblyContexts = _explorer.FindAssemblyContextsIn(assembly);
var assemblyContexts = _explorer.FindAssemblyContextsIn(assembly);
assemblyContexts.Each(assemblyContext =>
{
assemblyContext.OnAssemblyStart();
Expand Down Expand Up @@ -122,11 +128,11 @@ public void EndExplicitRunScope(Assembly assembly)
}

void RunContext(Context context,
IEnumerable<ICleanupAfterEveryContextInAssembly> globalCleanups,
IEnumerable<ISupplementSpecificationResults> supplements)
IEnumerable<ICleanupAfterEveryContextInAssembly> globalCleanups,
IEnumerable<ISupplementSpecificationResults> supplements)
{
var runner = ContextRunnerFactory.GetContextRunnerFor(context);
runner.Run(context, _listener, _options, globalCleanups, supplements);
}
}
}
}
93 changes: 93 additions & 0 deletions src/Machine.Specifications/Runner/Impl/TestContextListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections.Generic;

namespace Machine.Specifications.Runner.Impl
{
public class TestContextListener : RunListenerBase
{
List<ITestContext> _testContext = new List<ITestContext>();

public void SetTestContexts(List<ITestContext> testContexts)
{
_testContext = testContexts ?? new List<ITestContext>();
}

public override void OnAssemblyStart(AssemblyInfo assembly)
{
foreach (var testContext in _testContext)
{
testContext.OnAssemblyStart(assembly);
}
}

public override void OnAssemblyEnd(AssemblyInfo assembly)
{
foreach (var testContext in _testContext)
{
testContext.OnAssemblyEnd(assembly);
}
}

public override void OnRunStart()
{
foreach (var testContext in _testContext)
{
testContext.OnRunStart();
}
}

public override void OnRunEnd()
{
foreach (var testContext in _testContext)
{
testContext.OnRunEnd();
}
}

public override void OnContextStart(ContextInfo context)
{
foreach (var testContext in _testContext)
{
testContext.OnContextStart(context);
}
}

public override void OnContextEnd(ContextInfo context)
{
foreach (var testContext in _testContext)
{
testContext.OnContextEnd(context);
}
}

public override void OnSpecificationStart(SpecificationInfo specification)
{
foreach (var testContext in _testContext)
{
testContext.OnSpecificationStart(specification);
}
}

public override void OnSpecificationEnd(SpecificationInfo specification, Result result)
{
foreach (var testContext in _testContext)
{
testContext.OnSpecificationEnd(specification, result);
}
}

public override void OnFatalError(ExceptionResult exception)
{
foreach (var testContext in _testContext)
{
try
{
testContext.OnFatalError(exception);
}
catch
{
// ignored
}
}
}
}
}