Skip to content

Commit

Permalink
[Touch.Client] Add API to exclude tests based on categories.
Browse files Browse the repository at this point in the history
The NUnitLite API to create test filters is very rudimentary (either subclass
TestFilter, or have NUnitLite create the filter based on an xml string), so we
can at least make it easy for consumers to exclude tests based on categories,
which we need to be able to do for our tests anyway.
  • Loading branch information
rolfbjarne committed Jul 22, 2020
1 parent e6fb7d7 commit dafdf9f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 0 deletions.
42 changes: 42 additions & 0 deletions NUnitLite/TouchRunner/ExcludedCategoryFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;

using NUnit.Framework.Internal;
using NUnit.Framework.Interfaces;

namespace MonoTouch.NUnit.UI {
class ExcludeCategoryFilter : TestFilter {
public HashSet<string> ExcludedCategories { get; private set; }

public ExcludeCategoryFilter (IEnumerable<string> categories)
{
ExcludedCategories = new HashSet<string> (categories);
}

public override TNode AddToXml (TNode parentNode, bool recursive)
{
throw new NotImplementedException ();
}

public override bool Match (ITest test)
{
var categories = test.Properties ["Category"];
if (categories != null) {
foreach (string cat in categories) {
if (ExcludedCategories.Contains (cat))
return false;
}
}

if (test.Parent != null)
return Match (test.Parent);

return true;
}

public override bool Pass (ITest test)
{
return Match (test);
}
}
}
14 changes: 14 additions & 0 deletions NUnitLite/TouchRunner/TouchRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
Expand Down Expand Up @@ -84,6 +85,8 @@ public ITestFilter Filter {
set { filter = value; }
}

public HashSet<string> ExcludedCategories { get; set; }

public bool TerminateAfterExecution {
get { return TouchOptions.Current.TerminateAfterExecution && !connection_failure; }
set { TouchOptions.Current.TerminateAfterExecution = value; }
Expand Down Expand Up @@ -591,6 +594,10 @@ public void Run (Test test)

#if NUNITLITE_NUGET
var filter = new MatchTestFilter { MatchTest = test };
if (this.filter != null)
filter.AndFilters.Add (this.filter);
if (ExcludedCategories != null)
filter.AndFilters.Add (new ExcludeCategoryFilter (ExcludedCategories));
foreach (var runner in runners)
runner.Run (this, filter);

Expand Down Expand Up @@ -878,6 +885,7 @@ protected override void ExecuteOnMainThread (Action action)
// A filter that matches a specific test
class MatchTestFilter : TestFilter {
public ITest MatchTest;
public List<ITestFilter> AndFilters = new List<ITestFilter> ();

#if NUNITLITE_NUGET
public override TNode AddToXml (TNode parentNode, bool recursive)
Expand All @@ -888,6 +896,12 @@ public override TNode AddToXml (TNode parentNode, bool recursive)

public override bool Match (ITest test)
{
if (AndFilters != null) {
// If any of the And filters returns false, then return false too.
if (AndFilters.Any ((v) => !v.Pass (test)))
return false;
}

return IsMatch (test, MatchTest);
}

Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/dotnet/iOS/Touch.Client-iOS.dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<AssemblyName>Touch.Client</AssemblyName>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/dotnet/tvOS/Touch.Client-tvOS.dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<AssemblyName>Touch.Client</AssemblyName>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/iOS/Touch.Client-iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/macOS/full/Touch.Client-macOS-full.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/macOS/mobile/Touch.Client-macOS-mobile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/tvOS/Touch.Client-tvOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Touch.Client/watchOS/Touch.Client-watchOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
<Link>ExcludedCategoryFilter.cs</Link>
</Compile>
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
<Link>HttpTextWriter.cs</Link>
</Compile>
Expand Down

0 comments on commit dafdf9f

Please sign in to comment.