Meta tests

I found this test fixture hilarious.

[TestFixture]
public class ValidateNamingConventions
{
    private System.Type[] types;

    [SetUp]
    public void Setup()
    {
        types = typeof(ISearchFactory).Assembly.GetExportedTypes();            
    }

    [Test]
    public void InterfacesStartsWithI()
    {
        foreach (System.Type type in types)
        {
            if(type.IsInterface==false)
                continue;
            Assert.IsTrue(type.Name.StartsWith("I"),type.Name);
        }
    }

    [Test]
    public void FirstLaterOfMethodIsCapitalized()
    {
        foreach (System.Type type in types)
        {
            foreach (MethodInfo method in type.GetMethods())
            {
                Assert.IsTrue(char.IsUpper(method.Name[0]), type.FullName + "." + method.Name);
            }
        } 
    }

    [Test]
    public void AttributesEndWithAttribute()
    {
        foreach (System.Type type in types)
        {
            if (type.IsAssignableFrom(typeof(Attribute)))
                continue;
            Assert.IsTrue(type.Name.EndsWith("Attribute"), type.Name);
     
        }
    }
}

Print | posted on Wednesday, October 01, 2008 9:14 AM

Feedback


Gravatar

# re: Meta tests 10/1/2008 10:01 AM Casey

Well I started reading it thinking "how ridiculous" ... but now I'm not so sure ... in the absense of FxCop or NDepend ... it is one way of ensuring coding standards are being applied, though I grant a rather ingenious one :)


Gravatar

# re: Meta tests 10/1/2008 10:17 AM ZeusTheTrueGod

Looks like reinventing the weel
May be better put this into PostBuild event?
I prefer Agent Smith plugin for Resharper - it shows me when code convention is broken


Gravatar

# re: Meta tests 10/1/2008 10:27 AM Jason Stangroome

Definitely hilarious. Really should be using MbUnit's TestSuite feature or your preferred test framework's equivalent so you get one test passing or failing for each interface, method, and attribute, not just a test that fails on the first problem it finds.


Gravatar

# re: Meta tests 10/1/2008 10:32 AM Tobin Harris

They forgot to add

[Test]
public void TestNamesAreSmeltCorrectly(){...}

To catch the spelling error in "FirstLaterOfMethodIsCapitalized".


Gravatar

# re: Meta tests 10/1/2008 4:11 PM otter

that is very funny, and a very cool idea. however i don't think i would ever want to include a test like that as part of my continuous integration. it doesn't seem like it 'belongs' as a true unit test, perhaps it would be best used as a separate test from your standard unit tests.


Gravatar

# re: Meta tests 10/1/2008 9:43 PM Morgan

Thats a bit hilarius.
I use Code Style Enforcer for this, it plays nicely with ReSharper. You can customize the rules.
Anyone tried Microsofts StyleCop?


Gravatar

# re: Meta tests 10/2/2008 4:22 AM Judah Himango

I've tried StyleCop. It complained about some things we don't like ("always use 'this.' before accessing members", for example). We had literally several thousand violations. So we didn't pursue it any further.


Gravatar

# re: Meta tests 10/2/2008 9:11 AM Morgan

I think you can turn off rules in StyleCop, but you cant change a rule that says that private fields should start with m_ when you want only _.
So you have to disable that rule and implement your own custom rule.


Gravatar

# re: Meta tests 10/2/2008 4:54 PM Bunter

This kind of test is perfect for continuous integration as it will reveal convention mistakes for whole team extremely quickly and well, continuously.


Gravatar

# re: Meta tests 10/2/2008 11:15 PM John Rayner

We have tests like this to validate some of the conventions we've built up on the project. For instance, we have an AOP framework in play and so we have a test that validates that constructors are protected and not public. We have another that checks that properties meant to AOP'd are virtual.

We've even taken it a step further - for integration tests which connect to the database we setup a transaction at the start of the test and rollback when the test completes, but this is done in a base class. So we actually have a test that validates that integration tests derive from this base class!!!

To be honest, I'd rather having a test that fails with a clear message than more subtle problems.


Gravatar

# re: Meta tests 10/8/2008 8:10 PM Roy Tate

Function AttributesEndWithAttribute should be corrected ...

if (type.IsAssignableFrom(typeof(Attribute)))

should be

if (type.IsAssignableFrom(typeof(Attribute)) == false)

Comments have been closed on this topic.