Permalink
Browse files

Part 2b: a better approach without requiring to fork NUnit

  • Loading branch information...
Sebazzz committed Sep 3, 2016
1 parent 1ff501a commit 04837ac892a1a8e46cc13ee64f2937deaa9b24c2
Submodule nunit updated 276 files
@@ -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
@@ -37,4 +35,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly:TestAssemblyBuilder(typeof(OrderedTestAssemblyBuilder))]
[assembly: EnabledTestFixtureOrdering]
@@ -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();
}
}
}
@@ -27,8 +27,6 @@ where typeof(ITestCollection).IsAssignableFrom(type)
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);
@@ -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();
@@ -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" />

This file was deleted.

Oops, something went wrong.
@@ -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>();
@@ -50,8 +57,12 @@ public sealed class OrderedTreeBuilder {
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);
}
}
@@ -66,8 +77,9 @@ public sealed class OrderedTreeBuilder {
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);
@@ -85,12 +97,14 @@ public sealed class OrderedTreeBuilder {
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);
}
}
}
@@ -105,7 +119,9 @@ public sealed class OrderedTreeBuilder {
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);
}
}
@@ -141,7 +157,6 @@ public sealed class OrderedTreeBuilder {
}
public class OrderedTestSuite : TestSuite {
public OrderedTestSuite(Test original) : base(original.TypeInfo) {
this.MaintainTestOrder = true;
}
@@ -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);

0 comments on commit 04837ac

Please sign in to comment.