Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.

learn more… | top users | synonyms (1)

69
votes
13answers
7k 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 ...
66
votes
16answers
9k 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 ...
8
votes
5answers
856 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 ...
59
votes
10answers
31k 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?
18
votes
6answers
1k views

How to explain the value of unit testing

I want to introduce the concept of unit tests (and testing in general) to my co-workers; right now there are no tests at all and things are tested by actually performing the tasks via the UI to see ...
12
votes
2answers
555 views

Software Testing Techniques [closed]

What kinds of software testing do you know? I've heard about Test-Driven Development, Unit tests etc, but can't understand their importance and difference. For example, why are we using regression ...
8
votes
4answers
435 views

Staying OO and Testable while working with a database

What are some OOP strategies for working with a database but keeping things unit testable? Say I have a User class and my production environment works against MySQL. I see a couple possible ...
29
votes
5answers
2k 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 ...
5
votes
1answer
584 views

Is wrapping a third party code the only solution to unit test its consumers?

I'm doing unit testing and in one of my classes I need to send a mail from one of the methods, so using constructor injection I inject an instance of Zend_Mail class which is in Zend framework. Now ...
45
votes
10answers
5k 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 ...
34
votes
10answers
4k 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 ...
20
votes
7answers
4k views

Automated unit testing, integration testing or acceptance testing [closed]

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 ...
39
votes
8answers
15k 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 ...
20
votes
4answers
1k views

Unit testing best practices for a unit testing newbie

In recent years, I have only written small components for people in larger projects or small tools. I have never written a unit test and it always seems like learning how to write them and actually ...
26
votes
5answers
3k views

Why is it often said that the test cases need to be made before we start coding? [duplicate]

Why is it often said that the test cases need to be made before we start coding? What are its pros and what the cons if we don't listen to this advice? Moreover, does that advice refer to black box ...
11
votes
5answers
2k views

What is a normal “functional lines of code” to “test lines of code” ratio?

I'm pretty new to TDD approach and my first experiments say that writing 1 line of functional code means writing about 2-3 lines of testing code. So, in case I'm going to write 1000 LOC, the whole ...
0
votes
2answers
91 views

How should I test the functionality of a function that uses other functions in it? [duplicate]

Suppose there is a function get-data which returns a map of information about the id of the user passed in. now this function uses 3 functions source-a, source-b and source-c to get three different ...
83
votes
18answers
11k 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 ...
94
votes
10answers
3k 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 ...
93
votes
10answers
25k 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 ...
79
votes
12answers
6k 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 ...
21
votes
8answers
2k views

Are “TDD Tests” different to Unit Tests?

I read Stephen Walther's article about Test Driven Development (TDD) and unit testing. I think it was an excellent article. The author makes a distinction between what he calls "TDD Tests" and unit ...
24
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 ...
14
votes
4answers
662 views

How to Quantify the Value of Unit Testing [duplicate]

Our organization is considering integrating unit testing into our software development workflow. I've heard lots of anecdotal stories about how it encourages better, easy to maintain, and well-planned ...
14
votes
5answers
1k views

Databases and Unit/Integration Testing

I have had a discussion with someone about unit/integration testing with web applications and I have a disagreement about 1 core idea. The issues is that the person I am talking to think that the ...
8
votes
6answers
1k views

Why did Java make package access default?

I'm asking this question because I believe they did it for a very good reason and that most people do not use it properly, well from my experience in industry so far anyway. But if my theory is true ...
6
votes
2answers
463 views

Unit test SHA256 wrapper queries

I have the following SHA256 wrapper. public static string SHA256(string plainText) { StringBuilder sb = new StringBuilder(); SHA256CryptoServiceProvider provider = new ...
62
votes
10answers
4k 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 ...
37
votes
4answers
782 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 ...
49
votes
16answers
3k 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 ...
29
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 ...
26
votes
7answers
3k 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. ...
46
votes
12answers
3k 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 ...
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 ...
11
votes
2answers
724 views

Adding unit tests to a legacy, plain C project

The title says it all. My company is reusing a legacy firmware project for a microcontroller device, written completely in plain C. There are parts which are obviously wrong and need changing, and ...
15
votes
8answers
761 views

What are good unit tests to cover the use case of rolling a die?

I'm trying to get to grips with unit testing. Say we have a die which can has a default number of sides equal to 6 (but can be 4, 5 sided etc.): import random class Die(): def __init__(self, ...
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 ...
11
votes
4answers
1k views

Should each unit test be able to be run independently of other tests?

Say you have tests for two methods of a class. The first method gathers data from another tier and puts it into some sort of storage independent of the runtime (such as a SQL table), so all of the ...
20
votes
7answers
784 views

Should I have unit tests for known defects?

If my code contains a known defect which should be fixed, but isn't yet, and won't be fixed for the current release, and might not be fixed in the forseeable future, should there be a failing unit ...
17
votes
11answers
1k views

At what point would you drop some of your principles of software development for the sake of more money?

I'd like to throw this question out there to interestingly see where the medium is. I'm going to admit that in my last 12 months, I picked up TDD and a lot of the Agile values in software ...
15
votes
5answers
1k views

How granular should TDD tests be?

During TDD training based on medical software case we are implementing the following story: "When user presses Save button, system should add patient, add device and add device data records". The ...
13
votes
8answers
605 views

How have you made unit testing more enjoyable?

If you've always loved unit testing, good for you! But for the unfortunate ones who weren't born with a liking for it, how have you managed to make this task more enjoyable ? This is not a "what is ...
8
votes
3answers
702 views

What should be tested in Javascript?

At work, we've just started on a heavily Javascript based application (actually using Coffeescript, but still), of which I've been implementing an automated test system using JsTestDriver and fabric. ...
26
votes
11answers
2k 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 ...
20
votes
8answers
1k views

What are the disadvantages of writing code before writing unit tests?

I have always seen the recommendation that we should first write unit tests and then start writing code. But I feel that going the other way is much more comfortable (for me) - write code and then the ...
18
votes
6answers
779 views

Does adding unit tests make sense for well-known legacy code?

I'm talking about unit tests in the TDD sense. (Not automated "integration", or what you like to call it tests.) Legacy code as in: (C++) code without tests. (see: Michael Feathers' Working ...
8
votes
3answers
608 views

Relative value of manual vs automated testing

The organisation I work for has recently employed a testing officer to run manual tests, but when I asked about being given time as a developer to write unit tests the response was that the manual ...
3
votes
2answers
343 views

Term for unit testing that separates test logic from test result data

So I'm not doing any unit testing. But I've had an idea to make it more appropriate for my field of use. Yet it's not clear if something like this exists, and if, how it would possibly be called. ...
10
votes
5answers
2k views

Would you rather make private stuff internal/public for tests, or use some kind of hack like PrivateObject?

I am quite a beginner in code testing, and was an assert whore before. One thing worrying me in unit testing is that is often requires you to make public (or at least internal) fields that would have ...
9
votes
3answers
675 views

Ramping Up On Legacy Code

When starting to work on a project with an existing code base, the first thing that needs to be done is usually to understand the application & existing code. Let's assume that the existing code ...