Questions tagged [unit-testing]
Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.
1,755
questions
531
votes
18
answers
469k
views
Is it OK to have multiple asserts in a single unit test?
In the comment to this great post, Roy Osherove mentioned the OAPT project that is designed to run each assert in a single test.
The following is written on the project's home page:
Proper unit ...
422
votes
12
answers
19k
views
Should I intentionally break the build when a bug is found in production?
It seems reasonable to me that if a serious bug is found in production by end-users, a failing unit test should be added to cover that bug, thus intentionally breaking the build until the bug is fixed....
297
votes
14
answers
444k
views
How do you unit test private methods?
I am working on a java project. I am new to unit testing. What is the best way to unit test private methods in java classes?
237
votes
9
answers
35k
views
Is it normal to spend as much, if not more, time writing tests than actual code?
I find tests a lot trickier and harder to write than the actual code they are testing. It's not unusual for me to spend more time writing the test than the code it is testing.
Is that normal or am I ...
184
votes
16
answers
21k
views
Why does automated testing keep failing in my company?
We have tried to introduce developer automated testing several times at my company. Our QA team uses Selenium to automate UI tests, but I always wanted to introduce unit tests and integration tests. ...
173
votes
11
answers
96k
views
How should I test randomness?
Consider a method to randomly shuffle elements in an array. How would you write a simple yet robust unit test to make sure that this is working?
I've come up with two ideas, both of which have ...
163
votes
13
answers
36k
views
Is there such a thing as having too many unit tests?
I have been tasked with writing unit tests for an existing application. After finishing my first file, I have 717 lines of test code for 419 lines of original code.
Is this ratio going to become ...
157
votes
15
answers
26k
views
When is it appropriate to not unit test?
I work in a small company as a solo developer. I'm the only developer at the company in fact. I have several (relatively) large projects I've written and maintain regularly, and none of them have ...
144
votes
11
answers
55k
views
Are (database) integration tests bad?
Some people maintain that integration tests are all kinds of bad and wrong - everything must be unit-tested, which means you have to mock dependencies; an option which, for various reasons, I'm not ...
142
votes
12
answers
13k
views
TDD vs. Productivity
In my current project (a game, in C++), I decided that I would use Test Driven Development 100% during development.
In terms of code quality, this has been great. My code has never been so well ...
141
votes
7
answers
61k
views
What should you test with unit tests?
I'm freshly out of college, and starting university somewhere next week. We've seen unit tests, but we kinda not used them much; and everyone talks about them, so I figured maybe I should do some.
...
140
votes
13
answers
23k
views
Time difference between developing with unit tests vs no tests
I'm a solo developer with a pretty time-constrained work environment where development time ranges usually from 1-4 weeks per project, depending on either requirements, urgency, or both. At any given ...
133
votes
13
answers
43k
views
(Why) is it important that a unit test not test dependencies?
I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on ...
131
votes
15
answers
24k
views
How do you write unit tests for code with difficult to predict results?
I frequently work with very numeric / mathematical programs, where the exact result of a function is difficult to predict in advance.
In trying to apply TDD with this kind of code, I often find ...
123
votes
16
answers
63k
views
When is unit testing inappropriate or unnecessary? [duplicate]
It seems to be generally assumed (on Stack Overflow at least) that there should always be unit tests, and they should be kept up to date. But I suspect the programmers making these assertions ...
120
votes
19
answers
13k
views
What is the point of unit tests?
I've been a software developer for 20+ years, although it's been an unusual career. I've mostly worked on either legacy projects, or small standalone, non-public-facing apps and so only a couple of ...
120
votes
8
answers
47k
views
How exactly should unit tests be written without mocking extensively?
As I understand, the point of unit tests is to test units of code in isolation. This means, that:
They should not break by any unrelated code change elsewhere in the codebase.
Only one unit test ...
110
votes
12
answers
18k
views
Is testable code better code?
I'm attempting to get into the habit of writing unit tests regularly with my code, but I've read that first it's important to write testable code.
This question touches on SOLID principles of writing ...
105
votes
9
answers
20k
views
What's the point of running unit tests on a CI server?
Why would you run 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 ...
104
votes
10
answers
12k
views
Where is the line between unit testing application logic and distrusting language constructs?
Consider a function like this:
function savePeople(dataStore, people) {
people.forEach(person => dataStore.savePerson(person));
}
It might be used like this:
myDataStore = new Store('some ...
101
votes
11
answers
18k
views
How do you detect dependency problems with unit tests when you use mock objects?
You have a class X and you write some unit tests that verify behaviour X1.
There's also class A which takes X as a dependency.
When you write unit tests for A, you mock X. In other words, while unit ...
98
votes
18
answers
20k
views
Are unit tests really that useful? [closed]
I just graduated with a degree in CS and I currently have a job as a Junior .NET Developer (C#, ASP.NET, and web forms). Back when I was still in university, the subject of unit testing did get ...
96
votes
19
answers
33k
views
Why are unit tests failing seen as bad?
In some organisations, apparently, part of the software release process is to use unit testing, but at any point in time all unit tests must pass. Eg there might be some screen which shows all unit ...
95
votes
15
answers
18k
views
Should we design our code from the beginning to enable unit testing?
There's a debate going on in our team at the moment as to whether modifying code design to allow unit testing is a code smell, or to what extent it can be done without being a code smell. This has ...
93
votes
11
answers
85k
views
Is static universally "evil" for unit testing and if so why does Resharper recommend it? [closed]
I have found that there are only 3 ways to unit test (mock/stub) dependencies that are static in C#.NET:
Moles
TypeMock
JustMock
Given that two of these are not free and one has not hit release 1.0, ...
93
votes
12
answers
11k
views
Is there a reason that tests aren't written inline with the code that they test?
I've been reading a bit about Literate Programming recently, and it got me thinking... Well-written tests, especially BDD-style specs can do a better job at explaining what code does than prose does, ...
93
votes
10
answers
12k
views
How to motivate co-workers to write unit-tests? [closed]
We're working on a large product which has been in production for about 5 years. The codebase is.. erm.. working. Not really well but it is working. New features are thrown into production and tested ...
90
votes
12
answers
9k
views
How big does my project need to be for me to unit test it? [closed]
I assume that my project is decoupled enough to allow for unit testing. But how big, exactly, in terms of clases and functions does my project need to be to make unit testing worthwhile?
We all make ...
90
votes
6
answers
20k
views
Is it bad practice to enforce an execution order for unit tests?
I am writing tests for a project that consists of multiple submodules. Each test case that I have written runs independent of each other and I clear all data between tests.
Even though the tests run ...
88
votes
11
answers
17k
views
Is it a bad practice to modify code strictly for testing purposes
I have a debate with a programmer colleague about whether it is a good or bad practice to modify a working piece of code only to make it testable (via unit tests for example).
My opinion is that it ...
86
votes
11
answers
12k
views
Would unit tests have helped Citigroup to avoid this expensive mistake?
I read about this snafu: Programming bug costs Citigroup $7m after legit transactions mistaken for test data for 15 years.
When the system was introduced in the mid-1990s, the program code filtered ...
85
votes
12
answers
15k
views
Is it considered 'bad practice' to check file contents/encoding in unit tests?
A bit of context: Earlier today I had to update some SQL code that another colleague of mine provided, and since it’s a pretty large script, it’s stored as a separate file (which is then read and ...
85
votes
11
answers
12k
views
Shouldn't unit tests use my own methods?
Today I was watching a "JUnit basics" video and the author said that when testing a given method in your program, you shouldn't use other of your own methods in the process.
To be more specific, he ...
79
votes
10
answers
35k
views
Should we test all our methods?
So today I had a talk with my teammate about unit testing. The whole thing started when he asked me "hey, where are the tests for that class, I see only one?". The whole class was a manager (or a ...
75
votes
10
answers
8k
views
Does it make sense to write tests for legacy code when there is no time for a complete refactoring?
I usually try to follow the advice of the book Working Effectively with Legacy Code. I break dependencies, move parts of the code to @VisibleForTesting public static methods and to new classes to make ...
75
votes
7
answers
20k
views
Writing Tests for Existing Code
Suppose one had a relatively large program (say 900k SLOC in C#), all commented/documented thoroughly, well organized and working well. The entire code base was written by a single senior developer ...
74
votes
6
answers
28k
views
Is there a point to unit tests that stub and mock everything public?
When doing unit tests the "proper" way, i.e. stubbing every public call and return preset values or mocks, I feel like I'm not actually testing anything. I'm literally looking at my code and creating ...
73
votes
7
answers
65k
views
Difference Between Unit Testing and Test Driven Development
From reading the descriptions, I understand that in TDD tests are done prior to writing the function and in Unit Testing, its done afterwards.
Is this the main difference, or the two terms can't even ...
71
votes
5
answers
14k
views
Do I need unit test if I already have integration test?
If I already have integration test for my program, and they all passed, then I have a good feel that it will work. Then what are the reasons to write/add unit tests? Since I already have to write ...
69
votes
7
answers
18k
views
Is it sufficient to use acceptance and integration tests instead of unit test?
Short introduction to this question. I have used now TDD and lately BDD for over one year now. I use techniques like mocking to make writing my tests more efficiently. Lately I have started a ...
67
votes
7
answers
48k
views
Is dependency injection essential for unit testing?
Is using dependency injection (DI) essential for unit testing?
I can't think of another alternative for isolating code so it can be tested. Also, all the examples I have ever seen use this pattern. ...
64
votes
5
answers
18k
views
Is the usage of random values in unit testing a good practice? [duplicate]
Having worked in complex solutions that had Unit Tests and Integration Test in the CI/CD pipeline, I recall having a tough time with tests that failed randomly (either due to random values being ...
63
votes
11
answers
15k
views
Are magic numbers acceptable in unit tests if the numbers don't mean anything?
In my unit tests, I often throw arbitrary values at my code to see what it does. For example, if I know that foo(1, 2, 3) is supposed to return 17, I might write this:
assertEqual(foo(1, 2, 3), 17)
...
63
votes
9
answers
23k
views
How to write "good" unit tests?
Triggered by this thread, I (again) am thinking about finally using unit tests in my projects. A few posters there say something like "Tests are cool, if they are good tests". My question now: What ...
62
votes
4
answers
21k
views
TDD - Outside In vs Inside Out
What is the difference between building an application Outside In vs building it Inside Out using TDD?
These are the books I read about TDD and unit testing:
Test Driven Development: By Example
Test-...
61
votes
16
answers
8k
views
Why not write all tests at once when doing TDD?
The Red - Green - Refactor cycle for TDD is well established and accepted. We write one failing unit test and make it pass as simply as possible. What are the benefits to this approach over writing ...
60
votes
7
answers
16k
views
Code coverage highlights unused methods - what should I do?
I have been tasked to increase code coverage of an existing Java project.
I noticed that the code coverage tool (EclEmma) has highlighted some methods that are never called from anywhere.
My initial ...
60
votes
5
answers
35k
views
What are the design principles that promote testable code? (designing testable code vs driving design through tests)
Most of the projects that I work on consider development and unit testing in isolation which makes writing unit tests at a later instance a nightmare. My objective is to keep testing in mind during ...
58
votes
11
answers
15k
views
If two individual branches pass unit tests, once they're merged, is the result also guaranteed to pass unit tests?
Suppose we have two branches A and B which have been forked from master.
Both branches A and B make some changes and implement some unit tests. They pass all current and new tests, then are merged ...
58
votes
9
answers
10k
views
How to write unit tests before refactoring?
I've read some answers to questions along a similar line such as "How do you keep your unit tests working when refactoring?". In my case the scenario is slightly different in that
I've been given a ...