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. ...
Doc Brown's user avatar
  • 203k
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
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. ...
David Arno's user avatar
  • 39.1k
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 - ...
Phill  W.'s user avatar
  • 12.1k
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 ...
Oded's user avatar
  • 53.5k
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 ...
esoterik's user avatar
  • 3,899
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 ...
Kilian Foth's user avatar
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
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. ...
whatsisname's user avatar
  • 27.7k
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 ...
candied_orange's user avatar
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 ...
Esben Skov Pedersen's user avatar
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,...
RubberChickenLeader's user avatar
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 ...
Jeff Bowman's user avatar
  • 1,890
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 ...
David Arno's user avatar
  • 39.1k
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.3k
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. ...
candied_orange's user avatar
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 ...
Bart van Ingen Schenau's user avatar
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 ...
l0b0's user avatar
  • 11.3k
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 ...
Aki K's user avatar
  • 1,123
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 ...
Telastyn's user avatar
  • 109k
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 ...
Telastyn's user avatar
  • 109k
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 ...
JacquesB's user avatar
  • 57.8k
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 ...
Arseni Mourzenko's user avatar
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 ...
D Stanley's user avatar
  • 1,185
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
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 ...
Philipp's user avatar
  • 23.3k
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
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 ...
VoiceOfUnreason's user avatar
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 ...
Eternal21's user avatar
  • 1,584
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 ...
Kilian Foth's user avatar

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