Created
January 28, 2013 09:23
-
-
Save ptomasroos/4654162 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
namespace ClassLibrary2 | |
{ | |
using System; | |
using System.Reflection; | |
using NUnit.Core; | |
using NUnit.Core.Extensibility; | |
using NUnit.Framework; | |
namespace ClassLibrary1 | |
{ | |
[NUnitAddin(Name = "SampleSuiteExtension", Description = "Recognizes Tests starting with SampleTest...")] | |
public class Addin : IAddin | |
{ | |
#region IAddin Members | |
public bool Install(IExtensionHost host) | |
{ | |
IExtensionPoint builders = host.GetExtensionPoint("SuiteBuilders"); | |
if (builders == null) | |
return false; | |
builders.Install(new SampleSuiteExtensionBuilder()); | |
return true; | |
} | |
#endregion | |
} | |
class SampleSuiteExtension : TestSuite | |
{ | |
public SampleSuiteExtension(Type fixtureType) | |
: base(fixtureType) | |
{ | |
// Create the fixture object. We could wait to do this when | |
// it is needed, but we do it here for simplicity. | |
this.Fixture = Reflect.Construct(fixtureType); | |
// Locate our test methods and add them to the suite using | |
// the Add method of TestSuite. Note that we don't do a simple | |
// Tests.Add, because that wouldn't set the parent of the tests. | |
foreach (MethodInfo method in fixtureType.GetMethods( | |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) | |
{ | |
if (method.Name.StartsWith("SampleTest")) | |
this.Add(new NUnitTestMethod(method)); | |
} | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] | |
public sealed class SampleSuiteExtensionAttribute : Attribute | |
{ | |
} | |
/// <summary> | |
/// SampleSuiteExtensionBuilder knows how to build a SampleSuiteExtension | |
/// </summary> | |
public class SampleSuiteExtensionBuilder : ISuiteBuilder | |
{ | |
#region ISuiteBuilder Members | |
// This builder delegates all the work to the constructor of the | |
// extension suite. Many builders will need to do more work, | |
// looking for other attributes, setting properties on the | |
// suite and locating methods for tests, setup and teardown. | |
public Test BuildFrom(Type type) | |
{ | |
if (CanBuildFrom(type)) | |
return new SampleSuiteExtension(type); | |
return null; | |
} | |
// The builder recognizes the types that it can use by the presense | |
// of SampleSuiteExtensionAttribute. Note that an attribute does not | |
// have to be used. You can use any arbitrary set of rules that can be | |
// implemented using reflection on the type. | |
public bool CanBuildFrom(Type type) | |
{ | |
return type.GetCustomAttributes(typeof (SampleSuiteExtensionAttribute), true).Any(); | |
//return Reflect.HasAttribute(type, "NUnit.Core.Extensions.SampleSuiteExtensionAttribute", false); | |
} | |
#endregion | |
} | |
/// <summary> | |
/// Test class that demonstrates SampleSuiteExtension | |
/// </summary> | |
[SampleSuiteExtension] | |
public class SampleSuiteExtensionTests | |
{ | |
public void SampleTest1() | |
{ | |
Console.WriteLine("Hello from sample test 1"); | |
} | |
public void SampleTest2() | |
{ | |
Console.WriteLine("Hello from sample test 2"); | |
} | |
public void NotATest() | |
{ | |
Console.WriteLine("I shouldn't be called!"); | |
} | |
[Test] | |
public void APA() | |
{ | |
Console.WriteLine("I shouldn't be called either!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment