If I'm answering someone's question, instead of saying "your code will fail on input x, y or z", should I provide a unit test snippet?


2 Answers 2


I think in general our answers should be concise and clear. It units help us communicate something then perhaps they should be included. If they merely clutter the answer then I'd say no.


I don't think it's bad to include a reproducible test-case that shows where the code went wrong. But don't get bogged down in anything irrelevant to the task at hand. So don't bother writing a full testing harness, just post the relevant bit. So if it's a function that throws an exception when fed foo:

func('foo'); // <-- throws exception

But at the same time, if you need more code, absolutely post it...

  • 2
    \$\begingroup\$ In the same vein, where possible, it'd be nice for the OP to include a couple of lines calling the code with sample input. Comments describing input and expected output could work too. Sometimes it makes it much easier to visualize problems or optimizations. \$\endgroup\$
    – TryPyPy
    Jan 20, 2011 at 2:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .