Skip to content

Commit

Permalink
Add support for executing tests in nested classes when parent class i…
Browse files Browse the repository at this point in the history
…s targeted #4.
  • Loading branch information
jcansdale committed Jul 17, 2016
1 parent 67b470f commit b688206
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/NUnitTDNet.Adapter.Examples/Expected/NestedClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
using NUnit.Framework;
using TestDriven.Framework;

// Should we attempt to run nested classes?
[ExpectTestRun(TestRunState.NoTests)]
[ExpectTestRun(TestRunState.Success, PassedCount = 1)]
public class NestedClassTests
{
[ExpectTestRun(TestRunState.Success, PassedCount = 1)]
Expand Down
17 changes: 10 additions & 7 deletions src/NUnitTDNet.Adapter/ConsoleTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ public TestRunState RunAssembly(ITestListener testListener, Assembly assembly)

public TestRunState RunMember(ITestListener testListener, Assembly assembly, MemberInfo member)
{
string testPath = Utilities.GetTestPath(member);
return executeConsoleRunner(testListener, assembly, testPath);
var testPaths = Utilities.GetTestPaths(member);
return executeConsoleRunner(testListener, assembly, testPaths);
}

public TestRunState RunNamespace(ITestListener testListener, Assembly assembly, string ns)
{
return executeConsoleRunner(testListener, assembly, ns);
var testPaths = new string[] { ns };
return executeConsoleRunner(testListener, assembly, testPaths);
}

TestRunState executeConsoleRunner(ITestListener testListener, Assembly testAssembly, string testPath)
TestRunState executeConsoleRunner(ITestListener testListener, Assembly testAssembly, string[] testPaths)
{
var exeFile = findConsoleRunner();
if(exeFile == null)
Expand All @@ -39,13 +40,15 @@ TestRunState executeConsoleRunner(ITestListener testListener, Assembly testAssem

string assemblyFile = new Uri(testAssembly.EscapedCodeBase).LocalPath;
string arguments = quote(assemblyFile);
if(testPath != null)
if(testPaths != null)
{
arguments += " --test=" + quote(testPath);
foreach(var testPath in testPaths)
{
arguments += " --test=" + quote(testPath);
}
}

arguments += " --process:InProcess";
//arguments += " --teamcity";

var startInfo = new ProcessStartInfo(exeFile, arguments);
startInfo.UseShellExecute = false;
Expand Down
17 changes: 11 additions & 6 deletions src/NUnitTDNet.Adapter/EngineTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public class EngineTestRunner : TDF.ITestRunner
public TDF.TestRunState RunAssembly(TDF.ITestListener testListener, Assembly assembly)
{
var testPath = new Uri(assembly.EscapedCodeBase).LocalPath;
return run(testListener, assembly, testPath);
var testPaths = new string[] { testPath };
return run(testListener, assembly, testPaths);
}

public TDF.TestRunState RunMember(TDF.ITestListener testListener, Assembly assembly, MemberInfo member)
{
string testPath = Utilities.GetTestPath(member);
return run(testListener, assembly, testPath);
var testPaths = Utilities.GetTestPaths(member);
return run(testListener, assembly, testPaths);
}

public TDF.TestRunState RunNamespace(TDF.ITestListener testListener, Assembly assembly, string ns)
Expand All @@ -38,10 +39,11 @@ public TDF.TestRunState RunNamespace(TDF.ITestListener testListener, Assembly as
testPath = new Uri(assembly.EscapedCodeBase).LocalPath;
}

return run(testListener, assembly, testPath);
var testPaths = new string[] { testPath };
return run(testListener, assembly, testPaths);
}

TDF.TestRunState run(TDF.ITestListener testListener, Assembly testAssembly, string testPath)
TDF.TestRunState run(TDF.ITestListener testListener, Assembly testAssembly, string[] testPaths)
{
using (var engine = new TestEngineClass())
{
Expand All @@ -55,7 +57,10 @@ TDF.TestRunState run(TDF.ITestListener testListener, Assembly testAssembly, stri

var filterService = engine.Services.GetService<ITestFilterService>();
ITestFilterBuilder builder = filterService.GetTestFilterBuilder();
builder.AddTest(testPath);
foreach(var testPath in testPaths)
{
builder.AddTest(testPath);
}

var filter = builder.GetFilter();

Expand Down
34 changes: 30 additions & 4 deletions src/NUnitTDNet.Adapter/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
namespace NUnitTDNet.Adapter
{
using System;
using System.Collections.Generic;
using System.Reflection;

class Utilities
{
public static string GetTestPath(MemberInfo member)
public static string[] GetTestPaths(MemberInfo member)
{
if (member is Type)
{
Type type = (Type)member;
return type.FullName;
var targetType = (Type)member;
var types = includeNestedTypes(targetType);
var testPathList = new List<string>();
foreach (var type in types)
{
testPathList.Add(type.FullName);
}

return testPathList.ToArray();
}

if (member is MethodInfo)
{
MethodInfo methodInfo = (MethodInfo)member;
return methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
var testPath = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
var testPaths = new string[] { testPath };
return testPaths;
}

throw new Exception("Member type not supported: " + member.GetType());
}

static Type[] includeNestedTypes(Type type)
{
var types = new List<Type>();
includeNestedTypes(types, type);
return types.ToArray();
}

static void includeNestedTypes(List<Type> types, Type type)
{
types.Add(type);
foreach (var nestedType in type.GetNestedTypes())
{
includeNestedTypes(types, nestedType);
}
}
}
}

0 comments on commit b688206

Please sign in to comment.