Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometimes I end up writing unit test cases for the code other developers have written .There are occasions when I really didn't know what the developer was trying to do (the business part) and just manipulated the test case to get the green line . Are these things normal in the industry ?

What is the normal trend ? Are the developers who wrote the code supposed to write the unit test cases ?

share|improve this question
"dint"? What does "dint" mean? – S.Lott Feb 25 '11 at 19:13
up vote 10 down vote accepted

Try reading this blog post: Writing Great Unit Tests: Best and Worst Practices.

But there are countless others on the web.

In direct answer to your questions...

  1. "Normal trend" - I guess this could differ from place to place, whats normal for me might be odd for others.
  2. I would say (in my option) the developer who writes the code should write the test, ideally using methods like TDD, where you would write the test before the code. But others may have different methods and ideas here!

And the way you described of writing the tests (in your question) is totally wrong!!

share|improve this answer

This approach make the unit test worthless.

You need to have the unit test fail when some real action does not work as intended. If you don't do it like that, and perhaps even write the test before the code to test, it is like having non-functioning smoke alarms.

share|improve this answer
This is not quite true. Or rather, it is true in an ideal world, but alas, often we are far from that. Consider having legacy code without tests and without specifications, and without anyone who could reliably tell you up to the minute details, what a specific piece of code is precisely supposed to do (this is reality in a large proportion of existing projects). Even in this case, it may still worth writing unit tests to lock the current state of the code, and to ensure that you don't break anything with future refactoring, bug fixes or extensions. – Péter Török Feb 25 '11 at 19:08
Also, I guess you meant "write the test after the code to test" did you? – Péter Török Feb 25 '11 at 19:14
@Péter, the wording went wrong - you got it right. But, if you decide to write tests they should do something to be useful. Just blindly invoking code saying it is a test, is - in my opinion - not testing. – user1249 Feb 25 '11 at 22:42
ørn, if you mean that we must have meaningful asserts in our unit tests, to verify that the tested code indeed does what we think it does, I fully agree. – Péter Török Feb 26 '11 at 12:53

If you don't know what a function does then you can't write a unit test for it. For all you know it doesn't even do what it's supposed to. You need to find out what it's supposed to do first. THEN write the test.

share|improve this answer

When im writing test cases (for printers) I try to think of each little components....and what can I do to possibly break it. So lets say the scanner for instance, what commands does it use (in the pjl printer-job-language) what can I write to test every bit of functionality....Ok now what can I do to try and break that.

I try to do that for each major components, but when it comes to software and not so much hardware you want to look at each method/function and check boundaries and such.

share|improve this answer

It sounds like you are working with other developers (or maintaining code written by other developers) who do not do unit testing. In that case, I think you'd definitely want to know what the object or method you're testing is supposed to do, then create a test for it.

It won't be TDD because you didn't write the test first, but you could improve the situation. You might also want to create a a copy of the objects under test with stubs so that can establish that your tests function properly when the code fails.

share|improve this answer

protected by gnat May 6 at 10:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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