Skip to main content
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
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
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
  • 59.5k
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
  • 59.5k
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
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
  • 9,029
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
34 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
  • 136k
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

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.6k
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
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
  • 220k
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
  • 220k
25 votes

Does TDD contradict the open-closed principle?

Frankly, I see at least three huge misconceptions in this question: what TDD is about, and what the OCP is about, and software is developed in a waterfall approach. Let me start with the OCP. The ...
Doc Brown's user avatar
  • 220k
25 votes
Accepted

How do you push Design changes in TDD in late development stage

How do you do design change in TDD when you have such huge amounts of test that may be dependent on the existing implementation? First, the best approach may be to avoid running into this situation ...
Doc Brown's user avatar
  • 220k
25 votes

How do unit tests facilitate refactoring without introducing regressions?

There is a lot to digest in this question. There appear to be some misconceptions about unit tests, what exactly constitutes a "regression", and how much unit test code you should expect to ...
Greg Burghardt's user avatar
24 votes

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

Is it still okay or good idea to stop most of the development and start writing whole possible test cases from the beginning [...] ? Given legacy1 code, write unit tests in these situations: when ...
Nick Alexeev's user avatar
  • 2,532
24 votes

How do I really write tests without mocking/stubbing?

How do I really write tests without mocking/stubbing? You design your code such that it can be tested without mocking and stubbing. That's one of the important, if perhaps subtle, ideas behind TDD: ...
VoiceOfUnreason's user avatar
23 votes

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

If the method is complex enough to need testing, it should be public in some class. So you refactor from: public class X { private int complexity(...) { ... } public void somethingElse() { ...
kevin cline's user avatar
  • 33.9k
23 votes

How do unit tests facilitate refactoring without introducing regressions?

You're running into the classicists vs mockists debate or the sociable tests vs solitary tests debate. I may also suggest that the pyramid of tests may not be the only way to think about tests. Brian ...
Thomas Owens's user avatar
  • 85.9k
22 votes

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

This could be seen as two steps: first you are going to create a new public class Clamper (without changing HPContainer). This is actually not a refactoring, and when applying TDD strictly, literally ...
Doc Brown's user avatar
  • 220k
21 votes

Testing - In-Memory DB vs Mocking

Mocking is the ideal solution for unit tests, and it may also be used for integration tests to improve speed, but it doesn't provide the same level of confidence as when you use an in-memory database. ...
Samuel's user avatar
  • 9,247

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