2

Recently I've been introduced to Behavior Driven Testing and have run into an issue with the way classes are named.

Example:

[TestClass]
public class MyObjectTests
{
  [TestClass]
  public class GivenValidAgruments : MyObjectTests
  {
    [TestClass]
    public class WhenConfigurationIsValid : GivenValidAgruments
    {
      [TestClass]
      public class WhenAnotherCaseIsTrue : WhenConfigurationIsValid
      {
        [TestMethod]
        public void ShouldDoSomething()
        {
          Assert.IsTrue(true);
        }
      }
    }
  }
}

This makes for a class name like this:

MyObjectTests.GivenValidArguments.WhenConfigurationIsValid.WhenAnotherCaseIsTrue

I can see the benefit of this kind of a structure. Unfortunately, I've run into an issue where the class name is getting too long. Either Visual Studio is getting confused, or I've reached some kind of limitation in the unit test framework when it uses reflection to get all the classes. Regardless of the actual cause, the problem is that the names are too long.

Have you run into this before? What did you do to resolve the issue? Do you have recommendations for following this pattern, but making it cleaner?

Seattle Leonard
  • 6,548
  • 3
  • 27
  • 37
  • Surely these classes aren't both nested and inherited? Btw [There is n o limit in the CLR](http://stackoverflow.com/questions/186523/what-is-the-maximum-length-of-a-c-cli-identifier) – Nathan Cooper Jul 22 '14 at 20:50
  • 1
    They are nested and inherited. This structure was shown to me as the way they want it done. There are some great benefits to doing it this way. By nesting them, you get the class name like I showed above. That's the way it will appear in the test Explorer. By inheriting also, you can make for some nice clean set up code. In other words, you only have to set it up once and all the sub-classes (Cases within that logic flow) can call the base set up. – Seattle Leonard Jul 22 '14 at 20:54
  • About CLR limits. My short bit of research I did today would agree with your statement. However, I have narrowed it down to only changing the length of the class name (or sub-class) by one character. – Seattle Leonard Jul 22 '14 at 20:56
  • There still are file name limits. Maybe you're hitting them when it builds? – Nathan Cooper Jul 22 '14 at 20:57
  • This is all within one file. – Seattle Leonard Jul 22 '14 at 20:58
  • I don't really know what you mean by trouble, so I'm just guessing. One more: Since you're thinning out the contents in each class to do this, you're not now expecting ClassInitialize to run on any classes with test methods directly in them are you? – Nathan Cooper Jul 22 '14 at 21:06
  • I have a virtual base.SetUp() that is called by all the inherited classes. All classes with TestMethods must also have a TestInitialize method to take advantage of the base.SetUp() – Seattle Leonard Jul 22 '14 at 21:10
  • 3
    Where exactly are you having problems? What happens? I can't reproduce any error with the code posted (it works as expected). – MAV Jul 22 '14 at 21:12
  • The sample code will not cause any issues. Nest the "When" classes a few more times and give them long names. Then, right click a single TestMethod and select Run Tests. Only 1 test should run. However, multiple tests (seemingly chosen at random) end up getting run and it won't debug into the method under those circumstances. – Seattle Leonard Jul 22 '14 at 21:14

0 Answers0