Skip to content

Commit

Permalink
Merge pull request #1460 from nunit/issue-1457
Browse files Browse the repository at this point in the history
Run all unit tests under NUnitLite
  • Loading branch information
CharliePoole authored Aug 13, 2024
2 parents f8900c6 + a25dcad commit 12f638b
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 56 deletions.
22 changes: 1 addition & 21 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ BuildSettings.Initialize(
githubRepository: "nunit-console",
solutionFile: "NUnitConsole.sln",
exemptFiles: new[] { "Options.cs", "ProcessUtils.cs", "ProcessUtilsTests.cs" },
unitTests: "**/*.tests.exe|**/nunit3-console.tests.dll",
unitTestRunner: new CustomTestRunner());
unitTests: "**/*.tests.exe");

//////////////////////////////////////////////////////////////////////
// PACKAGE TEST LISTS
Expand Down Expand Up @@ -349,25 +348,6 @@ BuildSettings.Packages.AddRange(new PackageDefinition[] {
// TEST RUNNERS
//////////////////////////////////////////////////////////////////////

// Custom unit test runner to run console vs engine tests differently
// TODO: Use NUnitLite for all tests?
public class CustomTestRunner : TestRunner, IUnitTestRunner
{
public int RunUnitTest(FilePath testPath)
{
// Run console tests under the just-built console
if (testPath.ToString().Contains("nunit3-console.tests.dll"))
{
return BuildSettings.Context.StartProcess(
BuildSettings.OutputDirectory + "net462/nunit3-console.exe",
$"\"{testPath}\" {BuildSettings.UnitTestArguments}");
}

// All other tests use NUnitLite
return new NUnitLiteRunner().RunUnitTest(testPath);
}
}

// Use the console runner we just built to run package tests
public class ConsoleRunnerSelfTester : TestRunner, IPackageTestRunner
{
Expand Down
22 changes: 11 additions & 11 deletions src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System;
using System.IO;
using System.Reflection;
using NUnit.Common;
using System.Collections.Generic;
using NUnit.Framework;

using NUnit.Options;
using NUnit.Common;
using NUnit.ConsoleRunner.Options;
using NUnit.Framework;

namespace NUnit.ConsoleRunner.Tests
{
Expand Down Expand Up @@ -403,7 +403,7 @@ public void ResultOptionWithFilePath()
Assert.That(options.InputFiles.Count, Is.EqualTo(1), "assembly should be set");
Assert.That(options.InputFiles[0], Is.EqualTo("tests.dll"));

OutputSpecification spec = options.ResultOutputSpecifications[0];
var spec = options.ResultOutputSpecifications[0];
Assert.That(spec.OutputPath, Is.EqualTo("results.xml"));
Assert.That(spec.Format, Is.EqualTo("nunit3"));
Assert.Null(spec.Transform);
Expand All @@ -417,7 +417,7 @@ public void ResultOptionWithFilePathAndFormat()
Assert.That(options.InputFiles.Count, Is.EqualTo(1), "assembly should be set");
Assert.That(options.InputFiles[0], Is.EqualTo("tests.dll"));

OutputSpecification spec = options.ResultOutputSpecifications[0];
var spec = options.ResultOutputSpecifications[0];
Assert.That(spec.OutputPath, Is.EqualTo("results.xml"));
Assert.That(spec.Format, Is.EqualTo("nunit2"));
Assert.Null(spec.Transform);
Expand All @@ -437,7 +437,7 @@ public void ResultOptionWithFilePathAndTransform()
Assert.That(options.InputFiles.Count, Is.EqualTo(1), "assembly should be set");
Assert.That(options.InputFiles[0], Is.EqualTo("tests.dll"));

OutputSpecification spec = options.ResultOutputSpecifications[0];
var spec = options.ResultOutputSpecifications[0];
Assert.That(spec.OutputPath, Is.EqualTo("results.xml"));
Assert.That(spec.Format, Is.EqualTo("user"));
var fullFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, transformFile);
Expand Down Expand Up @@ -524,7 +524,7 @@ public void InvalidResultSpecRecordsError()
{
var options = ConsoleMocks.Options("test.dll", "-result:userspecifed.xml;format=nunit2;format=nunit3");
Assert.That(options.ResultOutputSpecifications, Has.Exactly(1).Items
.And.Exactly(1).Property(nameof(OutputSpecification.OutputPath)).EqualTo("TestResult.xml"));
.And.Exactly(1).Property(nameof(Options.OutputSpecification.OutputPath)).EqualTo("TestResult.xml"));
Assert.That(options.ErrorMessages, Has.Exactly(1).Contains("conflicting format options").IgnoreCase);
}

Expand All @@ -538,7 +538,7 @@ public void MissingXsltFileRecordsError()
new VirtualFileSystem(),
"test.dll", $"-result:userspecifed.xml;transform={missingXslt}");
Assert.That(options.ResultOutputSpecifications, Has.Exactly(1).Items
.And.Exactly(1).Property(nameof(OutputSpecification.Transform)).Null);
.And.Exactly(1).Property(nameof(Options.OutputSpecification.Transform)).Null);
Assert.That(options.ErrorMessages, Has.Exactly(1).Contains($"{missingXslt} could not be found").IgnoreCase);
}

