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
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....
MattDavey's user avatar
  • 7,106
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
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
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. ...
Mag20's user avatar
  • 3,291
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
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 ...
user2954463's user avatar
  • 1,460
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
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 ...
mindplay.dk's user avatar
  • 1,687
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
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
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
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
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 ...
JustinLovinger'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 ...
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 ...
Edwardo's user avatar
  • 937
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 ...
Alex Lomia's user avatar
  • 1,301
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 ...
WannabeCoder's user avatar
  • 2,794
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 ...
Calin Leafshade's user avatar
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
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 ...
bvgheluwe's user avatar
  • 1,177
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 ...
user619818's user avatar
  • 1,757
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 ...
Lee's user avatar
  • 1,101
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
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, ...
Chris Devereux's user avatar
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 ...
Lamin Sanneh's user avatar
  • 4,005
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 ...
Ali Rasim Kocal's user avatar
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
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 ...
Matt Evans's user avatar
  • 1,015
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 ...
Paradoxis's user avatar
  • 1,351
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 ...
carlossierra's user avatar
  • 1,405
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
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 ...
is4's user avatar
  • 779
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 ...
Paul's user avatar
  • 1,025
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
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
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
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 ...
Yggdrasil's user avatar
  • 908
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. ...
Tom Squires's user avatar
  • 17.7k
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 ...
Vinicius Scheidegger's user avatar
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) ...
Kevin's user avatar
  • 2,658
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 ...
Jens's user avatar
  • 739
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-...
Songo's user avatar
  • 6,558
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 ...
Lucas T's user avatar
  • 753
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 ...
CKing's user avatar
  • 1,012
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 ...
kentrid's user avatar
  • 721
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 ...
PDStat's user avatar
  • 783

1
2 3 4 5
36