During unit testing I need to decorate my methods with Test attributes as shown below:
[Test]
public class when_1_is_passed : specifications_for_prime_test
{
public void should_return_false()
{
Assert.AreEqual(1,1);
}
}
The [Test] attributes represents that a method is a test method. I want to get rid of the [Test] attribute. In 95% of the cases all the methods defined in the test suite will be test methods. The other 5% can be initialization code etc.
Anyway, now somehow I need to inject the TestAttribute to all of the methods dynamically. I have the following code but I don't see a way to inject attributes on a method.
public static void Configure<T>(T t)
{
var assemblyName = "TestSuite";
var assembly = Assembly.Load(assemblyName);
var assemblyTypes = assembly.GetTypes();
foreach (var assemblyType in assemblyTypes)
{
if(assemblyType.BaseType == typeof(specifications_for_prime_test))
{
// get all the methods from the class and inject the TestMethod attribute
var methodInfos = assemblyType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach(var methodInfo in methodInfos)
{
// now attach the TestAttribute to the method
}
}
}
var methodsInfo = t.GetType().GetMethods();
}
OR there maybe some hidden feature of the unit testing framework which allows the user to simply put the attribute on the class and all the methods inside that class becomes a test method.
Here is what the Tests looks like:
[TestFixture]
public class specifications_for_prime_test
{
[SetUp]
public void initialize()
{
UnitTestHelper.Configure(this);
}
}
public class when_1_is_passed : specifications_for_prime_test
{
public void should_return_false()
{
Assert.AreEqual(1,1);
}
}