Skip to content

Commit 37eba9d

Browse files
[Bug] Fix execution of Establish and Cleanup when running specific tests (#87)
* [Bug] Fix execution of Establish and Cleanup when running specific tests * add tests * tidy * tidy * rename
1 parent 7791803 commit 37eba9d

File tree

4 files changed

+86
-27
lines changed

4 files changed

+86
-27
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Linq;
2+
using Machine.Fakes;
3+
using Machine.Specifications;
4+
using Machine.VSTestAdapter.Helpers;
5+
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
6+
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
7+
8+
namespace Machine.VSTestAdapter.Specs.Execution
9+
{
10+
public class When_cleaning_up_a_context : With_MultipleSpecExecutionSetup
11+
{
12+
Establish context = () =>
13+
SpecificationsToRun = new[]
14+
{
15+
new VisualStudioTestIdentifier("SampleSpecs.CleanupSpec", "should_not_increment_cleanup"),
16+
new VisualStudioTestIdentifier("SampleSpecs.CleanupSpec", "should_have_no_cleanups")
17+
};
18+
19+
It should_tell_visual_studio_it_passed = () =>
20+
{
21+
The<IFrameworkHandle>()
22+
.WasToldTo(x => x.RecordEnd(
23+
Param<TestCase>.Matches(y => SpecificationsToRun.Contains(y.ToVisualStudioTestIdentifier())),
24+
Param<TestOutcome>.Matches(y => y == TestOutcome.Passed)))
25+
.Twice();
26+
27+
The<IFrameworkHandle>()
28+
.WasToldTo(x =>x.RecordResult(Param<TestResult>.Matches(y => y.Outcome == TestOutcome.Passed)))
29+
.Twice();
30+
};
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.IO;
3+
using Machine.Fakes;
4+
using Machine.Specifications;
5+
using Machine.VSTestAdapter.Configuration;
6+
using Machine.VSTestAdapter.Execution;
7+
using Machine.VSTestAdapter.Helpers;
8+
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
9+
10+
namespace Machine.VSTestAdapter.Specs.Execution
11+
{
12+
public abstract class With_MultipleSpecExecutionSetup : WithFakes
13+
{
14+
protected static ISpecificationExecutor Executor;
15+
protected static string AssemblyPath;
16+
protected static VisualStudioTestIdentifier[] SpecificationsToRun;
17+
18+
Establish context = () =>
19+
{
20+
Executor = new SpecificationExecutor();
21+
AssemblyPath = Path.Combine(Helper.GetTestDirectory(), "SampleSpecs.dll");
22+
};
23+
24+
Because of = () =>
25+
Executor.RunAssemblySpecifications(AssemblyPath, SpecificationsToRun, The<Settings>(), new Uri("bla://executor"), The<IFrameworkHandle>());
26+
}
27+
}

Source/Machine.VSTestAdapter/Execution/TestExecutor.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
using Machine.Specifications.Runner.Impl;
33
using System;
44
using System.Collections.Generic;
5-
using System.Diagnostics;
6-
using System.IO;
75
using System.Linq;
86
using System.Reflection;
9-
using Machine.Specifications.Explorers;
10-
using Machine.Specifications.Model;
117
using Machine.Specifications;
128
using Machine.VSTestAdapter.Helpers;
139

@@ -48,7 +44,6 @@ private DefaultRunner CreateRunner(Assembly assembly,ISpecificationRunListener s
4844
return new DefaultRunner(listener, RunOptions.Default);
4945
}
5046

51-
5247
public void RunTestsInAssembly(string pathToAssembly, IEnumerable<VisualStudioTestIdentifier> specsToRun, ISpecificationRunListener specificationRunListener)
5348
{
5449
DefaultRunner mspecRunner = null;
@@ -59,34 +54,19 @@ public void RunTestsInAssembly(string pathToAssembly, IEnumerable<VisualStudioTe
5954
assemblyToRun = AssemblyHelper.Load(pathToAssembly);
6055
mspecRunner = CreateRunner(assemblyToRun, specificationRunListener);
6156

62-
IEnumerable<Context> specificationContexts = new AssemblyExplorer().FindContextsIn(assemblyToRun) ?? Enumerable.Empty<Context>();
63-
Dictionary<string, Context> contextMap = specificationContexts.ToDictionary(c => c.Type.FullName, StringComparer.Ordinal);
57+
var specsByContext = specsToRun.GroupBy(x => x.ContainerTypeFullName);
6458

65-
// We use explicit assembly start and end to wrap the RunMember loop
6659
mspecRunner.StartRun(assemblyToRun);
6760

68-
foreach (VisualStudioTestIdentifier test in specsToRun)
61+
foreach (var specs in specsByContext)
6962
{
70-
Context context = contextMap[test.ContainerTypeFullName];
71-
if (context == null)
72-
continue;
73-
74-
Specification specification = context.Specifications.SingleOrDefault(spec => spec.FieldInfo.Name.Equals(test.FieldName, StringComparison.Ordinal));
75-
76-
if (specification is BehaviorSpecification)
77-
{
78-
// MSpec doesn't expose any way to run an an "It" coming from a "[Behavior]", so we have to do some trickery
79-
VisualStudioTestIdentifier listenFor = specification.ToVisualStudioTestIdentifier(context);
80-
DefaultRunner behaviorRunner = new DefaultRunner(new SingleBehaviorTestRunListenerWrapper(specificationRunListener, listenFor), RunOptions.Default);
81-
behaviorRunner.RunMember(assemblyToRun, context.Type.GetTypeInfo());
82-
}
83-
else
84-
{
85-
mspecRunner.RunMember(assemblyToRun, specification.FieldInfo);
86-
}
63+
var fields = specs.Select(x => x.FieldName);
8764

65+
mspecRunner.RunType(assemblyToRun, assemblyToRun.GetType(specs.Key), fields.ToArray());
8866
}
89-
} catch (Exception e) {
67+
}
68+
catch (Exception e)
69+
{
9070
specificationRunListener.OnFatalError(new ExceptionResult(e));
9171
}
9272
finally

Source/SampleSpecs/CleanupSpec.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Machine.Specifications;
2+
3+
namespace SampleSpecs
4+
{
5+
public class CleanupSpec
6+
{
7+
static int cleanup_count;
8+
9+
Because of = () => { };
10+
11+
Cleanup after = () =>
12+
cleanup_count++;
13+
14+
It should_not_increment_cleanup = () =>
15+
cleanup_count.ShouldEqual(0);
16+
17+
It should_have_no_cleanups = () =>
18+
cleanup_count.ShouldEqual(0);
19+
}
20+
}

0 commit comments

Comments
 (0)