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.

A unit is the smallest testable part of an application. In procedural programming, a unit may be an individual function or procedure. Unit tests are created by programmers or occasionally by white box testers.

Further Reading

1744 questions
530
votes
18 answers

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 tests should fail for exactly one reason, that’s…
Restuta
  • 437
  • 3
  • 6
  • 14
421
votes
12 answers

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. My rationale for this is that the build should…
MattDavey
  • 7,096
  • 3
  • 31
  • 32
293
votes
14 answers

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?
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
237
votes
9 answers

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 doing something wrong? The questions “Is unit…
springloaded
  • 2,123
  • 3
  • 11
  • 10
183
votes
16 answers

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. In the past, each time we tried it, everyone got…
Mag20
  • 3,281
  • 2
  • 23
  • 21
171
votes
11 answers

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 noticeable flaws: Shuffle the array, then make sure its…
dlras2
  • 2,290
  • 2
  • 17
  • 16
161
votes
13 answers

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 unmanageable as we increase our code coverage? My…
user2954463
  • 1,440
  • 2
  • 10
  • 8
155
votes
15 answers

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 tests to support them. As I begin new projects I…
Ken Pespisa
  • 1,873
  • 2
  • 12
  • 11
143
votes
11 answers

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 always fond of. I find that, in some cases, a…
mindplay.dk
  • 1,677
  • 2
  • 11
  • 13
141
votes
12 answers

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 designed or so bug-free. I don't cringe when viewing…
Nairou
  • 1,519
  • 2
  • 10
  • 4
140
votes
13 answers

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 time I handle around 3-4 projects, some having…
Revenant
  • 1,395
  • 2
  • 11
  • 16
139
votes
7 answers

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. The problem is, I don't know what to test. Should I…
zneak
  • 2,556
  • 2
  • 23
  • 24
131
votes
13 answers

(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 StackOverflow emphasize testing only a unit, not its…
dsimcha
  • 17,224
  • 9
  • 64
  • 81
130
votes
15 answers

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 writing the code under test significantly easier than…
JustinLovinger
  • 1,179
  • 2
  • 7
  • 11
123
votes
16 answers

When is unit testing inappropriate or unnecessary?

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 work on different kinds of projects from me - they work…
Steve Bennett
  • 3,419
  • 4
  • 20
  • 24
1
2 3
99 100