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.

Filter by
Sorted by
Tagged with
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 ...
Jonah's user avatar
  • 1,466
4 votes
2 answers
9k 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 ...
Amogh Talpallikar's user avatar
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?
Vinoth Kumar C M's user avatar
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 ...
Bryan Chen's user avatar
  • 1,065
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. ...
zneak's user avatar
  • 2,576
18 votes
4 answers
2k 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 approaches,...
Annika Backstrom's user avatar
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 ...
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 ...
Ken Pespisa's user avatar
  • 1,893
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 ...
dsimcha's user avatar
  • 17.3k
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 ...
Zenzen's user avatar
  • 996
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, ...
Vaccano's user avatar
  • 4,038
35 votes
5 answers
20k 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 ...
Aquarius_Girl's user avatar
531 votes
18 answers
470k 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 ...
10 votes
1 answer
13k views

Testing private methods as protected

I was reading this answer about testing private methods and it mentioned several possibilities: extract methods as public to another class make them public separate the env of test and production ...
sam's user avatar
  • 211
29 votes
5 answers
15k 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 ...
Zonko's user avatar
  • 775
27 votes
8 answers
7k views

How to Avoid Fragile Unit Tests?

We have written close to 3,000 tests -- data has been hard coded, very little reuse of code. This methodology has began to bite us. As the system changes we find ourselves spending more time fixing ...
Chuck Conway's user avatar
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 ...
Lamin Sanneh's user avatar
  • 4,005
27 votes
6 answers
7k 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 ...
ryanzec's user avatar
  • 2,757
22 votes
1 answer
2k views

Unit testing for a scientific computing library

I've had a bit of experience with unit testing before, in what I call (not pejoratively) the classic software engineering project: an MVC, with a user GUI, a database, business logic in the middle ...
Alejandro Piad's user avatar
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 ...
dlras2's user avatar
  • 2,310
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 ...
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 ...
liortal's user avatar
  • 1,175
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 ...
Shamim Hafiz - MSFT's user avatar
29 votes
6 answers
5k 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 ...
Wayne Molina's user avatar
  • 15.7k
17 votes
2 answers
1k views

Software Testing Techniques or Categories [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 ...
Dehumanizer's user avatar
  • 1,381
13 votes
1 answer
4k 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 ...
Songo's user avatar
  • 6,568
145 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 ...
mindplay.dk's user avatar
  • 1,697
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 ...
enthrops's user avatar
  • 843
15 votes
4 answers
2k views

Unit testing internal components

To what extent do you unit test internal/private components of a class/module/package/etc? Do you test them at all or do you just test the interface to the outside world? An example of these internal ...
imgx64's user avatar
  • 2,168
14 votes
4 answers
2k views

When should I use mock objects?

I've read a lot of things about TDD but I still have doubts. For example, I have these class diagrams: It's a simple example, just to learn about TDD and mock objects. Which test should I write ...
user avatar
58 votes
12 answers
6k 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 ...
Ryszard Szopa's user avatar
48 votes
9 answers
10k 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 test-...
Owen Johnson's user avatar
21 votes
5 answers
12k views

How should I unit test mathematical formulae? [duplicate]

I have a number of functions that are pretty close to the mathematical definition of a function. For example, a simplified version of one of these functions may look like: int function foo(int a, int ...
Evan's user avatar
  • 1,265
11 votes
2 answers
3k views

Separate code coverage reports for unit and integration tests, or one report for both?

Should there be a separate code coverage report for unit and integration tests, or one code coverage report for both? The thinking behind this is that code coverage allows us to make sure that our ...
Marios's user avatar
  • 303
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 ...
springloaded's user avatar
  • 2,123
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 ...
Revenant's user avatar
  • 1,395
55 votes
3 answers
45k views

Is it actually worth unit-testing an API client?

This is something that's been troubling me for a while now. Is it actually worth unit-testing an API client? Let's say you're creating a small class to abstract-away the calls to a petshop REST API. ...
Phillip B Oldham's user avatar
51 votes
4 answers
5k 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 ...
zcourts's user avatar
  • 601
46 votes
5 answers
5k 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 ...
louisgab's user avatar
  • 876
42 votes
7 answers
18k views

Is using "new" in the constructor always bad?

I have read that using "new" in a constructor (for any other objects than simple value ones) is bad practice as it makes unit testing impossible (as then those collaborators needs to be created too ...
Ezoela Vacca's user avatar
35 votes
6 answers
14k 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 ...
Andrey Agibalov's user avatar
31 votes
7 answers
13k 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 ...
Bjarke Freund-Hansen's user avatar
30 votes
4 answers
3k 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 ...
wilhil's user avatar
  • 992
28 votes
4 answers
20k 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 ...
Morgan Herlocker's user avatar
24 votes
4 answers
22k views

How to use unit tests when using BDD?

I am trying to understand BDD. I've read some articles and as I understood BDD is "the next step" from TDD. I say that because I find both to be very similar, and as I could read in this ...
JSBach's user avatar
  • 1,375
15 votes
4 answers
2k 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 ...
Adam's user avatar
  • 271
14 votes
4 answers
1k views

What are best practices for testing programs with stochastic behavior?

Doing R&D work, I often find myself writing programs that have some large degree of randomness in their behavior. For example, when I work in Genetic Programming, I often write programs that ...
John Doucette's user avatar
12 votes
4 answers
1k views

Gradual approaches to dependency injection

I'm working on making my classes unit-testable, using dependency injection. But some of these classes have a lot of clients, and I'm not ready to refactor all of them to start passing in the ...
JW01's user avatar
  • 1,011
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....
MattDavey's user avatar
  • 7,106
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 ...
Nairou's user avatar
  • 1,529

1
2 3 4 5 6