“Generate Test” in Visual Studio

Martin Platt posted about when not to use Generate Test in VS – when your code doesn’t work.

I’m not that fanatic saying don’t use this feature at all, but I agree with the supposition – the code generator is not that smart. It doesn’t assume ANYTHING about your code. Make sure you write a test that:

  • Makes sense
  • Readable
  • Maintainable
  • Passes

If this tool (or any other tool, for that matter) doesn’t help you to create a test with these traits – don’t use it.

  • Avish

    I find that the worst thing about the VS “generate unit tests” feature is that it generates a single test for each member, called [MemberName]Test.
    This misleads newcomers to unit testing into thinking that “one test equals one member”, and they start testing members instead of features, or stories, or scenarios.
    Even when people realize they need more than one test per member, I’ve seen then end up with tests called MyMethodTest, MyMethodTest2, MyMethodTest3 etc., because they couldn’t get over the misdirection they got from the tool.

    As always, with code-generation tools or graphical designers from Microsoft, it’s important to distinguish between the things the tool does because “this is how you should be working”, and the things it does because “we didn’t make it smart enough to know better”.

  • Gil Zilberfeld

    Avish,

    The perception of “This is from MS, therefore it’s a guidance” is very much out there. You cannot just root it out.

    The problem is not “we didn’t make it smart enough”, rather “we didn’t make it think for you”. It’s a tool, plain and simple. It cannot invent a good name for the test.

    When I guide people on how to write tests (Isolator or not), I start with naming the test. Writing the name of the test(I use Roy‘s convention of Method_Scenario_ExpectedResult) makes you think about what you’re about to do. I even let discussion on the specific scenario I’m testing go to lengths, instead of jumping in, because by making you think, you’ll get a better, shorter, focused test.

  • Avish

    Gil,

    I agree fully with what you said, and my point is precisely this: I don’t expect the tool to think for me; but in cases where it can’t do this, I expect it to be very blunt about it and say “listen, human, you have to intervene here”.

    Ask me what the test name should be! By all means, yes, make me think! Generating a terrible default value and not mentioning its defaultness or terribility is misguiding, precisely because people perceive anything from MS as guidance, and because all humans tend to follow the path of least resistance.

    If it requires thinking to get right, force me to do it. Don’t let me off the hook with default values.

  • Gil Zilberfeld

    Interesting. When they generate the test, they put Assert.Inconclusive to make you fill the gap.

    Perhaps they didn’t perceive the naming of such importance. Not much we can do, apart from training others on this ourselves.

    Work, work, work.

TOP