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

1770 questions
96
votes
19 answers

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 tests passing in green - which is supposed to be…
user619818
  • 1,767
  • 2
  • 14
  • 23
86
votes
12 answers

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 executed at runtime). While doing this I accidentally…
Paradoxis
  • 1,361
85
votes
11 answers

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 out any transactions that were given three-digit…
Matt Evans
  • 1,015
63
votes
11 answers

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) These numbers are purely arbitrary and have no…
Kevin
  • 2,658
62
votes
9 answers

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 are "good" tests? In my applications, the main part…
Jens
  • 729
51
votes
4 answers

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 actually useful. What you want is to write tests that…
zcourts
  • 601
38
votes
7 answers

Is constructing objects with null parameters in unit tests OK?

I started writing some unit tests for my current project. I don't really have experience with it though. I first want to completely "get it", so I am currently using neither my IoC framework nor a mocking library. I was wondering if there is…
R. Schmitz
  • 2,608
37
votes
5 answers

Should you hard code your data across all unit tests?

Most unit testing tutorials/examples out there usually involve defining the data to be tested for each individual test. I guess this is part of the "everything should be tested in isolation" theory. However I've found that when dealing with…
mattdwen
  • 483
30
votes
9 answers

Unit testing multiple conditions in an IF statement

I have a chunk of code that looks something like this: function bool PassesBusinessRules() { bool meetsBusinessRules = false; if (PassesBusinessRule1 && PassesBusinessRule2 && PassesBusinessRule3) { …
bwalk2895
  • 1,988
29
votes
9 answers

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 unit tested, should I bother writing unit tests for…
RoboShop
  • 2,780
  • 6
  • 30
  • 38
29
votes
6 answers

Is there any point in unit testing a client service that simply passes through data from the server?

Imagine a simple AngularJS REST Service which retrieves (GET) data from REST endpoints on a server. It maintains no state of its own and each method only passes back a promise to whomever is using said service. Now, should I write tests for this…
mccainz
  • 439
26
votes
17 answers

Should I demand unit-testing from programmers?

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 or not we can demand automated unit testing from…
Morten
  • 903
25
votes
3 answers

Why would you write unit-tests for controllers?

To me this is a totally irrelevant unit-test and I don't understand why someone would spent time writing it, since there is very little value to gain from it. I would know perfectly well if this controller returned the wanted type by executing the…
frostings
  • 660
24
votes
4 answers

How do I write unit tests for robots (and other mechanical devices)?

I'm a member of my high school's robotics club, and am responsible for programming the robot. One suggestion I keep hearing from various adults is that I should be writing unit tests to help validate my code. The code base is getting a bit big,…
Michael0x2a
  • 1,119
  • 1
  • 7
  • 23
23
votes
10 answers

Is unit testing development or testing?

I had a discussion with a testing manager about the role of unit and integration testing. She requested that developers report what they have unit and integration tested and how. My perspective is that unit and integration testing are part of the…
Rubio
  • 613
  • 6
  • 11
1
2 3
10 11