views:

65

answers:

2

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);
        }
    }
A: 

Can you make use of TypeDescriptor?

Bryan Watts
@Bryan, I am checking on it right now! Thanks
azamsharp
Seems like the TypeDescriptor can only be used to add attributes on class level.
azamsharp
Too bad. It seems to have PropertyDescriptor and EventDescriptor, but no MethodDescriptor. Odd. Looking at the problem from another angle, can you tell your unit testing framework which classes and methods are testable? If so, you could make your own list of methods and pass it in. Not sure if that is possible, but it would be nice :-)
Bryan Watts
+2  A: 

What happens if you do not decorate the Test methods with the Test attribute, but instead, you prefix the name of the Test-methods with Test.

See this.

NUnit should recognize it as a Test-method as well, without you having to decorate the method with TestAttribute However, I've tested this with NUnit 2.4.7, and this doesn't seem to be true anymore.

Anyway, I do not see the reason why this bothers you ? What is the problem with having a method decorated with a Test-Attribute ? This just makes it explicit that it is a Test. I like explicitness :)

Frederik Gheysels
I use MBUnit and I just tried it and it did not work. You are right it is not a big deal to decorate methods with [Test] attribute but I was looking for a way to remove it.
azamsharp
TrueWill