Expand All @@ -559,7 +559,7 @@ public void ExploreOptionWithFilePath()
Assert.That(options.InputFiles[0], Is.EqualTo("tests.dll"));
Assert.That(options.Explore, Is.True);

OutputSpecification spec = options.ExploreOutputSpecifications[0];
var spec = options.ExploreOutputSpecifications[0];
Assert.That(spec.OutputPath, Is.EqualTo("results.xml"));
Assert.That(spec.Format, Is.EqualTo("nunit3"));
Assert.Null(spec.Transform);
Expand All @@ -574,7 +574,7 @@ public void ExploreOptionWithFilePathAndFormat()
Assert.That(options.InputFiles[0], Is.EqualTo("tests.dll"));
Assert.That(options.Explore, Is.True);

OutputSpecification spec = options.ExploreOutputSpecifications[0];
var spec = options.ExploreOutputSpecifications[0];
Assert.That(spec.OutputPath, Is.EqualTo("results.xml"));
Assert.That(spec.Format, Is.EqualTo("cases"));
Assert.Null(spec.Transform);
Expand All @@ -594,7 +594,7 @@ public void ExploreOptionWithFilePathAndTransform()
Assert.That(options.InputFiles[0], Is.EqualTo("tests.dll"));
Assert.That(options.Explore, Is.True);

OutputSpecification spec = options.ExploreOutputSpecifications[0];
var spec = options.ExploreOutputSpecifications[0];
Assert.That(spec.OutputPath, Is.EqualTo("results.xml"));
Assert.That(spec.Format, Is.EqualTo("user"));
var fullFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, transformFile);
Expand Down
1 change: 1 addition & 0 deletions src/NUnitConsole/nunit3-console.tests/ConsoleMocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using NSubstitute;
using NUnit.Common;
using NUnit.ConsoleRunner.Options;

namespace NUnit.ConsoleRunner.Tests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using NUnit.Common;

using NUnit.ConsoleRunner.Options;
using NUnit.Framework;

namespace NUnit.ConsoleRunner.Tests
Expand Down
26 changes: 14 additions & 12 deletions src/NUnitConsole/nunit3-console.tests/OutputSpecificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO;
using NUnit.Framework;

using Spec = NUnit.ConsoleRunner.Options.OutputSpecification;

