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

v4: Create ITestPackage interface #950

Closed
wants to merge 2 commits into from
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
17 changes: 17 additions & 0 deletions src/NUnitConsole/nunit3-console.tests/ConsoleMocks.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System.Text;
using NSubstitute;
using NUnit.Common;

Expand All @@ -13,5 +14,21 @@ public static ConsoleOptions Options(params string[] args)
var mockDefaultsProvider = Substitute.For<IDefaultOptionsProvider>();
return new ConsoleOptions(mockDefaultsProvider, mockFileSystem, args);
}

public class ExtendedTextWriter : NUnit.ConsoleRunner.ExtendedTextWriter
{
public override Encoding Encoding { get; }
public override void Write(ColorStyle style, string value) { }

public override void WriteLine(ColorStyle style, string value) { }

public override void WriteLabel(string label, object option) { }

public override void WriteLabel(string label, object option, ColorStyle valueStyle) { }

public override void WriteLabelLine(string label, object option) { }

public override void WriteLabelLine(string label, object option, ColorStyle valueStyle) { }
}
}
}
27 changes: 21 additions & 6 deletions src/NUnitConsole/nunit3-console.tests/MakeTestPackageTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.IO;
using NUnit.Common;
using NUnit.Engine;
using NUnit.Framework;

namespace NUnit.ConsoleRunner.Tests
Expand All @@ -11,7 +14,7 @@ public class MakeTestPackageTests
public void SingleAssembly()
{
var options = ConsoleMocks.Options("test.dll");
var package = ConsoleRunner.MakeTestPackage(options);
var package = CreatePackage(options);

Assert.That(package.SubPackages.Count, Is.EqualTo(1));
Assert.That(package.SubPackages[0].FullName, Is.EqualTo(Path.GetFullPath("test.dll")));
Expand All @@ -22,7 +25,7 @@ public void MultipleAssemblies()
{
var names = new [] { "test1.dll", "test2.dll", "test3.dll" };
var options = ConsoleMocks.Options(names);
var package = ConsoleRunner.MakeTestPackage(options);
var package = CreatePackage(options);

Assert.That(package.SubPackages.Count, Is.EqualTo(3));
Assert.That(package.SubPackages[0].FullName, Is.EqualTo(Path.GetFullPath("test1.dll")));
Expand Down Expand Up @@ -61,7 +64,7 @@ public void MultipleAssemblies()
public void WhenOptionIsSpecified_PackageIncludesSetting(string option, string key, object val)
{
var options = ConsoleMocks.Options("test.dll", option);
var package = ConsoleRunner.MakeTestPackage(options);
var package = CreatePackage(options);

Assert.That(package.Settings.ContainsKey(key), "Setting not included for {0}", option);
Assert.That(package.Settings[key], Is.EqualTo(val), "NumberOfTestWorkers not set correctly for {0}", option);
Expand All @@ -72,7 +75,7 @@ public void WhenOptionIsSpecified_PackageIncludesSetting(string option, string k
public void WhenDebugging_NumberOfTestWorkersDefaultsToZero()
{
var options = ConsoleMocks.Options("test.dll", "--debug");
var package = ConsoleRunner.MakeTestPackage(options);
var package = CreatePackage(options);

Assert.That(package.Settings["DebugTests"], Is.EqualTo(true));
Assert.That(package.Settings["NumberOfTestWorkers"], Is.EqualTo(0));
Expand All @@ -82,7 +85,7 @@ public void WhenDebugging_NumberOfTestWorkersDefaultsToZero()
public void WhenDebugging_NumberOfTestWorkersMayBeOverridden()
{
var options = ConsoleMocks.Options("test.dll", "--debug", "--workers=3");
var package = ConsoleRunner.MakeTestPackage(options);
var package = CreatePackage(options);

Assert.That(package.Settings["DebugTests"], Is.EqualTo(true));
Assert.That(package.Settings["NumberOfTestWorkers"], Is.EqualTo(3));
Expand All @@ -93,10 +96,22 @@ public void WhenDebugging_NumberOfTestWorkersMayBeOverridden()
public void WhenNoOptionsAreSpecified_PackageContainsOnlyTwoSettings()
{
var options = ConsoleMocks.Options("test.dll");
var package = ConsoleRunner.MakeTestPackage(options);
var package = CreatePackage(options);

Assert.That(package.Settings.Keys, Is.EquivalentTo(new string[] { "WorkDirectory", "DisposeRunners" }));
}

private static ITestPackage CreatePackage(ConsoleOptions options)
{
ITestPackage package;
using (var engine = new TestEngine())
{
var consoleRunner = new ConsoleRunner(engine, options, new ConsoleMocks.ExtendedTextWriter());
package = consoleRunner.MakeTestPackage(options);
}

return package;
}

}
}
12 changes: 6 additions & 6 deletions src/NUnitConsole/nunit3-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public int Execute()

DisplayTestFiles();

TestPackage package = MakeTestPackage(_options);
var package = MakeTestPackage(_options);

// We display the filters at this point so that any exception message
// thrown by CreateTestFilter will be understandable.
Expand All @@ -107,7 +107,7 @@ private void DisplayTestFiles()
_outWriter.WriteLine();
}

private int ExploreTests(TestPackage package, TestFilter filter)
private int ExploreTests(ITestPackage package, TestFilter filter)
{
XmlNode result;

Expand All @@ -130,7 +130,7 @@ private int ExploreTests(TestPackage package, TestFilter filter)
return ConsoleRunner.OK;
}

private int RunTests(TestPackage package, TestFilter filter)
private int RunTests(ITestPackage package, TestFilter filter)
{

var writer = new ColorConsoleWriter(!_options.NoColor);
Expand Down Expand Up @@ -364,9 +364,9 @@ private IResultWriter GetResultWriter(OutputSpecification spec)
}

// This is public static for ease of testing
public static TestPackage MakeTestPackage(ConsoleOptions options)
public ITestPackage MakeTestPackage(ConsoleOptions options)
{
TestPackage package = new TestPackage(options.InputFiles);
var package = _engine.CreatePackage(options.InputFiles);

if (options.ProcessModelSpecified)
package.AddSetting(EnginePackageSettings.ProcessModel, options.ProcessModel);
Expand Down Expand Up @@ -453,7 +453,7 @@ public static TestPackage MakeTestPackage(ConsoleOptions options)
/// <summary>
/// Sets test parameters, handling backwards compatibility.
/// </summary>
private static void AddTestParametersSetting(TestPackage testPackage, IDictionary<string, string> testParameters)
private static void AddTestParametersSetting(ITestPackage testPackage, IDictionary<string, string> testParameters)
{
testPackage.AddSetting(FrameworkPackageSettings.TestParametersDictionary, testParameters);

Expand Down
4 changes: 2 additions & 2 deletions src/NUnitEngine/nunit.engine.api/Extensibility/IProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IProject
/// specified in the project format.
/// </summary>
/// <returns>A TestPackage</returns>
TestPackage GetTestPackage();
ITestPackage GetTestPackage();

/// <summary>
/// Gets a TestPackage for a specific configuration
Expand All @@ -43,6 +43,6 @@ public interface IProject
/// </summary>
/// <param name="configName">The name of the config to use</param>
/// <returns>A TestPackage for the named configuration.</returns>
TestPackage GetTestPackage(string configName);
ITestPackage GetTestPackage(string configName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public interface IRuntimeFrameworkService
/// </summary>
/// <param name="package">A TestPackage</param>
/// <returns>The selected RuntimeFramework</returns>
string SelectRuntimeFramework(TestPackage package);
string SelectRuntimeFramework(ITestPackage package);
}
}
23 changes: 21 additions & 2 deletions src/NUnitEngine/nunit.engine.api/ITestEngine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;
using System.Xml;

namespace NUnit.Engine
Expand Down Expand Up @@ -46,12 +47,30 @@ public interface ITestEngine : IDisposable
/// </summary>
void Initialize();

/// <summary>
/// Construct a top-level ITestPackage that wraps one or more
/// test files, contained as subpackages.
/// </summary>
/// <remarks>
/// Semantically equivalent to the array method.
/// </remarks>
ITestPackage CreatePackage(IList<string> testFiles);

/// <summary>
/// Construct a top-level ITestPackage that wraps one or more
/// test files, contained as subpackages.
/// </summary>
/// <remarks>
/// Semantically equivalent to the IList method.
/// </remarks>
ITestPackage CreatePackage(params string[] testFiles);

/// <summary>
/// Returns a test runner instance for use by clients in discovering,
/// exploring and executing tests.
/// </summary>
/// <param name="package">The TestPackage for which the runner is intended.</param>
/// <param name="package">The ITestPackage for which the runner is intended.</param>
/// <returns>An ITestRunner.</returns>
ITestRunner GetRunner(TestPackage package);
ITestRunner GetRunner(ITestPackage package);
}
}
91 changes: 91 additions & 0 deletions src/NUnitEngine/nunit.engine.api/ITestPackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt


using System.Collections.Generic;

namespace NUnit.Engine
{
/// <summary>
/// ITestPackage holds information about a set of test files to
/// be loaded by a TestRunner. Each ITestPackage represents
/// tests for one or more test files.
///
/// Upon construction, a package is given an ID (string), which
/// remains unchanged for the lifetime of the ITestPackage instance.
/// The package ID is passed to the test framework for use in generating
/// test IDs.
///
/// A runner that reloads test assemblies and wants the ids to remain stable
/// should avoid creating a new package but should instead use the original
/// package, changing settings as needed. This gives the best chance for the
/// tests in the reloaded assembly to match those originally loaded.
/// </summary>
public interface ITestPackage
{
/// <summary>
/// Every test package gets a unique ID used to prefix test IDs within that package.
/// </summary>
/// <remarks>
/// The generated ID is only unique for packages created within the same application domain.
/// For that reason, NUnit pre-creates all test packages that will be needed.
/// </remarks>
string ID { get; }

/// <summary>
/// Gets the name of the package
/// </summary>
string Name { get; }

/// <summary>
/// Gets the path to the file containing tests. It may be
/// an assembly or a recognized project type.
/// </summary>
string FullName { get; }

/// <summary>
/// Gets the list of SubPackages contained in this package
/// </summary>
IList<ITestPackage> SubPackages { get; }

/// <summary>
/// Gets the settings dictionary for this package.
/// </summary>
IDictionary<string, object> Settings { get; }

/// <summary>
/// Add a subpackage to the package.
/// </summary>
/// <param name="subPackage">The subpackage to be added</param>
void AddSubPackage(ITestPackage subPackage);

/// <summary>
/// Add a subpackage to the package, specifying its name. This is
/// the only way to add a named subpackage to the top-level package.
/// </summary>
/// <param name="packageName">The name of the subpackage to be added</param>
ITestPackage AddSubPackage(string packageName);

/// <summary>
/// Add a setting to a package and all of its subpackages.
/// </summary>
/// <param name="name">The name of the setting</param>
/// <param name="value">The value of the setting</param>
/// <remarks>
/// Once a package is created, subpackages may have been created
/// as well. If you add a setting directly to the Settings dictionary
/// of the package, the subpackages are not updated. This method is
/// used when the settings are intended to be reflected to all the
/// subpackages under the package.
/// </remarks>
void AddSetting(string name, object value);

/// <summary>
/// Return the value of a setting or a default, which
/// is specified by the caller.
/// </summary>
/// <param name="name">The name of the setting</param>
/// <param name="defaultSetting">The default value</param>
/// <returns></returns>
T GetSetting<T>(string name, T defaultSetting);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void Stop()
Transport.Stop();
}

public override ITestEngineRunner CreateRunner(TestPackage package)
public override ITestEngineRunner CreateRunner(ITestPackage package)
{
return Services.GetService<ITestRunnerFactory>().MakeTestRunner(package);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine.core/Agents/TestAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TestAgent(Guid agentId, IServiceLocator services)
/// <summary>
/// Creates a test runner
/// </summary>
public abstract ITestEngineRunner CreateRunner(TestPackage package);
public abstract ITestEngineRunner CreateRunner(ITestPackage package);

public bool WaitForStop(int timeout)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace NUnit.Engine.Communication.Transports
public interface ITestAgentTransport : ITransport
{
TestAgent Agent { get; }
ITestEngineRunner CreateRunner(TestPackage package);
ITestEngineRunner CreateRunner(ITestPackage package);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void Stop()
});
}

public ITestEngineRunner CreateRunner(TestPackage package)
public ITestEngineRunner CreateRunner(ITestPackage package)
{
_runner = Agent.CreateRunner(package);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Stop()
Agent.StopSignal.Set();
}

public ITestEngineRunner CreateRunner(TestPackage package)
public ITestEngineRunner CreateRunner(ITestPackage package)
{
return Agent.CreateRunner(package);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine.core/ITestAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public interface ITestAgent
/// <summary>
/// Creates a test runner
/// </summary>
ITestEngineRunner CreateRunner(TestPackage package);
ITestEngineRunner CreateRunner(ITestPackage package);
}
}
4 changes: 2 additions & 2 deletions src/NUnitEngine/nunit.engine.core/ITestRunnerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ITestRunnerFactory
/// </summary>
/// <param name="package">The test package to be loaded by the runner</param>
/// <returns>A TestRunner</returns>
ITestEngineRunner MakeTestRunner(TestPackage package);
ITestEngineRunner MakeTestRunner(ITestPackage package);

/// <summary>
/// Return true if the provided runner is suitable for reuse in loading
Expand All @@ -24,6 +24,6 @@ public interface ITestRunnerFactory
/// <param name="runner">An ITestRunner to possibly be used.</param>
/// <param name="package">The TestPackage to be loaded.</param>
/// <returns>True if the runner may be reused for the provided package.</returns>
bool CanReuse(ITestEngineRunner runner, TestPackage package);
bool CanReuse(ITestEngineRunner runner, ITestPackage package);
}
}
Loading