A Complete Guide to Database Testing with Practical Tips and Examples
A Complete Guide to Database Testing with Practical Tips and Examples
Computer applications are more complex these days with technologies like Android and also
with lots of Smartphone apps. The more complex the front ends, the more intricate the back ends
become.
So it is all the more important to learn about DB testing and be able to validate Databases
effectively to ensure security and quality databases.
It does not matter whether it is a web, desktop or mobile, client-server, peer-to-peer, enterprise,
or individual business; the Database is required everywhere at the backend.
As the complexity of application increases, the need for a stronger and secure Database emerges.
In the same way, for the applications with a high frequency of transactions (For
Example, Banking or Finance application), the necessity of a fully-featured DB Tool is coupled.
Nowadays, we have big data that is large and complex that the traditional databases can’t handle
them.
There are several Database tools are available in the market For Example, MS-Access, MS
SQL Server, SQL Server, Oracle, Oracle Financial, MySQL, PostgreSQL, DB2, Toad, Admirer,
etc. These tools vary in cost, robustness, features, and security. Each of these has its own benefits
and drawbacks.
Further reading =>>PostgresSQL Vs MySQL
What You Will Learn: [hide]
Why Test Database?
o #1) Data Mapping
o #2) ACID Properties Validation
o #3) Data Integrity
o #4) Business Rule Conformity
What To Test (Database Testing Checklist)
o #1) Transactions
o #2) Database Schemas
o #3) Triggers
o #4) Stored Procedures
o #5) Field Constraints
o Data Testing Activities
How to Test the Database (Step-by-step Process)
o Some Practical Tips
o Conclusion
o Recommended Reading
Why Test Database?
Below, we will see why the following aspects of a DB should be validated:
Check whether the fields in the UI/frontend forms are mapped consistently with the
corresponding fields in the DB table. Typically this mapping information is defined in
the requirements documents.
Whenever a certain action is performed at the front end of an application, a
corresponding CRUD (Create, Retrieve, Update and Delete) action gets invoked at the
back end. A tester will have to check if the right action is invoked and whether the
invoked action in itself is successful or not.
#2) ACID Properties Validation
Atomicity, Consistency, Isolation, and Durability. Every transaction a DB performs has to adhere
to these four properties.
Atomicity means that a transaction either fails or passes. This means that even if a single
part of the transaction fails- it means that the entire transaction has failed. Usually, this is
called the “all-or-nothing” rule.
Consistency: A transaction will always result in a valid state of the DB
Isolation: If there are multiple transactions and they are executed all at once, the
result/state of the DB should be the same as if they were executed one after the other.
Durability: Once a transaction is done and committed, no external factors like power loss
or crash should be able to change it
Suggested reading =>> MySQL Transaction Tutorial
#3) Data Integrity
For any of the CRUD Operations, the updated and most recent values/status of shared data
should appear on all the forms and screens. The value should not be updated on one screen and
display an older value on another one.
When the application is under execution, the end-user mainly utilizes the ‘CRUD’ operations
facilitated by the DB Tool.
C: Create – When user ‘Save’ any new transaction, ‘Create’ operation is performed.
R: Retrieve – When user ‘Search’ or ‘View’ any saved transaction, ‘Retrieve’ operation is
performed.
U: Update – When user ‘Edit’ or ‘Modify’ an existing record, the ‘Update’ operation of DB is
performed.
D: Delete – When a user ‘Remove’ any record from the system, ‘Delete’ operation of DB is
performed.
Any database operation performed by the end-user is always one of the above four.
So devise your DB test cases in a way to include checking the data in all the places it appears to
see if it is consistently the same.
For Example, a new student joined a school. The student is taking 2 classes: math and science.
The student is added to the “student table”. A Trigger could add the student to the corresponding
subject tables once he is added to the student table.
The common method to test is to execute the SQL query embedded in the Trigger independently
first and record the result. Follow this up with executing the Trigger as a whole. Compare the
results.
These are tested in both the Black-box and White-box testing phases.
White box testing: Stubs and Drivers are used to insert or update or delete data that
would result in the trigger being invoked. The basic idea is to just test the DB alone even
before the integration with the front end (UI) is made.
Black box testing:
a) Since the UI and DB, integration is now available; we can Insert/Delete/Update data from the
front end in a way that the Trigger gets invoked. Following that, Select statements can be used to
retrieve the DB data to see if the Trigger was successful in performing the intended operation.
b) The second way to test this is to directly load the data that would invoke the Trigger and see if
it works as intended.
#4) Stored Procedures
Stored Procedures are more or less similar to user-defined functions. These can be invoked by
Call Procedure/Execute Procedure statements and the output is usually in the form of result sets.
These are stored in the RDBMS and are available for applications.
Checking the unique value can be done exactly the way we did for the default values. Try
entering values from the UI that will violate this rule and see if an error is displayed.
Make sure that the mapping between different forms or screens of AUT and its DB is not only
accurate but also per the design documents (SRS/BRS) or code. Basically, you need to validate
the mapping between every front-end field with its corresponding backend database field.
For all CRUD operations, verify that respective tables and records are updated when the user
clicks ‘Save’, ‘Update’, ‘Search’ or ‘Delete’ from GUI of the application.
The ACID test table will have two columns – A & B. There is an integrity constraint that the sum
of values in A and B should always be 100.
Atomicity test will ensure any transaction performed on this table is all or none i.e. no records
are updated if any step of the transaction is failed.
Consistency test will ensure that whenever the value in column A or B is updated, the sum
always remains 100. It won’t allow insertion/deletion/update in A or B if the total sum is
anything other than 100.
Isolation test will ensure that if two transactions are happening at the same time and trying to
modify the data of the ACID test table, then these tractions are executing in isolation.
Durability test will ensure that once a transaction over this table has been committed, it will
remain so, even in the event of power loss, crashes, or errors.
This area demands more rigorous, thorough and keen testing if your application is using the
distributed database.
In that case, make sure that the latest state of data is reflected everywhere. The system must
show the updated and most recent values or the status of such shared data on all the forms and
screens. This is called as Data Integrity.
Some simple examples of powerful features are ‘Referential Integrity’, Relational constraints,
Triggers, and stored procedures.
So, using these and many other features offered by DBs, developers implement the business
logic at the DB level. The tester must ensure that the implemented business logic is correct and
works accurately.
The above points describe the four most important ‘What To’ of testing DB. Now, let’s move
on to the ‘How To’ part.
How To Test The Database (Step-By-Step Process)
The general test process testing database is not very different from any other application.
Usually, SQL queries are used to develop the tests. The most commonly used command is
“Select”.
You can combine GUI and data verification in respective tables for better coverage. If you are
using the SQL server then you can make use of SQL Query Analyzer for writing queries,
executing them and retrieving results.
This is the best and robust way of testing a database when the application is of a small or
medium level of complexity.
If the application is very complex then it may be hard or impossible for the tester to write all the
required SQL queries. For complex queries, you take help from the developer. I always
recommend this method as it gives you confidence in testing and also enhances your SQL skills.
For Manual Data Testing, the Database tester must possess a good knowledge of database table
structure.
=> Here is the list of the TOP DB Testing Tools you should check
Conclusion
With all these features, factors and processes to test on a database, there is an increasing demand
for the testers to be technically proficient in the key Database concepts. Despite some of the
negative beliefs that testing a database creates new bottlenecks and is a lot of additional
expenditure – this is a realm of testing that is attracting significant attention and demand.