Skip to content

Commit

Permalink
Part 2b: a better approach without requiring to fork NUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebazzz committed Sep 3, 2016
1 parent 1ff501a commit 04837ac
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 77 deletions.
2 changes: 1 addition & 1 deletion extern/nunit
Submodule nunit updated 276 files
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NUnit.Extensions.TestOrdering;
using NUnit.Framework.Attributes;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
Expand Down Expand Up @@ -37,4 +35,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly:TestAssemblyBuilder(typeof(OrderedTestAssemblyBuilder))]
[assembly: EnabledTestFixtureOrdering]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ******************************************************************************
// © 2016 Ernst & Young - www.ey.com | www.beco.nl
//
// Author : Ernst & Young - Cleantech and Sustainability
// File: : OrderedTestAssemblyAttribute.cs
// Project : BankAccountApp.NUnitTests.Integration
// ******************************************************************************

namespace NUnit.Extensions.TestOrdering {
using System;

using Framework.Interfaces;
using Framework.Internal;

/// <summary>
/// To be applied to assemblies which require tests to be ordered
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class EnabledTestFixtureOrderingAttribute : Attribute, IApplyToTest {
public void ApplyToTest(Test test) {
if (test == null) throw new ArgumentNullException(nameof(test));

TestAssembly testAssembly = test as TestAssembly;

if (testAssembly == null) {
throw new ArgumentException($"Excepted argument \"{nameof(test)}\" to be of type {typeof(TestAssembly)} but was type {test.GetType()} instead", nameof(testAssembly));
}

OrderTestAssemblyTests(testAssembly);
}

private static void OrderTestAssemblyTests(TestAssembly testAssembly) {
OrderedTreeBuilder treeBuilder = new OrderedTreeBuilder(testAssembly);
treeBuilder.Add(testAssembly);
treeBuilder.Complete();
}
}
}
3 changes: 0 additions & 3 deletions src/NUnit.Extensions.TestOrdering/FixtureOrderingCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ private static TestCollectionInfo[] CreateHierarchy(IEnumerable<TestCollectionIn
foreach (TestCollectionInfo testCollection in returnValue) {
try {
testCollection.Parent = returnValue.SingleOrDefault(x => x.IsParentOf(testCollection));

testCollection.Parent?.Children.Add(testCollection);
}
catch (InvalidOperationException ex) {
throw new InvalidOperationException($"Unable to validate hierarchy: test collection {testCollection.TypeName} has multiple parents", ex);
Expand All @@ -48,7 +46,6 @@ public sealed class TestCollectionInfo : IEquatable<TestCollectionInfo> {

public bool ContinueOnFailure { get; }
public TestCollectionInfo Parent { get; set; }
public ICollection<TestCollectionInfo> Children { get; } = new List<TestCollectionInfo>();

public TestCollectionInfo(ITestCollection testCollection) {
this.ChildTypes = testCollection.GetTestFixtures().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<Compile Include="DependencySorter.cs" />
<Compile Include="FixtureOrderingCache.cs" />
<Compile Include="ITestCollection.cs" />
<Compile Include="OrderedTestAssemblyBuilder.cs" />
<Compile Include="EnabledTestFixtureOrderingAttribute.cs" />
<Compile Include="OrderedTreeBuilder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestMethodDependencyAttribute.cs" />
Expand Down
56 changes: 0 additions & 56 deletions src/NUnit.Extensions.TestOrdering/OrderedTestAssemblyBuilder.cs

This file was deleted.

42 changes: 29 additions & 13 deletions src/NUnit.Extensions.TestOrdering/OrderedTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ public sealed class OrderedTreeBuilder {
private readonly Dictionary<Type, TestCollectionWrapper> _testSuitesByType;
private readonly Dictionary<TestCollectionInfo, TestCollectionWrapper> _testSuitesByTestCollection;

public TestSuite Root { get; }
public TestAssembly Root { get; }

public OrderedTreeBuilder(Assembly assembly) {
this._orderedTests = new OrderedTestSuite("Ordered tests");
this._unorderedTests = new TestSuite("Other tests");

this.Root = new TestSuite(assembly.FullName);
public void Complete() {
this.Root.Add(this._unorderedTests);
this.Root.Add(this._orderedTests);

this._fixtureOrderingCache = new FixtureOrderingCache(assembly);
this._unorderedTests.Properties.Set(PropertyNames.Order, 0);
this._orderedTests.Properties.Set(PropertyNames.Order, 1);
}

public OrderedTreeBuilder(TestAssembly testAssembly) {
this._unorderedTests = new TestSuite("Unordered");
this._orderedTests = new OrderedTestSuite("Ordered");

this.Root = testAssembly;

this._fixtureOrderingCache = new FixtureOrderingCache(testAssembly.Assembly);
this._testSuitesByType = new Dictionary<Type, TestCollectionWrapper>();
this._testSuitesByTestCollection = new Dictionary<TestCollectionInfo, TestCollectionWrapper>();

Expand All @@ -50,8 +57,12 @@ public void Add(TestSuite suite) {
this.InitializeHierarchy();

// The given test suite is not all that interesting, but the children are
foreach (Test childTest in suite.Tests.Cast<Test>()) {
this._unorderedTests.Add(childTest);
for (int i = suite.Tests.Count - 1; i >= 0; i--) {
ITest childTest = suite.Tests[i];

this._unorderedTests.Add((Test) childTest);

suite.Tests.RemoveAt(i);
}
}

Expand All @@ -66,8 +77,9 @@ private void InitializeHierarchy() {
throw new InvalidOperationException($"Unable to find parent of {childCollection.TypeName} (expected: {childCollection.Parent.TypeName}");
}

if (!parentWrapper.TestSuite.Tests.Contains(testCollectionWrapper.TestSuite))
if (!parentWrapper.TestSuite.Tests.Contains(testCollectionWrapper.TestSuite)) {
parentWrapper.TestSuite.Add(testCollectionWrapper.TestSuite);
}
}
else if (!this._orderedTests.Tests.Contains(testCollectionWrapper.TestSuite)) {
this._orderedTests.Tests.Add(testCollectionWrapper.TestSuite);
Expand All @@ -85,12 +97,14 @@ private void SortTestCollections() {
Type childType = wrapper.TestCollection.ChildTypes[i];

Test childTypeTestSuite = this._testSuitesByType[childType].TestSuite;
int childTypeIndex = testSuite.Tests.IndexOf(childTypeTestSuite);
int childTypeTestSuiteIndex = testSuite.Tests.IndexOf(childTypeTestSuite);

// Swap
Test testAtTargetIndex = (Test) testSuite.Tests[i];
testSuite.Tests[i] = childTypeTestSuite;
testSuite.Tests[childTypeIndex] = testAtTargetIndex;
testSuite.Tests[childTypeTestSuiteIndex] = testAtTargetIndex;

childTypeTestSuite.Properties.Set(PropertyNames.Order, i);
}
}
}
Expand All @@ -105,7 +119,9 @@ private void AddFromHierarchy(Test parent, Test test) {
return;
}

foreach (Test childTest in test.Tests.Cast<Test>().ToList()) {
for (int i = test.Tests.Count - 1; i >= 0; i--) {
Test childTest = (Test) test.Tests[i];

this.AddFromHierarchy(test, childTest);
}
}
Expand Down Expand Up @@ -141,7 +157,6 @@ private static TestCollectionTestSuite GetTestSuite(Test test, TestCollectionWra
}

public class OrderedTestSuite : TestSuite {

public OrderedTestSuite(Test original) : base(original.TypeInfo) {
this.MaintainTestOrder = true;
}
Expand All @@ -154,6 +169,7 @@ public class TestCollectionTestSuite : OrderedTestSuite {

public TestCollectionTestSuite(Test original, TestCollectionInfo testCollection) : base(original) {
this.TestCollection = testCollection;
this.MaintainTestOrder = true;

foreach (ITest test in original.Tests) {
this.Add((Test) test);
Expand Down

0 comments on commit 04837ac

Please sign in to comment.