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

Allow passing arguments into the test run #1570

Merged
merged 5 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Common/nunit/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public CommandLineOptions(params string[] args)
private List<string> testList = new List<string>();
public IList<string> TestList { get { return testList; } }

public string TestParameters { get; private set; }

public string WhereClause { get; private set; }
public bool WhereClauseSpecified { get { return WhereClause != null; } }

Expand Down Expand Up @@ -296,6 +298,23 @@ protected virtual void ConfigureOptions()
this.Add("where=", "Test selection {EXPRESSION} indicating what tests will be run. See description below.",
v => WhereClause = RequiredValue(v, "--where"));

this.Add("params|p=", "Define a test parameter.",
v =>
{
string parameters = RequiredValue( v, "--params");

foreach (string param in parameters.Split(new[] { ';' }))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on #1571, should we be using semi-colons, or is there an easy workaround to that issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try it out when I get home to my Linux machine. We have had the result option for a long time and nobody has brought up the semicolon before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a problem. I commented on #1571

{
if (!param.Contains("="))
ErrorMessages.Add("Invalid format for test parameter. Use NAME=VALUE.");
}

if (TestParameters == null)
TestParameters = parameters;
else
TestParameters += ";" + parameters;
});

this.Add("timeout=", "Set timeout for each test case in {MILLISECONDS}.",
v => defaultTimeout = RequiredInt(v, "--timeout"));

Expand Down
5 changes: 5 additions & 0 deletions src/Common/nunit/PackageSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ public static class PackageSettings
/// </summary>
public const string DefaultTestNamePattern = "DefaultTestNamePattern";

/// <summary>
/// Parameters to be passed on to the test
/// </summary>
public const string TestParameters = "TestParameters";

#endregion

