Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.
312
votes
12answers
7k 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 ...
125
votes
23answers
6k 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. ...
77
votes
10answers
2k 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 ...
74
votes
18answers
7k 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 ...
74
votes
12answers
6k views
How big does my project need to be for me to unit test it?
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 ...
68
votes
15answers
5k views
How to motivate co-workers to write unit-tests?
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 ...
62
votes
10answers
5k 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, ...
61
votes
16answers
5k 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 ...
56
votes
8answers
6k views
Is it OK to have multiple asserts in a single unit test?
In the comment to this great post Roy Osherove pointed to the OAPT project that is designed to run each assert in a single test.
This is written on projects home page:
Proper unit tests should ...
50
votes
13answers
4k 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 ...
45
votes
17answers
2k 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 ...
43
votes
12answers
2k views
If your unit test code “smells” does it really matter?
Usually I just throw my unit tests together using copy and paste and all kind of other bad practices. The unit tests usually end up looking quite ugly, they're full of "code smell," but does this ...
39
votes
10answers
3k 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 ...
37
votes
6answers
2k views
Should unit tests be stored in the repository?
I'm a growing programmer who's finally putting unit testing into practice for a library that I'm storing on GitHub.
It occurred to me that I might include the test suites in the repo, but as I look ...
37
votes
11answers
5k views
What are the disadvantages of automated testing?
There are a number of questions on this site that give plenty of information about the benefits that can be gained from automated testing. But I didn't see anything that represented the other side of ...
36
votes
8answers
13k 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?
36
votes
9answers
1k views
How to test the tests?
We test our code to make it more correct (actually, less likely to be incorrect). However, the tests are also code -- they can also contain errors. And if your tests are buggy, they hardly make your ...
35
votes
10answers
3k 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 ...
35
votes
4answers
595 views
Determining what is a useful unit test
I've been going through phpunit's docs and came accross the following quote:
You can always write more tests. However, you will quickly find that only a fraction of the tests you can imagine are ...
31
votes
10answers
4k 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 ...
30
votes
9answers
2k views
Is unit testing or test-driven development worthwhile?
My team at work is moving to Scrum and other teams are starting to do test-driven development using unit tests and user acceptance tests. I like the UATs, but I'm not sold on unit testing for ...
30
votes
10answers
2k views
TDD - is it just about unit tests? [duplicate]
Do I understand it right that classical TDD is just about unit tests? Don't understand me wrong: I know the difference between TDD and just unit testing. I am asking whether it is correct to use ...
29
votes
8answers
9k views
Is static universally “evil” for unit testing and if so why does resharper recommend it?
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 ...
28
votes
5answers
2k views
Test driven development - convince me! [closed]
I know some people are massive proponents of test driven development. I have used unit tests in the past, but only to test operations that can be tested easily or which I believe will quite possibly ...
27
votes
7answers
1k views
Unit testing newbie team needs to unit test
I'm working with a new team that has historically not done ANY unit testing. My goal is for the team to eventually employ TDD (Test Driven Development) as their natural process. But since TDD is ...
27
votes
5answers
2k views
I want to start using TDD. Any tips for a beginner?
I never used an automated test mechanism in any of my projects and I feel I'm missing a lot. I want to improve myself, so I have to start tackling some issues I've been neglecting like this and trying ...
26
votes
6answers
2k views
How should you TDD a Yahtzee game?
Let's say you're writing a Yahtzee game TDD style. You want to test the part of the code that determines whether or not a set of five die rolls is a full house. As far as I know, when doing TDD, you ...
26
votes
12answers
2k views
Unit-testing of inherently random/non-deterministic algorithms
My current project, succinctly, involves the creation of "constrainably-random events". I'm basically generating a schedule of inspections. Some of them are based on strict schedule constraints; you ...
26
votes
2answers
1k views
How should C++ Unit Test code be organized for maximum Unit Test efficiency?
This question is not about Unit Testing Frameworks.
This question is not about writing Unit Tests.
This question is about where to put the UT code written and how/when/where to compile and run it.
...
25
votes
11answers
2k views
How do we make unit tests run fast?
We have reached the point in our project where we have almost a thousand tests and people have stopped bothering with running them before doing a check in because it takes so long. At best they run ...
25
votes
6answers
854 views
Unit and Integration testing: How can it become a reflex
All the programmers in my team are familiar with unit testing and integration testing. We have all worked with it. We have all written tests with it. Some of us even have felt an improved sense of ...
24
votes
11answers
1k views
Writing the minimum code to pass a unit test - without cheating!
When doing TDD and writing a unit test, how does one resist the urge to "cheat" when writing the first iteration of "implementation" code that you're testing?
For example:
Let's I need to calculate ...
24
votes
11answers
944 views
Unit testing methods with indeterminate output
I have a class that is meant to generate a random password of a length that's also random, but limited to be between a defined min and max length.
I'm constructing unit tests, and ran into an ...
23
votes
17answers
2k views
Should I demand unit-testing from programmers? [closed]
I work at a place where we buy a lot of IT projects. We are currently producing a standard for systems-requirements for the requisition of future projects. In that process, we are discussing whether ...
23
votes
6answers
2k 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. ...
23
votes
5answers
1k views
How do you convince management to “invest” in unit tests?
How did you convince your manager to let you unit test?
By "use", I mean being allowed to develop, check-in to source control and maintain the unit tests over time, etc.
Typical management ...
23
votes
1answer
460 views
How have Guava unit tests been generated automatically?
Guava has unit test cases automatically generated:
Guava has staggering numbers of unit tests: as of July 2012, the
guava-tests package includes over 286,000 individual test cases. Most
of ...
22
votes
12answers
1k views
Colleague unwilling to use unit tests “as it's more to code”
A colleague is unwilling to use unit tests and instead opting for a quick test, pass it to the users, and if all is well it is published live. Needless to say some bugs do get through.
I mentioned ...
22
votes
9answers
557 views
Is there any value in writing unit tests for code that already works when inheriting applications?
Obviously some old applications can't be or is extremely difficult to unit test because of the way it was written in the first place.
But in places, like some helper methods which could probably be ...
22
votes
7answers
1k 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.
...
21
votes
12answers
2k views
How does unit testing work?
I am trying to make my code more robust and I have been reading about unit testing, but I find it very hard to find an actual useful use.
For instance, the Wikipedia example:
public class TestAdder {
...
21
votes
7answers
2k views
Best book, articles and literature on unit testing [closed]
In my battle to introduce unit testing in my workgroup, I find a lot of people that have little to none knowledge of the concept. Can you suggest:
best articles or tutorials for quickly introducing ...
20
votes
11answers
2k views
How much Code Coverage is “enough”?
We are starting a push for code coverage here at my work, and it has got me to thinking.... How much code coverage is enough?
When do you get to the point of diminishing returns on code coverage? ...
20
votes
7answers
923 views
Quality of Code in unit tests?
When writing unit tests, is it worth spending the extra time to make the code have good quality and readability?
When writing tests I often break the Law of Demeter, for faster writing and to avoid ...
20
votes
11answers
3k views
Has test driven development (TDD) actually benefited a real world project?
I am not new to coding. I have been coding (seriously) for over 15 years now. I have always had some testing for my code. However, over the last few months I have been learning test driven ...
20
votes
10answers
689 views
Automated Testing: Explaining its Business Value
To start I don't think this is a repeat of other questions on unit testing. What I'm looking for help with is articulating its value to a team of programmers, analysts, managers and testers. By ...
20
votes
1answer
691 views
What is the effect of creating unit tests during development on time to develop as well as time spent in maintenance activities?
I'm a consultant and I am going to introduce unit tests to all developers at my client site. My goal is to ensure that all new applications should have unit tests for all classes created.
The client ...
20
votes
7answers
2k views
How do I unit test multi-threaded code?
Are there ways to unit test your multi-threaded code for race conditions and deadlocks?
To see if they are performing the way they should be...
19
votes
7answers
3k views
Automated unit testing, integration testing or acceptance testing
TDD and unit testing seems to be the big rave at the moment. But it is really that useful compared to other forms of automated testing?
Intuitively I would guess that automated integration testing is ...
18
votes
15answers
1k views
Do you use unit tests at work? What benefits do you get from them? [closed]
I had planned to study and apply unit testing to my code, but after talking with my colleagues, some of them suggested to me that it's not necessary and it has a very little benefit. They also claim ...