5

Usually when writing unit tests I create a helper class with extension methods. I use these methods exclusively for generating generic data for things that I am too lazy to type out. I usually name these classes something relevant to the class under test - for example, I'll follow a pattern like TestHelper.cs.

I've read that helper classes don't promote good design; however I know SOLID principles pretty well and I don't see how using "helpers" in the way described is violating any principles.

I think the issue I'm having is with the name of my "TestHelper" ... Should I simply come up with a new naming scheme or am I really violating a SOLID Principle by utilizing these helpers?

  • 1
    How is this related to SOLID? To me this question is simply about naming conventions. – Oded Sep 12 '11 at 20:56
5

This is not anti-SOLID at all, but you may want to consider the Object Mother pattern, which solves exactly the same problem in a similar way and has the added bonus of giving you a funky suffix to use in your class names.

Plus, like all patterns, because they do have a name, any newcomer to the team can simply google the term Object Mother and find a wealth of information, explaining what it is you've done.

| improve this answer | |
3

I don't see how this violates SOLID or relates to SOLID at all.

In terms of class naming, if the purpose it to generate test data, you can use a naming convention like <test name>DataGenerator and a GetTestData extension method to make it as clear as possible.

| improve this answer | |
  • I came across this article a while back and my 'Helper' naming has been bugging me ever since. blogs.msdn.com/b/nickmalik/archive/2005/09/06/461404.aspx – Robotsushi Sep 12 '11 at 21:04
  • @Robotsushi - looks like a rant about all purpose swiss army knife helper classes. Something that is single purpose as you describe is not a problem. – Oded Sep 12 '11 at 21:06
1

Here some other suggestions, you can use:

  • Mocks
  • [AssemblySetUp] *
  • [FixtureSetUp] *

*(from NUnit, or similar in other framework)

| improve this answer | |
  • i seperate the data generators from the test classes or else i would markup the data generation method with : [TestInitialize] – Robotsushi Sep 12 '11 at 21:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.