Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from nunit/revert-13
Browse files Browse the repository at this point in the history
Revert multiple assembly functionality
  • Loading branch information
rprouse authored Mar 17, 2017
2 parents e8bf355 + b78c3cc commit ea7fcfe
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 272 deletions.
129 changes: 64 additions & 65 deletions src/NUnit.Portable.Agent/NUnitPortableDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,23 @@ namespace NUnit.Engine
/// </summary>
public class NUnitPortableDriver
{
internal const string INVALID_FRAMEWORK_MESSAGE = "Running tests against this version of the framework using this driver is not supported. Please update NUnit.Framework to the latest version.";
private const string LOAD_MESSAGE = "Method called without loading any assemblies";

private const string CONTROLLER_TYPE = "NUnit.Framework.Api.FrameworkController";
private const string LOAD_METHOD = "LoadTests";
private const string EXPLORE_METHOD = "ExploreTests";
private const string COUNT_METHOD = "CountTests";
private const string RUN_METHOD = "RunTests";
private const string RUN_ASYNC_METHOD = "RunTests";
private const string STOP_RUN_METHOD = "StopRun";

private static readonly ILogger log = InternalTrace.GetLogger("NUnit3PortableDriver");
private readonly List<TestAssemblyWrapper> _testWrappers = new List<TestAssemblyWrapper>();
const string LOAD_MESSAGE = "Method called without calling Load first";
const string INVALID_FRAMEWORK_MESSAGE = "Running tests against this version of the framework using this driver is not supported. Please update NUnit.Framework to the latest version.";

static readonly string CONTROLLER_TYPE = "NUnit.Framework.Api.FrameworkController";
static readonly string LOAD_METHOD = "LoadTests";
static readonly string EXPLORE_METHOD = "ExploreTests";
static readonly string COUNT_METHOD = "CountTests";
static readonly string RUN_METHOD = "RunTests";
static readonly string RUN_ASYNC_METHOD = "RunTests";
static readonly string STOP_RUN_METHOD = "StopRun";

static ILogger log = InternalTrace.GetLogger("NUnit3PortableDriver");

Assembly _testAssembly;
Assembly _frameworkAssembly;
object _frameworkController;
Type _frameworkControllerType;

/// <summary>
/// An id prefix that will be passed to the test framework and used as part of the
Expand All @@ -65,15 +69,17 @@ public class NUnitPortableDriver
public string Load(Assembly frameworkAssembly, Assembly testAssembly, IDictionary<string, object> settings)
{
var idPrefix = string.IsNullOrEmpty(ID) ? "" : ID + "-";
var frameworkController = CreateObject(CONTROLLER_TYPE, frameworkAssembly, testAssembly, idPrefix, settings);
if (frameworkController == null)
_frameworkAssembly = frameworkAssembly;
_testAssembly = testAssembly;

_frameworkController = CreateObject(CONTROLLER_TYPE, testAssembly, idPrefix, settings);
if (_frameworkController == null)
throw new NUnitPortableDriverException(INVALID_FRAMEWORK_MESSAGE);

var testWrapper = new TestAssemblyWrapper(testAssembly, frameworkController);
_testWrappers.Add(testWrapper);
_frameworkControllerType = _frameworkController.GetType();

log.Info("Loading {0} - see separate log file", testAssembly.FullName);
return testWrapper.ExecuteMethod(LOAD_METHOD) as string;
log.Info("Loading {0} - see separate log file", _testAssembly.FullName);
return ExecuteMethod(LOAD_METHOD) as string;
}

/// <summary>
Expand All @@ -83,18 +89,9 @@ public string Load(Assembly frameworkAssembly, Assembly testAssembly, IDictionar
/// <returns>The number of test cases</returns>
public int CountTestCases(string filter)
{
CheckAssembliesLoaded();

var count = 0;

foreach (var wrapper in _testWrappers)
{
object assemblyCount = wrapper.ExecuteMethod(COUNT_METHOD, filter);
if (assemblyCount is int)
count += (int)assemblyCount;
}

return count;
CheckLoadWasCalled();
object count = ExecuteMethod(COUNT_METHOD, filter);
return count != null ? (int)count : 0;
}

/// <summary>
Expand All @@ -105,11 +102,9 @@ public int CountTestCases(string filter)
/// <returns>An Xml string representing the result</returns>
public string Run(Action<string> callback, string filter)
{
CheckAssembliesLoaded();

Func<TestAssemblyWrapper, string> runTestsAction =
p => p.ExecuteMethod(RUN_METHOD, new[] {typeof(Action<string>), typeof(string)}, callback, filter) as string;
return SummarizeResults("Running", runTestsAction);
CheckLoadWasCalled();
log.Info("Running {0} - see separate log file", _testAssembly.FullName);
return ExecuteMethod(RUN_METHOD, new[] { typeof(Action<string>), typeof(string) }, callback, filter) as string;
}

/// <summary>
Expand All @@ -119,23 +114,18 @@ public string Run(Action<string> callback, string filter)
/// <param name="filter">A filter that controls which tests are executed</param>
public void RunAsync(Action<string> callback, string filter)
{
CheckAssembliesLoaded();

foreach (var assembly in _testWrappers)
{
log.Info("Running {0} - see separate log file", assembly.FullName);
assembly.ExecuteMethod(RUN_ASYNC_METHOD, new[] { typeof(Action<string>), typeof(string) }, callback, filter);
}
CheckLoadWasCalled();
log.Info("Running {0} - see separate log file", _testAssembly.FullName);
ExecuteMethod(RUN_ASYNC_METHOD, new[] { typeof(Action<string>), typeof(string) }, callback, filter);
}

/// <summary>
/// Cancel the ongoing test run. If no test is running, the call is ignored.
/// Cancel the ongoing test run. If no test is running, the call is ignored.
/// </summary>
/// <param name="force">If true, cancel any ongoing test threads, otherwise wait for them to complete.</param>
public void StopRun(bool force)
{
foreach (var assembly in _testWrappers)
assembly.ExecuteMethod(STOP_RUN_METHOD, force);
ExecuteMethod(STOP_RUN_METHOD, force);
}

/// <summary>
Expand All @@ -145,41 +135,50 @@ public void StopRun(bool force)
/// <returns>An Xml string representing the tests</returns>
public string Explore(string filter)
{
CheckAssembliesLoaded();
CheckLoadWasCalled();

Func<TestAssemblyWrapper, string> exploreTestsAction = p => p.ExecuteMethod(EXPLORE_METHOD, filter) as string;
return SummarizeResults("Exploring", exploreTestsAction);
log.Info("Exploring {0} - see separate log file", _testAssembly.FullName);
return ExecuteMethod(EXPLORE_METHOD, filter) as string;
}

#region Helper Methods

private string SummarizeResults(string logTask, Func<TestAssemblyWrapper, string> testAction)
void CheckLoadWasCalled()
{
var summary = new ResultSummary();
foreach (var assembly in _testWrappers)
{
if (_frameworkController == null)
throw new InvalidOperationException(LOAD_MESSAGE);
}

log.Info("{0} {1} - see separate log file", logTask, assembly.FullName);
summary.AddResult(testAction(assembly));
object CreateObject(string typeName, params object[] args)
{
var typeinfo = _frameworkAssembly.DefinedTypes.FirstOrDefault(t => t.FullName == typeName);
if (typeinfo == null)
{
log.Error("Could not find type {0}", typeName);
}
return summary.GetTestResults().ToString();
return Activator.CreateInstance(typeinfo.AsType(), args);
}

private void CheckAssembliesLoaded()
object ExecuteMethod(string methodName, params object[] args)
{
if (_testWrappers.Count == 0)
throw new InvalidOperationException(LOAD_MESSAGE);
//var method = _frameworkControllerType.GetMethod(methodName, BindingFlags.Public);
var method = _frameworkControllerType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
return ExecuteMethod(method, args);
}

private static object CreateObject(string typeName, Assembly frameworkAssembly, params object[] args)
object ExecuteMethod(string methodName, Type[] ptypes, params object[] args)
{
var typeinfo = frameworkAssembly.DefinedTypes.FirstOrDefault(t => t.FullName == typeName);
if (typeinfo == null)
var method = _frameworkControllerType.GetMethod(methodName, ptypes);
return ExecuteMethod(method, args);
}

object ExecuteMethod(MethodInfo method, params object[] args)
{
if (method == null)
{
log.Error("Could not find type {0}", typeName);
return null;
throw new NUnitPortableDriverException(INVALID_FRAMEWORK_MESSAGE);
}
return Activator.CreateInstance(typeinfo.AsType(), args);
return method.Invoke(_frameworkController, args);
}

#endregion
Expand Down
15 changes: 3 additions & 12 deletions src/NUnit.Portable.Agent/ResultSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,14 @@ public XDocument GetTestResults()
var test = new XElement("test-run");
test.Add(new XAttribute("id", "0"));
test.Add(new XAttribute("testcasecount", TestCount));
test.Add(new XAttribute("result", Result));
test.Add(new XAttribute("total", TestCount));
test.Add(new XAttribute("passed", PassCount));
test.Add(new XAttribute("failed", FailedCount));
test.Add(new XAttribute("inconclusive", InconclusiveCount));
test.Add(new XAttribute("skipped", TotalSkipCount));
test.Add(new XAttribute("asserts", AssertCount));

//Occurs when summarizing explore only
if (Result != null)
test.Add(new XAttribute("result", Result));

test.Add(new XAttribute("portable-engine-version", typeof(ResultSummary).GetTypeInfo().Assembly.GetName().Version.ToString()));

EndTime = DateTime.UtcNow;
Expand All @@ -102,14 +99,8 @@ public XDocument GetTestResults()
test.Add(new XAttribute("duration", Duration.ToString("0.000000", NumberFormatInfo.InvariantInfo)));

foreach (var result in _results)
{
XElement firstSuite = result.Name.LocalName == "test-run"
? result.Element(XName.Get("test-suite"))
: result;

if (firstSuite != null) test.Add(firstSuite);
}

test.Add(result);

return new XDocument(test);
}

Expand Down
64 changes: 0 additions & 64 deletions src/NUnit.Portable.Agent/TestAssemblyWrapper.cs

This file was deleted.

99 changes: 0 additions & 99 deletions test/NUnit.Portable.Agent.Tests/MultipleAssemblyTests.cs

This file was deleted.

Loading

0 comments on commit ea7fcfe

Please sign in to comment.