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 test case filter to RunOptions (Support for #327) #352

Closed
Closed
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
13 changes: 13 additions & 0 deletions Source/Examples/Example/FailingSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Machine.Specifications;

namespace Example
{
[Subject("example")]
public class when_context_with_passing_and_failing_clauses
{
It should_fail = () => throw new Exception();

It should_pass = () => { };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

using System.Linq;
using System.Reflection;

using Example;

using FluentAssertions;
using Machine.Specifications.Runner;
using Machine.Specifications.Runner.Impl;

namespace Machine.Specifications.Specs.Runner
{
[Subject("Specification Runner")]
public class when_running_a_context_while_filtering_out_failing_tests
{
static TestListener testListener;

static DefaultRunner runner;

Establish context = () =>
{
testListener = new TestListener();
var filters = new[]
{
new ContextFilter(
"Example.when_context_with_passing_and_failing_clauses",
new[]
{
new SpecifiactionFilter("should_pass"),
})
};
var runOptions = new RunOptions(
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
filters);

runner = new DefaultRunner(
testListener,
runOptions);
};

Because of =
() => runner.RunAssemblyContainingType<when_context_with_passing_and_failing_clauses>();

It should_succeed =
() => testListener.LastResult.Passed.Should().BeTrue();

It should_run_spec =
() => testListener.SpecCount.Should().Be(1);
}

public static class RunnerExtensions
{
public static void RunAssemblyContainingType<T>(
this DefaultRunner runner)
where T : new()
{
runner.RunAssembly(typeof(T).GetTypeInfo().Assembly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ public override void BeforeEachTest()
}

[Test]
public void ShouldReturnThreeContexts()
public void ShouldReturnFourContexts()
{
specifications.Count().Should().Be(3);
specifications.Count().Should().Be(4);
}

[Test]
public void ShouldReturnThreeContextsNamedCorrectly()
public void ShouldReturnFourContextsNamedCorrectly()
{
var names = specifications.Select(x => x.Name).OrderBy(x => x).ToList();
names.Should().BeEquivalentTo(
new[]
{
"when context with passing and failing clauses",
"when a customer first views the account summary page",
"when transferring between two accounts",
"when transferring an amount larger than the balance of the from account"
Expand Down
22 changes: 22 additions & 0 deletions Source/Machine.Specifications/Runner/ContextFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Machine.Specifications.Runner
{
#if !NETSTANDARD
[Serializable]
#endif
public class ContextFilter
{
public ContextFilter(string name, IEnumerable<SpecifiactionFilter> specificFilters)
{
Name = name;
SpecificationFilters = specificFilters.ToList();
}

public string Name { get; }

public IEnumerable<SpecifiactionFilter> SpecificationFilters { get; }
}
}
23 changes: 20 additions & 3 deletions Source/Machine.Specifications/Runner/Impl/DefaultRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,26 @@ public static IEnumerable<Context> FilteredBy(this IEnumerable<Context> contexts

if (options.Filters.Any())
{
var includeFilters = options.Filters;

results = results.Where(x => includeFilters.Any(filter => StringComparer.OrdinalIgnoreCase.Equals(filter, x.Type.FullName)));
var includeFilters = options.Filters.ToList();

results = results.Where(x => includeFilters.Any(filter => StringComparer.OrdinalIgnoreCase.Equals(filter.Name, x.Type.FullName)))
.Select(x =>
{
ContextFilter contextFilter = includeFilters.Single(y => StringComparer.OrdinalIgnoreCase
.Equals(y.Name, x.Type.FullName));
List<SpecifiactionFilter> specifiactionFilters = contextFilter.SpecificationFilters
.ToList();
if (!specifiactionFilters.Any())
{
return x;
}

var specsToRun = x.Specifications
.Where(s => specifiactionFilters.Any(sf => StringComparer.OrdinalIgnoreCase.Equals(sf.Name, s.FieldInfo.Name)));
x.Filter(specsToRun);
return x;
})
.Where(x => x.Specifications.Any());
}

if (options.IncludeTags.Any())
Expand Down
16 changes: 13 additions & 3 deletions Source/Machine.Specifications/Runner/RunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@ public class RunOptions
{
public IEnumerable<string> IncludeTags { get; private set; }
public IEnumerable<string> ExcludeTags { get; private set; }
public IEnumerable<string> Filters { get; private set; }
public IEnumerable<ContextFilter> Filters { get; private set; }
public IEnumerable<string> Contexts { get; private set; }

public RunOptions(IEnumerable<string> includeTags, IEnumerable<string> excludeTags, IEnumerable<string> filters)
: this(includeTags, excludeTags, filters, Enumerable.Empty<string>())
{
}

public RunOptions(IEnumerable<string> includeTags, IEnumerable<string> excludeTags, IEnumerable<ContextFilter> filters)
: this(includeTags, excludeTags, filters, Enumerable.Empty<string>())
{
}

public RunOptions(IEnumerable<string> includeTags, IEnumerable<string> excludeTags, IEnumerable<string> filters, IEnumerable<string> contexts)
: this(includeTags, excludeTags, filters.Select(x => new ContextFilter(x, new SpecifiactionFilter[0])), contexts)
{
}

public RunOptions(IEnumerable<string> includeTags, IEnumerable<string> excludeTags, IEnumerable<ContextFilter> filters, IEnumerable<string> contexts)
{
IncludeTags = includeTags;
ExcludeTags = excludeTags;
Filters = filters;
Contexts = contexts;
}

public static RunOptions Default { get { return new RunOptions(Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<string>()); } }
public static RunOptions Default { get { return new RunOptions(Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<ContextFilter>(), Enumerable.Empty<string>()); } }

public static RunOptions Parse(string runOptionsXml)
{
Expand All @@ -39,7 +49,7 @@ public static RunOptions Parse(string runOptionsXml)

IEnumerable<string> includeTags = Parse(document, "/runoptions/includetags/tag");
IEnumerable<string> excludeTags = Parse(document, "/runoptions/excludetags/tag");
IEnumerable<string> filters = Parse(document, "/runoptions/filters/filter");
IEnumerable<ContextFilter> filters = Parse(document, "/runoptions/filters/filter").Select(x => new ContextFilter(x, Enumerable.Empty<SpecifiactionFilter>()));
IEnumerable<string> contexts = Parse(document, "/runoptions/contexts/context");

return new RunOptions(includeTags, excludeTags, filters, contexts);
Expand Down
17 changes: 17 additions & 0 deletions Source/Machine.Specifications/Runner/SpecifiactionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Machine.Specifications.Runner
{
#if !NETSTANDARD
[Serializable]
#endif
public class SpecifiactionFilter
{
public SpecifiactionFilter(string name)
{
Name = name;
}

public string Name { get; }
}
}
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
environment:
nuget_version: '0.12.0'
nuget_version: '0.13.0'
nuget_prerelease: false
assembly_version: '0.12.0.0'
assembly_version: '0.13.0.0'

image: Visual Studio 2017

deploy:
- provider: GitHub
description: |
* Track behavior field from context in behavior specification
* Add filtering for test cases to RunOptions

on:
appveyor_repo_tag: true
Expand Down