Why autogenerated MSTests are bad for you!

MS can generate complete test classes for you that test and access all methods and properties, no matter if they are private or public. Isn’t that awesome? Well, no.

Let me make a statement first: I’m not fond of auto-generated code, no matter what it is for.

Why? Because writing code makes you think. You’ll have a better chance to find design errors early than if you get a lot of stuff generated for you. You also need to know how stuff works. If everything is auto-generated for you, you don’t have to care. Right?

Back to MSTest.

Reason 1:
The access to private/protected members/properties is bad for you. If you can’t manage to test those methods/properties through the public API, it’s most likely that they can’t be called from your regular application either. Either remove the code that you can’t test or refactor the class.

Reason 2:
The generated test classes include one test per method. Inexperienced unit testers might think that one test per method is enough: they either just write one test or include all the different tests in the same test method. Both of those options make it a lot harder to test and get robust applications.

Reason 3:
Readability. The generated tests are harder to read:

(I’ll post samples here later)

Conclusion: build all your tests by yourself (it makes you think about your design once more). Remove code that you can’t test through the public interface (or refactor).