Skip to main content
258 votes

How do you write unit tests for code with difficult to predict results?

There are two things you can test in difficult-to-test code. First, the degenerate cases. What happens if you have no elements in your task array, or only one, or two but one is past the due date, etc....
Karl Bielefeldt's user avatar
244 votes
Accepted

When do you write the "real" code in TDD?

If I have a fairly complex object with a complex method, and I write my test and the bare minimum to make it pass (after it first fails, Red). When do I go back and write the real code? And how much ...
RubberDuck's user avatar
  • 9,021
200 votes
Accepted

Is there such a thing as having too many unit tests?

Yes, with 100% coverage you will write some tests you don't need. Unfortunately, the only reliable way to determine which tests you don't need is to write all of them, then wait 10 years or so to see ...
Karl Bielefeldt's user avatar
140 votes
Accepted

Why isn't TDD more popular in universities?

I am a part-time programming teacher at a local community college. The first course that is taught at this college is Java Programming and Algorithms. This is a course that starts with basic loops ...
Robert Harvey's user avatar
101 votes

Why isn't TDD more popular in universities?

First of all, we have to fundamentally distinguish between Computer Science and Software Engineering. (And maybe to a lesser extent between Software Engineering and Programming or "Coding".) As one ...
Jörg W Mittag's user avatar
82 votes

How do you write unit tests for code with difficult to predict results?

I used to write tests for scientific software with difficult-to-predict outputs. We made a lot of use of Metamorphic Relations. Essentially there are things you know about how your software should ...
James Elderfield's user avatar
80 votes

Is there such a thing as having too many unit tests?

If you have worked on large code bases created using Test Driven Development, you would already know there can be such a thing as too many unit tests. In some cases, most of the development effort ...
Frank Hileman's user avatar
75 votes

How should I test "Glue Functions" without testing that "the code I wrote is the code I wrote"?

Boring structural code doesn’t need isolated testing. Test interesting code. That code has a behavior. Nail down the behavior you expect and not only will your code likely be correct, it’ll be easier ...
candied_orange's user avatar
66 votes
Accepted

Struggling with cyclical dependencies in unit tests

You're worrying about implementation details too much. It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should ...
Philip Kendall's user avatar
65 votes
Accepted

How do I really write tests without mocking/stubbing?

This answer consists of two separate views on the same issue, as this isn't a "right vs wrong" scenario, but rather a broad spectrum where you can approach it the way it's most appropriate ...
Flater's user avatar
  • 56.3k
63 votes

Are the tests for test-driven development (TDD) always unit-tests?

The red green refactor cycle is built on one very sound principle: Only trust tests that you have seen both pass and fail. Yes that works with automated integration tests as well. Also manual tests. ...
candied_orange's user avatar
60 votes

How can I do TDD in real-world applications?

Behavior Start with behavior. Don't focus on structure. What matters is some complex operation is supposed to transform some old data to some new data. You can create a test by finding examples of the ...
candied_orange's user avatar
56 votes

Why isn't TDD more popular in universities?

TDD is a nice process in "real-world" programming because problems often arrive on our desks underspecified. "Add a feature that does X", "fix the bug where it shows the wrong thing if you do Y", etc. ...
MattPutnam's user avatar
52 votes
Accepted

What happens with methods' tests when that method become private after re-design in TDD?

In TDD, the tests serve as executable documentation of your design. Your design changed, so obviously, your documentation must, too! Note that, in TDD, the only way in which the attack method could ...
Jörg W Mittag's user avatar
50 votes
Accepted

In TDD, should I add unit tests to refactored code?

Testing before and after In TDD, should I add unit tests to refactored code? "refactored code" implies you are adding the tests after you've refactored. This is missing the point of testing your ...
Flater's user avatar
  • 56.3k
47 votes

When do you write the "real" code in TDD?

The "real" code is the code you write to make your test pass. Really. It's that simple. When people talk about writing the bare minimum to make the test green, that just means that your real code ...
GenericJon's user avatar
47 votes
Accepted

How do integration tests criticize design?

Microtests can help lead to good design. By writing good small tests, you are deliberately testing a small amount of code and filling in its gaps with mock objects. This leads to low coupling (things ...
Thorin Jacobs's user avatar
43 votes

Is there such a thing as having too many unit tests?

Answers to your questions Is there such a thing as having too many unit tests? Sure... You could, for example, have multiple tests which seem to be different at first glance but really test the ...
AnoE's user avatar
  • 5,877
40 votes

How do you write unit tests for code with difficult to predict results?

The other answers have good ideas for developing tests for edge or error case. For the others, using the algorithm itself is not ideal (obviously) but still useful. It will detect if the algorithm (...
user949300's user avatar
  • 8,919
39 votes

Is it a good idea to write all possible test cases after transforming the team to TDD to achieve a full coverage?

There was no test-driven development process during the development due to very tight deadlines This statement is very concerning. Not because it means you developed without TDD or because you aren't ...
candied_orange's user avatar
33 votes
Accepted

How to correct a mistake in the test, after writing implementation

The absolutely critical thing is that you see the test both pass and fail. Whether you delete the code to make the test fail then rewrite the code or sneak it off to the clipboard only to paste it ...
candied_orange's user avatar
33 votes
Accepted

What is black box unit testing?

Your professor is right: unit testing can be either black-box or white-box. The difference is less about what the tester knows, but more about how you generate test cases. With black box testing, you ...
amon's user avatar
  • 135k
33 votes

Why isn't TDD more popular in universities?

I suspect it's mostly because writing automated tests is more difficult than writing the code being tested. When you're still struggling with the basic mechanics, it's difficult to add another layer. ...
Karl Bielefeldt's user avatar
31 votes
Accepted

Are the tests for test-driven development (TDD) always unit-tests?

All TDD requires of you is that you write a failing test, then modify your code to make it pass. Typically "unit tests" are small and fast and test some part of your code in isolation. Because they ...
David Arno's user avatar
  • 39.5k
31 votes

How should factored-out code be tested as part of the TDD refactoring step?

You went wrong by thinking of stringy_sum as something that should be tested. It's an internal implementation detail, and you shouldn't test internal implementation details. An alternative way of ...
Philip Kendall's user avatar
30 votes
Accepted

Testing a list...All in the same test or one test for each condition?

The simple rule of thumb I use for whether to perform a set of tests in one test case, or many, is: does it involve just one setup? So if I were testing that, for multiple elements, it both ...
David Arno's user avatar
  • 39.5k
30 votes

How do integration tests criticize design?

The extremely short version: smaller tests, because they run smaller parts of the system, naturally constrain what the programmers can write, and so this creates an opportunity for sharper (easier to ...
J. B. Rainsberger's user avatar
27 votes

Is there such a thing as having too many unit tests?

Yes, it is possible to have too many unit tests. If you have 100% coverage with unit tests and no integration tests for example, you have a clear issue. Some scenarios: You over-engineer your tests ...
Bruno Guardia's user avatar
27 votes
Accepted

How can you TDD for a bug that can only be tested after it has been fixed?

When I understood you correctly, you cannot even write a reliable automated test for your "ghost image" example after you found a solution, since the only way of verifying the correct behaviour is to ...
Doc Brown's user avatar
  • 214k
26 votes

How should factored-out code be tested as part of the TDD refactoring step?

Philip Kendall's answer says the issue can be discussed away by interpreting stringy_sum as a private function, as an implementation detail which does not need a test on its own. That is a possible ...
Doc Brown's user avatar
  • 214k

Only top scored, non community-wiki answers of a minimum length are eligible