#region Internal Settings - Used only within the engine
Expand Down
60 changes: 60 additions & 0 deletions src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.IO;
using System.Reflection;
using NUnit.Common;
Expand Down Expand Up @@ -206,6 +207,7 @@ public void CanRecognizeIntOptions(string propertyName, string pattern)
[TestCase("--work")]
[TestCase("--trace")]
[TestCase("--test-name-format")]
[TestCase("--params")]
public void MissingValuesAreReported(string option)
{
ConsoleOptions options = new ConsoleOptions(option + "=");
Expand Down Expand Up @@ -548,6 +550,64 @@ public void ShouldNotFailOnEmptyLine()

#endregion

#region Test Parameters

[Test]
public void SingleTestParameter()
{
var options = new ConsoleOptions("--params=X=5");
Assert.That(options.errorMessages, Is.Empty);
Assert.That(options.TestParameters, Is.EqualTo("X=5"));
}

[Test]
public void TwoTestParametersInOneOption()
{
var options = new ConsoleOptions("--params:X=5;Y=7");
Assert.That(options.errorMessages, Is.Empty);
Assert.That(options.TestParameters, Is.EqualTo("X=5;Y=7"));
}

[Test]
public void TwoTestParametersInSeparateOptions()
{
var options = new ConsoleOptions("-p:X=5", "-p:Y=7");
Assert.That(options.errorMessages, Is.Empty);
Assert.That(options.TestParameters, Is.EqualTo("X=5;Y=7"));
}

[Test]
public void ThreeTestParametersInTwoOptions()
{
var options = new ConsoleOptions("--params:X=5;Y=7", "-p:Z=3");
Assert.That(options.errorMessages, Is.Empty);
Assert.That(options.TestParameters, Is.EqualTo("X=5;Y=7;Z=3"));
}

[Test]
public void ParameterWithoutEqualSignIsInvalid()
{
var options = new ConsoleOptions("--params=X5");
Assert.That(options.ErrorMessages.Count, Is.EqualTo(1));
}

[Test]
public void DisplayTestParameters()
{
if (TestContext.Parameters.Count == 0)
{
Console.WriteLine("No Test Parameters were passed");
return;
}

Console.WriteLine("Test Parameters---");

foreach (var name in TestContext.Parameters.Names)
Console.WriteLine(" Name: {0} Value: {1}", name, TestContext.Parameters[name]);
}

#endregion

#region Helper Methods

private static FieldInfo GetFieldInfo(string fieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void MultipleAssemblies()
[TestCase("--workers=0", "NumberOfTestWorkers", 0)]
[TestCase("--debug", "DebugTests", true)]
[TestCase("--pause", "PauseBeforeRun", true)]
[TestCase("--params:X=5;Y=7", "TestParameters", "X=5;Y=7")]
#if DEBUG
[TestCase("--debug-agent", "DebugAgent", true)]
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/NUnitConsole/nunit3-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ public static TestPackage MakeTestPackage(ConsoleOptions options)
if (options.DefaultTestNamePattern != null)
package.AddSetting(PackageSettings.DefaultTestNamePattern, options.DefaultTestNamePattern);

if (options.TestParameters != null)
package.AddSetting(PackageSettings.TestParameters, options.TestParameters);

return package;
}

Expand Down
18 changes: 18 additions & 0 deletions src/NUnitFramework/framework/Api/DefaultTestAssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ private TestSuite Build(Assembly assembly, string assemblyPath, IDictionary<stri
if (options.ContainsKey(PackageSettings.DefaultTestNamePattern))
TestNameGenerator.DefaultTestNamePattern = options[PackageSettings.DefaultTestNamePattern] as string;

if (options.ContainsKey(PackageSettings.TestParameters))
{
string parameters = options[PackageSettings.TestParameters] as string;
if (!string.IsNullOrEmpty(parameters))
foreach (string param in parameters.Split(new[] { ';' }))
{
int eq = param.IndexOf("=");

if (eq > 0 && eq < param.Length - 1)
{
var name = param.Substring(0, eq);
var val = param.Substring(eq + 1);

TestContext.Parameters.Add(name, val);
}
}
}

IList fixtureNames = null;
if (options.ContainsKey (PackageSettings.LOAD))
fixtureNames = options[PackageSettings.LOAD] as IList;
Expand Down
8 changes: 7 additions & 1 deletion src/NUnitFramework/framework/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using NUnit.Framework.Constraints;
Expand Down Expand Up @@ -88,6 +89,11 @@ public static TextWriter Out
public static readonly TextWriter Progress = new EventListenerTextWriter("Progress", Console.Error);
#endif

/// <summary>
/// TestParameters object holds parameters for the test run, if any are specified
/// </summary>
public static readonly TestParameters Parameters = new TestParameters();

/// <summary>
/// Get a representation of the current test.
/// </summary>
Expand Down Expand Up @@ -458,7 +464,7 @@ public int InconclusiveCount

#endregion
}

#endregion
}
}
94 changes: 94 additions & 0 deletions src/NUnitFramework/framework/TestParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NUnit.Framework
{
/// <summary>
/// TestParameters class holds any named parameters supplied to the test run
/// </summary>
public class TestParameters
{
private readonly Dictionary<string, string> _parameters = new Dictionary<string, string>();

/// <summary>
/// Gets the number of test parameters
/// </summary>
public int Count
{
get { return _parameters.Count; }
}

/// <summary>
/// Gets a collection of the test parameter names
/// </summary>
public ICollection<string> Names
{
get { return _parameters.Keys; }
}

/// <summary>
/// Gets a flag indicating whether a parameter with the specified name exists.N
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <returns>True if it exists, otherwise false</returns>
public bool Exists(string name)
{
return _parameters.ContainsKey(name);
}

/// <summary>
/// Indexer provides access to the internal dictionary
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <returns>Value of the parameter or null if not present</returns>
public string this[string name]
{
get { return Get(name); }
}

/// <summary>
/// Get method is a simple alternative to the indexer
/// </summary>
/// <param name="name">Name of the paramter</param>
/// <returns>Value of the parameter or null if not present</returns>
public string Get(string name)
{
return Exists(name) ? _parameters[name] : null;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very inefficient. Should be:

string value;
_parameters.TryGetValue (name, out value)

return value;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Exists for clarity, but it's basically as if I wrote...

if (_parameters.ContainsKey(name) return _parameters[name];

This is a pattern we use all over NUnit. If memory serves, it's because TryGetValue doesn't (or maybe didn't) exist on some platforms. As far as I have always understood, it's a little bit inefficient, but not enough to make me want to prematurely optimize it, especially at the expense of clarity. The various TryXxx functions always seem very hard to read.

Do you have an estimate of how inefficient it is in a typical use? For that matter, since it's new, do we have any idea what typical use will be? I'm thinking of some small number of users having maybe three or four parameters so maybe that biases my approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using exists and then getting the value requires two completely independent lookups in the dictionary.  Trygetvalue only does a single lookup.  I never use the pattern you used.  As far as I know every platform has trygetvalue for the generic dictionary class.

---Sent from Boxer | http://getboxer.com

On June 17, 2016 at 9:08:04 AM GMT+8, CharliePoole notifications@github.com wrote:In src/NUnitFramework/framework/TestParameters.cs: > + /// Value of the parameter or null if not present > + public string this[string name] > + { > + get { return Get(name); } > + } > + > + ///

  • /// Get method is a simple alternative to the indexer > + ///
  • /// Name of the paramter > + /// Value of the parameter or null if not present > + public string Get(string name) > + { > + return Exists(name) ? _parameters[name] : null; > + } > + I used Exists for clarity, but it's basically as if I wrote... if (_parameters.ContainsKey(name) return _parameters[name]; This is a pattern we use all over NUnit. If memory serves, it's because TryGetValue doesn't (or maybe didn't) exist on some platforms. As far as I have always understood, it's a little bit inefficient, but not enough to make me want to prematurely optimize it, especially at the expense of clarity. The various TryXxx functions always seem very hard to read. Do you have an estimate of how inefficient it is in a typical use? For that matter, since it's new, do we have any idea what typical use will be? I'm thinking of some small number of users having maybe three or four parameters so maybe that biases my approach. —You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or mute the thread.

/// <summary>
/// Get the value of a parameter or a default string
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <param name="defaultValue">Default value of the parameter</param>
/// <returns>Value of the parameter or default value if not present</returns>
public string Get(string name, string defaultValue)
{
return Get(name) ?? defaultValue;
}

/// <summary>
/// Get the value of a parameter or return a default
/// </summary>
/// <typeparam name="T">The return Type</typeparam>
/// <param name="name">Name of the parameter</param>
/// <param name="defaultValue">Default value of the parameter</param>
/// <returns>Value of the parameter or default value if not present</returns>
public T Get<T>(string name, T defaultValue)
{
string val = Get(name);
return val != null ? (T)Convert.ChangeType(val, typeof(T), null) : defaultValue;
}

/// <summary>
/// Adds a parameter to the list
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <param name="value">Value of the parameter</param>
internal void Add(string name, string value)
{
_parameters[name] = value;
}
}
}
1 change: 1 addition & 0 deletions src/NUnitFramework/framework/nunit.framework-2.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
<Compile Include="StringAssert.cs" />
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
<Compile Include="Internal\Results\TestCaseResult.cs" />
<Compile Include="Internal\Results\TestSuiteResult.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NUnitFramework/framework/nunit.framework-3.5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@
<Compile Include="StringAssert.cs" />
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
<Compile Include="Internal\Results\TestCaseResult.cs" />
<Compile Include="Internal\Results\TestSuiteResult.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NUnitFramework/framework/nunit.framework-4.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestFixtureData.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
<Compile Include="Internal\Results\TestCaseResult.cs" />
<Compile Include="Internal\Results\TestSuiteResult.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NUnitFramework/framework/nunit.framework-4.5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestFixtureData.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
<Compile Include="Internal\Results\TestCaseResult.cs" />
<Compile Include="Internal\Results\TestSuiteResult.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestFixtureData.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestFixtureData.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/NUnitFramework/framework/nunit.framework-sl-5.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@
<Compile Include="TestCaseData.cs" />
<Compile Include="TestContext.cs" />
<Compile Include="TestFixtureData.cs" />
<Compile Include="TestParameters.cs" />
<Compile Include="Throws.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
using NUnit.TestUtilities;

#if NET_4_0 || NET_4_5 || PORTABLE
using System;
using System.Threading.Tasks;
#endif

Expand Down