Hot answers tagged

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.2k
232 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
189 votes

Can we assume while testing software that a user wouldn't perform such silly actions on software?

You might not enter random values into fields of a web application, but there certainly people out there that do just that. Some people enter random by accident and others do it intentionally trying ...
Bart van Ingen Schenau'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
151 votes

How to avoid logical mistakes in code, when TDD didn't help?

Test driven development didn't help. It seems like it did help, its just that you didn't have a test for the "a day ago" scenario. Presumably, you added a test after this case was found; this is ...
esoterik's user avatar
  • 3,899
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.2k
123 votes
Accepted

Writing tests for code whose purpose I don't understand

You are doing fine! Creating automated regression tests is often the best thing you can do for making a component refactorable. It may be surprising, but such tests can often be written without the ...
Doc Brown's user avatar
  • 206k
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
115 votes

How to avoid logical mistakes in code, when TDD didn't help?

an event happening twenty six hours ago would be one day ago Tests won't help much if a problem is poorly defined. You're evidently mixing calendar days with days reckoned in hours. If you stick to ...
Kevin Krumwiede's user avatar
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
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
  • 58.7k
101 votes

Can we assume while testing software that a user wouldn't perform such silly actions on software?

Never Assume Anything You cannot assume that any user will not do something "dumb" with your software by accident or on-purpose. Users can accidentally press the wrong button, the cat can ...
Ben Cottrell's user avatar
  • 11.7k
81 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
78 votes

Is the usage of random values in unit testing a good practice?

Yes, I agree that randomness shouldn't be part of a testing suite. What you want is to mock any real randomness, to create deterministic tests. Even if you genuinely need bulk random data, more than ...
Flater's user avatar
  • 48.7k
77 votes

What's the point of running unit tests on a CI server?

As a developer who doesn't run all the integration and unit tests before making a commit to source control, I'll offer up my defense here. I would have to build, test and verify that an application ...
TZHX's user avatar
  • 5,072
74 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. In other words, while unit testing A, you set (postulate) the behaviour of X's mock to be X1. Time goes by, people do use your system, needs change, X ...
VoiceOfUnreason's user avatar
66 votes

How exactly should unit tests be written without mocking extensively?

How exactly should unit tests be written without mocking extensively? By minimising side-effects in your code. Taking your example code, if calculator for example talks to a web API, then either you ...
David Arno's user avatar
  • 39.2k
66 votes
Accepted

Company outsourced testing; how can we encourage programmers to stop overly relying on testers?

It seems to me there is a contradiction in policy here. On the one hand, the firm has outsourced testing because it consumed programmers' time excessively, and could be done more cheaply by others. ...
Steve's user avatar
  • 8,436
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
  • 48.7k
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

Can we assume while testing software that a user wouldn't perform such silly actions on software?

There are several factors to take in account. To illustrate those points, I'll use an example of a field where a user should enter a percentage in a context of a quota defined for a specific task in ...
Arseni Mourzenko's user avatar
58 votes
Accepted

How to avoid logical mistakes in code, when TDD didn't help?

These are the kinds of errors you typically find in the refactor step of red/green/refactor. Don't forget that step! Consider a refactor like the following (untested): def pluralize(num, unit): ...
Karl Bielefeldt's user avatar
57 votes

Dry running a function

You're doing multiple different things in that function. Start off by splitting it into separate functions. Here's an example using a closure for the "dry run" function. function ...
8bittree's user avatar
  • 5,656
52 votes

Is the usage of random values in unit testing a good practice?

I've worked on projects which use anywhere from no to extensive randomness in tests, and I'm generally in favour of it. The most important thing to remember is that the randomness must be repeatable. ...
l0b0's user avatar
  • 11.4k
51 votes

What are best practices for testing the absence of functionality

For a web application, this is actually easy. Presumably there is functionality to edit these objects (e.g. for maintainers or admins), and this functionality must somehow map to a system state change ...
Kilian Foth's user avatar
43 votes
Accepted

Should one test the values of an enum using unit tests?

If you have an enum with values only (no methods as one could do in Java), and this enum is part of the business definition of the system, should one write unit tests for it? No, they are just state. ...
VoiceOfUnreason's user avatar
39 votes
Accepted

Does integration testing use mocks?

If you have a piece of functionality that touches several external components, you might mock all but one to isolate and test a specific component. For example, suppose you have a function that calls ...
FrustratedWithFormsDesigner's user avatar
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

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