Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing of what the program should NOT do

Tags:

unit-testing

This question is about the extent to which it makes sense unit testing.

I’ve been writing a typical program that updates a database with the information coming in XML messages. I thought about the unit tests it needed. The program inserts or updates records according to complicated rules, which spawns many different cases. At first, I decided to test the following several conditions for each case:

  • The necessary number of records added
  • The fields of inserted and updated records are filled correctly
  • No records existing, but irrelevant to the uploaded data were changed

It looked to me that the third type of test really made sense. But soon I found that this is not so easy to implement, because you actually need to kind of snapshot the database and then compare it with the modified one. I quickly started getting annoyed by the fact I needed to write such tests for different cases of database modification, while these tests were not much valuable and informative in sense of specification and design of the production code.

Then I thought, maybe, I’m testing too much? And if not, so if I test that the program does NOT modify irrelevant records, then why don’t I test that it:

  • Does NOT modify other databases
  • Does NOT delete files on disks
  • Does NOT send e-mails to Santa Klaus?

I got completely confused where to draw the borderline. Where would you draw it?

UPDATE

I read many useful hints in the answers and marked one as the solution, because it has more useful ideas to me, but still it's not clear to me how to properly test database updates. Does it make sense testing that the program doen't change too much? And if so, then how thoroughly?

like image 281
Dmitry Tashkinov Avatar asked Jan 14 '11 08:01

Dmitry Tashkinov


1 Answers

You draw the line at the point where tests stop being useful and no longer tell you something about your code.

Is it useful to know that your software doesn't send emails to Santa? No. Then don't test for that.

Knowing that the data access layer is doing the right thing is useful - that the right updates are happening.

like image 90
Oded Avatar answered Oct 12 '22 10:10

Oded