CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

ClassMemberDisplayLayout Attribute Classs?

A few weeks ago, Raymond IM’d me and asked if I remembered the attribute that you can apply to your class to force the explicit layout of members.  I asked him why he would need or care about the layout of his class members, and he asked me: “Do you use NUnit?” and I immediately understood why he cared.

NUnit, in it’s GUI, presents your test cases in alphabetical oder, not in the order that you created them in your class. Other programs like Reflector also present the methods in alpha order, in fact it looks like members are laid out in IL in this order.   This can be a little confusing when you’re doing TDD, and you’ve written a bunch of test cases, and you go to implement them.  So say you have this test fixture:

  [TestFixture]

  public class LiveLinkServiceTest

  {

    LiveLinkPrincipal [] liveLinkUsers;

 

    [SetUp]

    public void SetUp()

    {

      LiveLinkConfigInfoProvider llcip = new  LiveLinkConfigInfoProvider();

      liveLinkUsers = llcip.GetAllLiveLinkUsers(); 

      Assert.IsNotNull(liveLinkUsers, "LiveLinkPrinipal Array is NULL");

      Assert.IsTrue(liveLinkUsers.Length > 0, "LiveLinkPrinipal Array is EMPTY");

    }

 

    [Test]

    public void TestGetLiveLinkTransactions()

    { 

      LiveLinkService lls = new LiveLinkService();

      ContainerTransactions cts = lls.GetNewTransactions(System.DateTime.Now.Subtract(TimeSpan.FromMinutes(10)),liveLinkUsers[0]);

      Assert.IsTrue(cts.CONTAINER_TRANSACTIONS.Count > 0, "Container Transactions is empty");

      Console.Out.WriteLine(cts.GetXml());

    }

 

    … // Rest of class removed

}

Here’s how it ends up looking in NUnit… Note that the order of my tests are alphabetical, not in the order I arranged them in my class.

Unittest1

NUnit will load the DLL, and display your methods in alphabetical order, unless it’s already loaded and cached the tests, in which case it adds new methods that it finds to the bottom of the tree.  This only happens when the DLL is re-loaded.

It’d be nice to test them one at a time, from top-to-bottom, as you write the code and go “green”, in the order that you arrange them in code.

So, here’s the point of this post.  Is there an attribute that you can use to order these methods inside an assembly?  I’ve researched StructLayout Attribute Class. and while it will affect your assembly class’ fields, methods are not affected.

If there’s not an existing attribute, would it make sense to have one that programs like Reflector and NUnit, that use reflection to determine class members, can use to determine method ordering for display purposes?

How about ClassMemberDisplayLayout Attribute Classs? Would this make sense?

-Brendan



Comments

Eric Wise said:

There are other unit test packages that do have the concept of execution order. NUnit just isn't there yet.
# April 14, 2005 6:46 AM

Raymond Lewallen said:

This has been driving me absolutely nuts. I am almost positive that there is an attribute that will effect the compiled layout of all class members, and I think I read it in something either Jeffrey Richter or Serge Lidin wrote. I sure wish I could find it again. Maybe I'm wrong, maybe it doesn't exist and I'm just thinking that I read it somewhere, but it sure would be a nice attribute to have.
# April 14, 2005 6:47 AM

Pete Nelson said:

Look through my NUnit test classes and you'll see stuff like this:

public void _01_CreateAndDeleteRandomBOM()
public void _05_CreateOneHundredRandomBOMs()
public void _09_SearchBOMs()
public void _99_DeleteRandomBOMs()

Seems like it would be pretty easy for NUnit to support ordering of tests via a constructor parameter on their [Test] attribute.
# April 14, 2005 7:57 AM

Scott Isaacs said:

I've resorted (no pun intended) to naming conventions like Pete has described. I chose to use four digits though, because you never know when you'll need to write 10,000 unit tests in one fixture. ;) Actually I chose that to allow easier grouping (e.g., all tests of adding data might be in the 1000s, deleting data might be in the 9000s, etc.).
# April 14, 2005 9:19 AM

Brendan Tompkins said:

Yep. This looks like the only resort, currently.

All kinds of problems with this, though, it's clunky, adding tests has to be planned for, remember line numbering?

Not to mention that if you discover you need a test and add it, it's not in the right order, it's at the end. (until you re-start NUnit, I think)
# April 14, 2005 10:09 AM

Mike Powell said:

OT: I like the screenshot--what do you use for that?
# April 14, 2005 10:46 AM

Brendan Tompkins said:

Took a screencap and polished it up with Macromedia Fireworks...
# April 14, 2005 11:09 AM

darrell said:

They're alphabetical because reflection uses a non-deterministic method in pulling method info back, so the order sometimes kept changing etc.

I know MbUnit has a Sequence attribute property, and with NUnit you can simulate it simply by adding numbers to the beginning of the test method, like: 01TestDequeueLiveLinkTransactions . Yeah, that's kind of hokey, but it already works. :)
# April 14, 2005 11:38 AM

darrell said:

Oh, and the two attributes/properties you mentioned, those affect the memory layout. I'm not sure if that affects how reflection would pull the member info back since I haven't used reflector to check out what it does in the GetMethods() method.
# April 14, 2005 11:40 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Free Tech Publications