How should modules updating databases be tested?

I have an application that runs intensively in a database. Most application methods are updating data in a database. Some calls are wrappers for stored procedures, while others execute internal database code using third-party APIs.

What should I test in my unit tests? Should I...

  • Test that each method exits without throwing an exception -or-
  • Validate the data in the database after each test to ensure that the data status is as expected

My initial thought is No. 2, but I'm worried that I would write a bunch of frame code to go along with my unit tests. I read that you should not write a bunch of framework code for unit testing.

Thoughts?

EDIT: What I mean by the framework is writing a ton of other code that serves as a library for the unit test code ... not a third-party structure.

+7
database unit-testing tdd
source share
7 answers

I am doing number 2 i.e. I check the update by updating the record, and then reading it and checking that the values ​​are the same as the ones you entered. Do both update and read in the transaction, and then roll back to avoid constant impact on the database. I don’t think of it as testing Framework code, no more than I think of it as testing OS code or network code ... Structure (if you mean a component of the database access level that is not specific to a specific application ) must be checked and verified independently of each other.

+8
source share

There is a third option, which is to use a database access object that knows how to respond to the update as if it were connected to a live database, but in fact it does not perform a query on the database.

This method can be used to supplement testing against a live database. This is not the same as testing with a live database and should not replace this type of testing. But it can be used, at least, to verify that the call to update the database with your class has been made with the appropriate entries. It also usually runs much faster than running tests with a real database.

+3
source share

You should check the actual effect of the code on the data and its compliance with validation rules, etc., and not just the exception exceptions - this will be a bit like a simple compilation of the procedure!

it complex database test code that performs inserts, updates, or deletes (DML) because the test changes the environment in which it runs, i.e. database. Performing the same procedure several times in a row can (and probably should) have different results each time. This is very different from unit testing of "clean code", which you can run thousands of times and always get the same result, that is, "clean code" is deterministic , the database code that runs DML is not.

For this reason, you often have to create a "framework" to support database module tests — that is, scripts to configure some test data in the correct state and to clear it after running the test.

+1
source share

If you do not write to the database manually and instead use the framework (jvm, .net framework, ...), you can safely assume that the structure is written to the database correctly. What you should check if you use the framework correctly.

Just mock database methods and objects. Check if you are calling them correctly and returning the data correctly. Doing this will make it easier for you to write your tests, run them much faster, and make them parallel without any problems.

+1
source share

They do not have to be fully tested! The whole point of these methods is to integrate with the outside world (i.e. into the database). So, make sure your integration tests are superior to you because of these methods and just forget about unit tests.

They should be so simple that in any case they are “clearly error-free”, and if not, you should break them into one part, which has logic and a dumb part, which simply takes the value and sticks it into the database.

Remember: the goal is 100% test coverage, not 100% unit test coverage; which includes all your tests: unit, integration, functionality, system, acceptance and guidance.

+1
source share

If the update logic is complex, you should do # 2.

In practice, the only way to really unit test complex calculations and updates, for example, to calculate bank expenses for a set of customer transactions, is to install a set of tables to known values ​​at the beginning of your unit test and check the expected values ​​at the end.

0
source share

I use DBUnit to load the database with data, execute the update logic, and finally read the updated data from the database and check it. Mostly # 2.

0
source share

All Articles