Hot answers tagged

257 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
  • 8,921
199 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
197 votes
Accepted

Does TDD make defensive programming redundant?

That's ridiculous. TDD forces code to pass tests and forces all code to have some tests around it. It doesn't prevent your consumers from incorrectly calling code, nor does it magically prevent ...
enderland's user avatar
  • 12.1k
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
122 votes

Where is the line between unit testing application logic and distrusting language constructs?

Should savePeople() be unit tested? Yes. You aren't testing that dataStore.savePerson works, or that the db connection works, or even that the foreach works. You are testing that savePeople fulfills ...
Bryan Oakley's user avatar
  • 25.2k
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
86 votes

Why is agile all about the test-driven development (TDD) and not development-driven test (DDT)?

Software is not a house. Intuition is good, but understand that it isn't always correct. Break down all the specs into inspection I think I will need (see into the future). This isn't accurate. In ...
Telastyn's user avatar
  • 109k
83 votes
Accepted

Why is agile all about the test-driven development (TDD) and not development-driven test (DDT)?

One of the benefits of a TDD approach is only realised when you also do emergent design. So in your first analogy, you wouldn't write 100 tests, as there's no possible way that you'll know what your ...
Phil Riley's user avatar
81 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
78 votes
Accepted

Is there a need to keep tests for simple (self-contained) functions?

Regression testing It's all about regression testing. Imagine the next developer looking at your method and noticing that you are using magical numbers. He was told that magical numbers are evil, so ...
Arseni Mourzenko's user avatar
73 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
  • 45.5k
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
61 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
  • 45.5k
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
42 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,664
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
38 votes

Where is the line between unit testing application logic and distrusting language constructs?

Usually this kind of question comes up when people do "test-after" development. Approach this problem from the point of view of TDD, where tests come before the implementation, and ask yourself this ...
MichelHenrich's user avatar
38 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,679
37 votes
Accepted

Agile without unit tests

To be pedantic, nothing in the Agile Manifesto or the Scrum Guide make any reference to technical practices, like unit testing or TDD, at all. So, yes, in theory you could deliver early and often with ...
RubberDuck's user avatar
  • 8,921
33 votes

Does TDD make defensive programming redundant?

Defensive programming and unit tests are two different ways of catching errors and each have different strengths. Using only one way of detecting errors makes your error detection mechanisms fragile. ...
Kevin Fee's user avatar
  • 2,837
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

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