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:
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.
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?