288
votes
Accepted
Why are unit tests failing seen as bad?
This question contains IMHO several misconceptions, but the main one I would like to focus on is that it does not differentiate between local development branches, trunk, staging or release branches.
...
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....
241
votes
Accepted
Is it bad practice to enforce an execution order for unit tests?
I can not work with dummy data, as the main functionality I am testing is the connection to a black box remote server, which only gets the data from the first submodule.
This is the key part for me. ...
235
votes
Why are unit tests failing seen as bad?
... all unit tests passing in green - which is supposed to be good.
It is good. No "supposed to be" about it.
It promotes the idea that code should be perfect and no bugs should exist - ...
233
votes
Accepted
What's the point of running unit tests on a CI server?
Surely, by the time something gets committed to master, a developer has already run all the unit tests before and fixed any errors that might've occurred with their new code.
Or not. There can be ...
216
votes
Is it normal to spend as much, if not more, time writing tests than actual code?
I remember from a software engineering course, that one spends ~10% of development time writing new code, and the other 90% is debugging, testing, and documentation.
Since unit-tests capture the ...
203
votes
Accepted
Should we design our code from the beginning to enable unit testing?
Reluctance to modify code for the sake of testing shows that a developer hasn't understood the role of tests, and by implication, their own role in the organization.
The software business revolves ...
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 ...
184
votes
Accepted
Shouldn't unit tests use my own methods?
The spirit of his claim is indeed correct. The point of unit tests is to isolate code, test it free of dependencies, so that any erroneous behavior can be quickly recognized where it is happening.
...
158
votes
Accepted
Time difference between developing with unit tests vs no tests
The later you test, the more it costs to write tests.
The longer a bug lives, the more expensive it is to fix.
The law of diminishing returns ensures you can test yourself into oblivion trying to ...
156
votes
If two individual branches pass unit tests, once they're merged, is the result also guaranteed to pass unit tests?
No.
The simplest example I've seen is: branch A cleans unused imports in a file. Branch B adds code that actually uses some of the unused imports. Git merges automatically since the lines that were ...
154
votes
Is it considered 'bad practice' to check file contents/encoding in unit tests?
Most likely the tests you wrote are closer to integration or regression tests than unit tests. While the line can be very fuzzy and sometimes devolves into pedantry over what is or is not a unit test,...
146
votes
Accepted
Are (database) integration tests bad?
Write the smallest useful test you can. For this particular case, an in-memory database might help with that.
It is generally true that everything that can be unit-tested should be unit-tested, and ...
126
votes
How do you detect dependency problems with unit tests when you use mock objects?
When you write unit tests for A, you mock X
Do you? I don't, unless I absolutely have to. I have to if:
X is slow, or
X has side effects
If neither of these apply, then my unit tests of A will test ...
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 ...
121
votes
What is the point of unit tests?
It's easy to write tests that have no value.
Especially if your employer mandates them. "You wanted tests. Here's your tests." If that's your criteria for tests then you're exactly right. ...
119
votes
Would unit tests have helped Citigroup to avoid this expensive mistake?
Unit tests could have caught that the branch codes 10B and 10C were incorrectly classified as "testing branches", but I find it unlikely that the tests for that branch classification would have been ...
118
votes
Accepted
Code coverage highlights unused methods - what should I do?
Delete.
Commit.
Forget.
Rationale:
Dead code is dead. By its very description it has no purpose. It may have had a purpose at one point, but that is gone, and so the code should be gone.
Version ...
116
votes
Time difference between developing with unit tests vs no tests
I agree with the rest of the answers but to answer the what is the time difference question directly.
Roy Osherove in his book The Art of Unit Testing, Second Edition page 200 did a case study of ...
114
votes
If two individual branches pass unit tests, once they're merged, is the result also guaranteed to pass unit tests?
No. As a counter example, consider branch A adds a unit test that uses reflection to check for a misspelling in an enum. And branch B adds a misspelling. Both pass because a misspelling doesn’t fail a ...
103
votes
How do unit tests facilitate design?
The great thing about unit tests is they allow you to use your code how other programmers will use your code.
If your code is awkward to unit test, then it's probably going to be awkward to use. If ...
102
votes
Should there be unit tests for complex regular expressions?
Testing dogmatism aside, the real question is whether it provides value to unit test complex regular expressions. It seems pretty clear that it does provide value (regardless of whether the regex is ...
99
votes
Is it normal to spend as much, if not more, time writing tests than actual code?
It is.
Even if you take just unit testing, it is not unusual to have more code within the tests than the actually tested code. There is nothing wrong with it.
Consider a simple code:
public void ...
93
votes
Are (database) integration tests bad?
One of my co-workers maintains that integration tests are all kinds of bad and wrong - everything must be unit-tested,
That's a little like saying that antibiotics are bad - everything should be ...
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 ...
81
votes
Are magic numbers acceptable in unit tests if the numbers don't mean anything?
When do you really have numbers which have no meaning at all?
Usually, when the numbers have any meaning, you should assign them to local variables of the test method to make the code more readable ...
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 ...
80
votes
Accepted
How exactly should unit tests be written without mocking extensively?
the point of unit tests is to test units of code in isolation.
Martin Fowler on Unit Test
Unit testing is often talked about in software development, and is a term that I've been familiar with ...
79
votes
How do you detect dependency problems with unit tests when you use mock objects?
You need both. Unit tests to verify behavior of each of your units, and a few integration tests to make sure they're connected correctly. The problem with relying only on integration testing is ...
78
votes
Accepted
Are integration tests meant to repeat all unit tests?
No, integration tests should not just duplicate the coverage of unit tests. They may duplicate some coverage, but that's not the point.
The point of a unit test is to ensure that a specific small bit ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
unit-testing × 1755testing × 463
tdd × 345
c# × 186
integration-tests × 145
mocking × 135
java × 113
design × 87
python × 66
c++ × 59
object-oriented × 53
dependency-injection × 52
.net × 50
javascript × 48
programming-practices × 47
php × 45
refactoring × 42
continuous-integration × 42
design-patterns × 38
test-coverage × 36
bdd × 35
database × 28
integration-testing × 28
architecture × 27
code-quality × 24