Permalink
Browse files

Initial implementation of custom assembly builder

  • Loading branch information...
Sebazzz committed Jun 7, 2016
1 parent 8fa4e88 commit 337f2412da1eb01932cb6f695feac7861884578c
@@ -70,7 +70,7 @@ public class FrameworkController : LongLivedMarshalByRefObject
/// <param name="settings">A Dictionary of settings to use in loading and running the tests</param>
public FrameworkController(string assemblyNameOrPath, string idPrefix, IDictionary settings)
{
this.Builder = new DefaultTestAssemblyBuilder();
this.Builder = new RuntimeAssemblyBuilder();
this.Runner = new NUnitTestAssemblyRunner(this.Builder);
Test.IdPrefix = idPrefix;
@@ -0,0 +1,107 @@
namespace NUnit.Framework.Api {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Attributes;
using Interfaces;
using Internal;
/// <summary>
/// Represents an <see cref="ITestAssemblyBuilder"/> which will instantiate the correct <see cref="ITestAssemblyBuilder"/> at runtime
/// </summary>
public sealed class RuntimeAssemblyBuilder : ITestAssemblyBuilder {
static Logger log = InternalTrace.GetLogger(typeof(RuntimeAssemblyBuilder));
/// <summary>
/// Build a suite of tests from a provided assembly
/// </summary>
/// <param name="assembly">The assembly from which tests are to be built</param>
/// <param name="options">A dictionary of options to use in building the suite</param>
/// <returns>A TestSuite containing the tests found in the assembly</returns>
public ITest Build(Assembly assembly, IDictionary<string, object> options) {
#if PORTABLE
log.Debug("Loading {0}", assembly.FullName);
#else
log.Debug("Loading {0} in AppDomain {1}", assembly.FullName, AppDomain.CurrentDomain.FriendlyName);
#endif
#if SILVERLIGHT
string assemblyPath = AssemblyHelper.GetAssemblyName(assembly).Name;
#elif PORTABLE
string assemblyPath = AssemblyHelper.GetAssemblyName(assembly).FullName;
#else
string assemblyPath = AssemblyHelper.GetAssemblyPath(assembly);
#endif
return BuildUsingInnerBuilder(assembly, assemblyPath, options);
}
/// <summary>
/// Build a suite of tests given the filename of an assembly
/// </summary>
/// <param name="assemblyName">The filename of the assembly from which tests are to be built</param>
/// <param name="options">A dictionary of options to use in building the suite</param>
/// <returns>A TestSuite containing the tests found in the assembly</returns>
public ITest Build(string assemblyName, IDictionary<string, object> options) {
#if PORTABLE
log.Debug("Loading {0}", assemblyName);
#else
log.Debug("Loading {0} in AppDomain {1}", assemblyName, AppDomain.CurrentDomain.FriendlyName);
#endif
ITest test = null;
try {
var assembly = AssemblyHelper.Load(assemblyName);
test = BuildUsingInnerBuilder(assembly, assemblyName, options);
}
catch (Exception ex) {
var testAssembly = new TestAssembly(assemblyName);
testAssembly.RunState = RunState.NotRunnable;
testAssembly.Properties.Set(PropertyNames.SkipReason, ex.Message);
test = testAssembly;
}
return test;
}
private static ITest BuildUsingInnerBuilder(Assembly assembly, string assemblyName, IDictionary<string, object> options) {
ITestAssemblyBuilder testAssemblyBuilder;
try {
testAssemblyBuilder = ConstructTestAssemblyBuilder(assembly);
}
catch (Exception ex) {
var testAssembly = new TestAssembly(assembly, assemblyName);
testAssembly.RunState = RunState.NotRunnable;
testAssembly.Properties.Set(PropertyNames.SkipReason, ex.Message);
return testAssembly;
}
return testAssemblyBuilder.Build(assembly, options);
}
private static ITestAssemblyBuilder ConstructTestAssemblyBuilder(Assembly assembly) {
log.Debug("Looking up ITestAssemblyBuilder for assembly");
#if PORTABLE
var attributes = assembly.GetCustomAttributes<TestAssemblyBuilderAttribute>().ToArray();
#else
var attributes = assembly.GetCustomAttributes(typeof(TestAssemblyBuilderAttribute), false /*unused*/);
#endif
TestAssemblyBuilderAttribute testAssemblyBuilderAttribute;
if (attributes.Length == 1 && (testAssemblyBuilderAttribute = attributes[0] as TestAssemblyBuilderAttribute) != null) {
log.Debug("Constructing ITestAssemblyBuilder {0}", testAssemblyBuilderAttribute.AssemblyBuilderType);
return (ITestAssemblyBuilder) Reflect.Construct(testAssemblyBuilderAttribute.AssemblyBuilderType);
}
// Fallback to default implementation
return new DefaultTestAssemblyBuilder();
}
}
}
@@ -0,0 +1,22 @@
namespace NUnit.Framework.Attributes {
using System;
/// <summary>
/// When declared on an assembly, specified which type should be used for discovery of tests within the assembly
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class TestAssemblyBuilderAttribute : Attribute {
/// <summary>
/// Gets the type that is used for discovery of tests in the assembly
/// </summary>
public Type AssemblyBuilderType { get; }
/// <summary>
/// Constructs a TestAssemblyBuilderAttribute
/// </summary>
/// <param name="assemblyBuilderType"></param>
public TestAssemblyBuilderAttribute(Type assemblyBuilderType) {
this.AssemblyBuilderType = assemblyBuilderType;
}
}
}
@@ -108,10 +108,12 @@
<Compile Include="ActionTargets.cs" />
<Compile Include="Api\FrameworkController.cs" />
<Compile Include="Api\NUnitTestAssemblyRunner.cs" />
<Compile Include="Api\RuntimeAssemblyBuilder.cs" />
<Compile Include="Attributes\ApartmentAttribute.cs" />
<Compile Include="Attributes\AuthorAttribute.cs" />
<Compile Include="Attributes\CombiningStrategyAttribute.cs" />
<Compile Include="Attributes\SingleThreadedAttribute.cs" />
<Compile Include="Attributes\TestAssemblyBuilderAttribute.cs" />
<Compile Include="Attributes\TestAssemblyDirectoryResolveAttribute.cs" />
<Compile Include="Attributes\OrderAttribute.cs" />
<Compile Include="Attributes\RetryAttribute.cs" />
@@ -107,10 +107,12 @@
<Compile Include="ActionTargets.cs" />
<Compile Include="Api\FrameworkController.cs" />
<Compile Include="Api\NUnitTestAssemblyRunner.cs" />
<Compile Include="Api\RuntimeAssemblyBuilder.cs" />
<Compile Include="Attributes\ApartmentAttribute.cs" />
<Compile Include="Attributes\AuthorAttribute.cs" />
<Compile Include="Attributes\CombiningStrategyAttribute.cs" />
<Compile Include="Attributes\SingleThreadedAttribute.cs" />
<Compile Include="Attributes\TestAssemblyBuilderAttribute.cs" />
<Compile Include="Attributes\TestAssemblyDirectoryResolveAttribute.cs" />
<Compile Include="Attributes\OrderAttribute.cs" />
<Compile Include="Attributes\RetryAttribute.cs" />
@@ -109,6 +109,7 @@
<Compile Include="Api\ITestAssemblyBuilder.cs" />
<Compile Include="Api\ITestAssemblyRunner.cs" />
<Compile Include="Api\NUnitTestAssemblyRunner.cs" />
<Compile Include="Api\RuntimeAssemblyBuilder.cs" />
<Compile Include="Assert.Comparisons.cs">
<DependentUpon>Assert.cs</DependentUpon>
</Compile>
@@ -139,6 +140,7 @@
<Compile Include="Attributes\CombinatorialAttribute.cs" />
<Compile Include="Attributes\CombiningStrategyAttribute.cs" />
<Compile Include="Attributes\SingleThreadedAttribute.cs" />
<Compile Include="Attributes\TestAssemblyBuilderAttribute.cs" />
<Compile Include="Attributes\TestAssemblyDirectoryResolveAttribute.cs" />
<Compile Include="Attributes\LevelOfParallelismAttribute.cs" />
<Compile Include="Attributes\OneTimeSetUpAttribute.cs" />
@@ -113,6 +113,7 @@
<Compile Include="Api\ITestAssemblyBuilder.cs" />
<Compile Include="Api\ITestAssemblyRunner.cs" />
<Compile Include="Api\NUnitTestAssemblyRunner.cs" />
<Compile Include="Api\RuntimeAssemblyBuilder.cs" />
<Compile Include="Assert.Comparisons.cs">
<DependentUpon>Assert.cs</DependentUpon>
</Compile>
@@ -152,6 +153,7 @@
<Compile Include="Attributes\IgnoreAttribute.cs" />
<Compile Include="Attributes\IncludeExcludeAttribute.cs" />
<Compile Include="Attributes\SingleThreadedAttribute.cs" />
<Compile Include="Attributes\TestAssemblyBuilderAttribute.cs" />
<Compile Include="Attributes\TestAssemblyDirectoryResolveAttribute.cs" />
<Compile Include="Attributes\LevelOfParallelismAttribute.cs" />
<Compile Include="Attributes\MaxTimeAttribute.cs" />
@@ -109,6 +109,7 @@
<Compile Include="Api\ITestAssemblyBuilder.cs" />
<Compile Include="Api\ITestAssemblyRunner.cs" />
<Compile Include="Api\NUnitTestAssemblyRunner.cs" />
<Compile Include="Api\RuntimeAssemblyBuilder.cs" />
<Compile Include="Assert.Comparisons.cs">
<DependentUpon>Assert.cs</DependentUpon>
</Compile>
@@ -147,6 +148,7 @@
<Compile Include="Attributes\IgnoreAttribute.cs" />
<Compile Include="Attributes\IncludeExcludeAttribute.cs" />
<Compile Include="Attributes\SingleThreadedAttribute.cs" />
<Compile Include="Attributes\TestAssemblyBuilderAttribute.cs" />
<Compile Include="Attributes\TestAssemblyDirectoryResolveAttribute.cs" />
<Compile Include="Attributes\LevelOfParallelismAttribute.cs" />
<Compile Include="Attributes\MaxTimeAttribute.cs" />
@@ -126,6 +126,7 @@
<Compile Include="Api\ITestAssemblyBuilder.cs" />
<Compile Include="Api\ITestAssemblyRunner.cs" />
<Compile Include="Api\NUnitTestAssemblyRunner.cs" />
<Compile Include="Api\RuntimeAssemblyBuilder.cs" />
<Compile Include="Assert.Comparisons.cs">
<DependentUpon>Assert.cs</DependentUpon>
</Compile>
@@ -189,6 +190,7 @@
<Compile Include="Attributes\SingleThreadedAttribute.cs" />
<Compile Include="Attributes\TearDownAttribute.cs" />
<Compile Include="Attributes\TestActionAttribute.cs" />
<Compile Include="Attributes\TestAssemblyBuilderAttribute.cs" />
<Compile Include="Attributes\TestAttribute.cs" />
<Compile Include="Attributes\TestCaseAttribute.cs" />
<Compile Include="Attributes\TestCaseSourceAttribute.cs" />

0 comments on commit 337f241

Please sign in to comment.