namespace NUnit.Common.Tests
{
public class OutputSpecificationTests
Expand All @@ -11,7 +13,7 @@ public class OutputSpecificationTests
public void SpecMayNotBeNull()
{
Assert.That(
() => new OutputSpecification(null, null),
() => new Spec(null, null),
Throws.TypeOf<ArgumentNullException>());
}

Expand All @@ -20,22 +22,22 @@ public void SpecMayNotBeNull()
public void SpecOptionMustContainEqualSign()
{
Assert.That(
() => new OutputSpecification("MyFile.xml;transform.xslt", null),
() => new Spec("MyFile.xml;transform.xslt", null),
Throws.TypeOf<ArgumentException>());
}

[Test]
public void SpecOptionMustContainJustOneEqualSign()
{
Assert.That(
() => new OutputSpecification("MyFile.xml;transform=xslt=transform.xslt", null),
() => new Spec("MyFile.xml;transform=xslt=transform.xslt", null),
Throws.TypeOf<ArgumentException>());
}

[Test]
public void FileNameOnly()
{
var spec = new OutputSpecification("MyFile.xml", null);
var spec = new Spec("MyFile.xml", null);
Assert.That(spec.OutputPath, Is.EqualTo("MyFile.xml"));
Assert.That(spec.Format, Is.EqualTo("nunit3"));
Assert.Null(spec.Transform);
Expand All @@ -44,7 +46,7 @@ public void FileNameOnly()
[Test]
public void FileNamePlusFormat()
{
var spec = new OutputSpecification("MyFile.xml;format=nunit2", null);
var spec = new Spec("MyFile.xml;format=nunit2", null);
Assert.That(spec.OutputPath, Is.EqualTo("MyFile.xml"));
Assert.That(spec.Format, Is.EqualTo("nunit2"));
Assert.Null(spec.Transform);
Expand All @@ -54,7 +56,7 @@ public void FileNamePlusFormat()
public void FileNamePlusTransform()
{
const string fileName = "transform.xslt";
var spec = new OutputSpecification($"MyFile.xml;transform={fileName}", null);
var spec = new Spec($"MyFile.xml;transform={fileName}", null);
Assert.That(spec.OutputPath, Is.EqualTo("MyFile.xml"));
Assert.That(spec.Format, Is.EqualTo("user"));
Assert.That(spec.Transform, Is.EqualTo(fileName));
Expand All @@ -64,7 +66,7 @@ public void FileNamePlusTransform()
public void UserFormatMayBeIndicatedExplicitlyAfterTransform()
{
const string fileName = "transform.xslt";
var spec = new OutputSpecification($"MyFile.xml;transform={fileName};format=user", null);
var spec = new Spec($"MyFile.xml;transform={fileName};format=user", null);
Assert.That(spec.OutputPath, Is.EqualTo("MyFile.xml"));
Assert.That(spec.Format, Is.EqualTo("user"));
Assert.That(spec.Transform, Is.EqualTo(fileName));
Expand All @@ -74,7 +76,7 @@ public void UserFormatMayBeIndicatedExplicitlyAfterTransform()
public void UserFormatMayBeIndicatedExplicitlyBeforeTransform()
{
const string fileName = "transform.xslt";
var spec = new OutputSpecification($"MyFile.xml;format=user;transform={fileName}", null);
var spec = new Spec($"MyFile.xml;format=user;transform={fileName}", null);
Assert.That(spec.OutputPath, Is.EqualTo("MyFile.xml"));
Assert.That(spec.Format, Is.EqualTo("user"));
Assert.That(spec.Transform, Is.EqualTo(fileName));
Expand All @@ -84,23 +86,23 @@ public void UserFormatMayBeIndicatedExplicitlyBeforeTransform()
public void MultipleFormatSpecifiersNotAllowed()
{
Assert.That(
() => new OutputSpecification("MyFile.xml;format=nunit2;format=nunit3", null),
() => new Spec("MyFile.xml;format=nunit2;format=nunit3", null),
Throws.TypeOf<ArgumentException>());
}

[Test]
public void MultipleTransformSpecifiersNotAllowed()
{
Assert.That(
() => new OutputSpecification("MyFile.xml;transform=transform1.xslt;transform=transform2.xslt", null),
() => new Spec("MyFile.xml;transform=transform1.xslt;transform=transform2.xslt", null),
Throws.TypeOf<ArgumentException>());
}

[Test]
public void TransformWithNonUserFormatNotAllowed()
{
Assert.That(
() => new OutputSpecification("MyFile.xml;format=nunit2;transform=transform.xslt", null),
() => new Spec("MyFile.xml;format=nunit2;transform=transform.xslt", null),
Throws.TypeOf<ArgumentException>());
}

Expand All @@ -112,7 +114,7 @@ public void TransformWithNonUserFormatNotAllowed()
public void TransformFolderIsUsedToSpecifyTransform(string transformFolder)
{
const string fileName = "transform.xslt";
var spec = new OutputSpecification($"MyFile.xml;transform=transform.xslt", transformFolder);
var spec = new Spec($"MyFile.xml;transform=transform.xslt", transformFolder);
var expectedTransform = Path.Combine(transformFolder ?? "", fileName);
Assert.That(spec.Transform, Is.EqualTo(expectedTransform));
}
Expand Down
18 changes: 18 additions & 0 deletions src/NUnitConsole/nunit3-console.tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System.Reflection;

namespace NUnit.Engine.Tests
{
class Program
{
static int Main(string[] args)
{
#if NETFRAMEWORK
return new NUnitLite.TextRunner(typeof(Program).Assembly).Execute(args);
#else
return new NUnitLite.TextRunner(typeof(Program).GetTypeInfo().Assembly).Execute(args);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PropertyGroup>
<RootNamespace>NUnit.ConsoleRunner.Tests</RootNamespace>
<TargetFrameworks>net462;net6.0;net8.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<StartupObject>NUnit.Engine.Tests.Program</StartupObject>
<NoWarn>1685</NoWarn>
<DebugType>Full</DebugType>
</PropertyGroup>
Expand All @@ -16,6 +18,7 @@
<ItemGroup>
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnitLite" Version="3.13.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'!='net462'">
Expand All @@ -42,7 +45,9 @@
<ItemGroup>
<ProjectReference Include="..\..\NUnitEngine\mock-assembly\mock-assembly.csproj" />
<ProjectReference Include="..\..\NUnitEngine\nunit.engine\nunit.engine.csproj" />
<ProjectReference Include="..\nunit3-console\nunit3-console.csproj" />
<ProjectReference Include="..\nunit3-console\nunit3-console.csproj">
<Aliases></Aliases>
</ProjectReference>
<ProjectReference Include="..\..\NUnitEngine\nunit.engine.api\nunit.engine.api.csproj" />
</ItemGroup>

Expand Down
7 changes: 4 additions & 3 deletions src/NUnitConsole/nunit3-console/ConsoleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Options;
using NUnit.ConsoleRunner.OptionsUtils;

namespace NUnit.Common
using NUnit.Common;
using NUnit.ConsoleRunner.Options;

namespace NUnit.ConsoleRunner
{
/// <summary>
/// <c>ConsoleOptions</c> encapsulates the option settings for
Expand Down
1 change: 1 addition & 0 deletions src/NUnitConsole/nunit3-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Xml;
using NUnit.Common;
using NUnit.ConsoleRunner.Options;
using NUnit.ConsoleRunner.Utilities;
using NUnit.Engine;
using NUnit.Engine.Extensibility;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

namespace NUnit.Common
namespace NUnit.ConsoleRunner.Options
{
using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

namespace NUnit.Common
namespace NUnit.ConsoleRunner.Options
{
internal interface IDefaultOptionsProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Generic;
using NUnit.Common;

namespace NUnit.ConsoleRunner.OptionsUtils
namespace NUnit.ConsoleRunner.Options
{
internal class OptionParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
using MessageLocalizerConverter = System.Converter<string, string>;
#endif

namespace NUnit.Options
namespace NUnit.ConsoleRunner.Options
{
static class StringCoda
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Text;

namespace NUnit.Common
namespace NUnit.ConsoleRunner.Options
{
/// <summary>
/// OutputSpecification encapsulates a file output path and format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System.Collections.Generic;

namespace NUnit.Common
namespace NUnit.ConsoleRunner.Options
{
/// <summary>
/// TestNameParser is used to parse the arguments to the
Expand Down
3 changes: 2 additions & 1 deletion src/NUnitConsole/nunit3-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
using System.Linq;
using System.Reflection;
using System.Text;

using NUnit.Common;
using NUnit.ConsoleRunner.Options;
using NUnit.Engine;
using NUnit.Options;

namespace NUnit.ConsoleRunner
{
Expand Down

0 comments on commit 12f638b

Please sign in to comment.