Database testing
Database testing is testing aimed directly at the data layer of an application instead of at its screens. It checks that the schema and its constraints are right, that stored procedures and triggers do what they should, that data stays correct after transactions and migrations, and that what the interface shows matches what was stored.
The longer version
The interface is one reader and writer of the database among several. Reports, exports, scheduled jobs, other services and the support team's own queries use the same tables, and none of them passes through the form whose validation the functional tests exercised. Database testing therefore starts with the structure. Each column has the type, length and default the design calls for. A field that must be filled refuses a null, and a field that must be unique refuses a duplicate. A foreign key rejects a row that points at a parent that does not exist, and deleting the parent does what was decided for its children, whether that is blocking the delete, removing them or leaving them without an owner. Each check is made by attempting the bad write against the database itself and confirming that it is refused. A rule enforced only in the application's code holds until the first script, import or second application writes to the table without it.
Logic that lives in the database is tested like any other code, with inputs and expected results. A stored procedure that closes a month's accounts is run against a known set of rows and its output compared with figures worked out by hand. A trigger that writes an audit record is checked for every statement that should fire it and for the statements that should not. Transactions need tests of their own, organised around the four properties known as ACID: atomicity, consistency, isolation and durability. A transfer of stock between two warehouses is two writes, and the test interrupts it between them to confirm that neither survives alone. Two sessions are made to reserve the last seat on one coach at the same moment, and exactly one of them must succeed. The ISTQB glossary defines data integrity as the accuracy and consistency of data over its entire life-cycle, including storage, processing, and retrieval. These tests examine that property at the two moments it is most at risk: a failure half-way through a write, and two writers at once.
Two further checks catch what the others miss. The first is the round trip between the screen and the store. A tester saves a record through the interface, reads the row directly and compares the two, then changes the row and reloads the page. The comparison finds the name cut off at the column's length, the amount rounded on the way in, the time saved in one zone and shown in another, and the accented character that comes back as a question mark. The second is migration. A change to the schema is rehearsed against a copy shaped like production, with its volume and its old, untidy rows. The test counts rows before and after, confirms that existing values survived the conversion and records how long the tables were locked. All of this depends on test data chosen for the purpose. An empty database passes every constraint, and a migration that succeeds on ten tidy rows says little about ten years of real ones, so database testing leans on test data management for masked or synthetic sets that include the awkward records.
Guides to the subject usually sort these checks into three types. Structural testing covers the schema, keys, indexes, stored procedures and the database server. Functional testing covers what the data does in use: the create, read, update and delete operations behind each screen, the transactions and the business rules. Non-functional testing covers behaviour under load and under attack. A query that answers at once against a thousand test rows can take seconds against the real table, so performance checks run on production-sized volumes and look for the missing index. Security checks confirm that each application account reaches only the tables it needs, and that text typed into a form arrives as data and never as part of a query, which is the defence against SQL injection. A backup counts only once it has been restored and the result checked. The tools follow the same split. Unit frameworks such as DbUnit, a JUnit extension, and tSQLt for SQL Server run assertions against the data layer, while load tools such as Apache JMeter supply the volume.
Common questions
What is database testing?
Database testing is testing that examines an application's data layer directly: the structure of its tables, the rules that constrain them, the code stored in the database and the state of the data after operations have run. The tester works with queries against the database as well as through the screens, and compares what is stored with what should be. A simple example: insert an order for a customer who does not exist, and pass the test only if the database refuses the row.
What are the types of database testing?
Database testing is usually divided into three types. Structural testing checks the schema, tables, columns, keys, indexes, stored procedures and triggers against the design. Functional testing checks the create, read, update and delete operations, the transactions and the business rules as the application uses them. Non-functional testing checks performance on realistic volumes, behaviour under load, access permissions and resistance to SQL injection.
Which tools are used for database testing?
Most checks need nothing beyond the database's own query client and a test runner. Unit frameworks add assertions on the data layer, for instance DbUnit, which extends JUnit for Java, and tSQLt, which runs tests inside SQL Server. Load tools such as Apache JMeter drive volume for performance checks, and schema migration tools let the same change be rehearsed on a copy before production.
What is checked in database testing?
The checks fall into five groups. Structure: tables, columns, types, defaults, keys and indexes match the design. Constraints: invalid, duplicate and orphaned rows are refused. Stored logic: procedures, functions, triggers and views return correct results. Transactions: interrupted and concurrent operations leave consistent data. Mapping and migration: what the interface shows equals what is stored, and schema changes preserve existing rows.
How is database testing different from testing through the user interface?
Testing through the interface confirms what one client of the database shows and accepts. Database testing looks at what was stored and at what the database itself will allow. A screen can report success while the row holds a truncated name, and a form can block a bad value that an import script writes without difficulty. Only a direct look at the tables finds either.
How is a database migration tested?
A migration is run first against a copy that resembles production in schema, volume and the oddities of old data. Row counts are compared before and after, a sample of converted values is checked field by field, the application is exercised against the new schema, and the time the tables stayed locked is noted. The reverse step, where one exists, is run and checked in the same way.
What test data does database testing need?
Database testing needs rows that strain the rules: values at the length limit, empty and null fields, duplicates, records whose parent is missing, text in several scripts, dates around the turn of a year, and enough volume for a slow query to show. Production holds such rows, but copying it brings privacy obligations, so teams use masked extracts or synthetic data built to include them.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.