Database Testing Tutorial
Database Testing Tutorial
Database Testing Tutorial
Audience
This tutorial has been designed for all those readers who want to learn the basics of
Database testing. It is especially going to be useful for all those software testing
professionals who are required to test the applications in order to find out how they affect
the database performance.
Prerequisites
We assume the readers of this tutorial have hands-on experience of handling a database
using SQL queries. In addition, it is going to help if the readers have an elementary
knowledge of basic database concepts.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
Database Testing
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Copyright & Disclaimer ..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
ii
Database Testing
iii
Database Testing
1. DB Testing Overview
Database testing includes performing data validity, data integrity testing, performance
check related to database and testing of procedures, triggers and functions in the
database.
Example
Consider an application that captures the day-to-day transaction details for users and
stores the details in the database. From database testing point of view, the following
checks should be performed:
The transactional information from the application should be stored in the database
and it should provide correct information to the user.
Only completed transactions should be stored and all incomplete operations should
be aborted by the application.
To ease the complexity of calls to database backend, developers increase the use
of View and Stored Procedures.
These Stored procedures and Views contain critical tasks such as inserting
customer details (name, contact information, etc.) and sales data. These tasks
need to be tested at several levels.
In a database, data comes from multiple applications and there is a possibility that
harmful or incorrect data is stored in the database. Therefore, there is a need to
check database components regularly. In addition, data integrity and consistency
should be checked regularly.
1
Database Testing
2
Database Testing
3
Database Testing
2. DB Testing Types
Based on the function and structure of a database, DB testing can be categorized into
three categories:
Structural Database Testing It deals with table and column testing, schema
testing, stored procedures and views testing, checking triggers, etc.
Discussed are the common components tested with respect to Structural Testing:
In Schema Testing:
Sometimes it happens that the end user application objects are not correctly
mapped or compatible with database objects. Therefore, checking the validation of
the various schema formats associated with the databases is required.
It is required to find the unmapped objects in database, like tables, views, columns
etc. is required.
There are various tools in the market that can be used to perform object mapping in
schemas.
Example: In Microsoft SQL Server, a tester can write simple queries to check and validate
schemas in the database.
If the tester wants to make changes to a table structure, he/she should ensure that all the
stored procedures having that table are compatible with this change.
4
Database Testing
If the development team has covered all the loops and conditions by passing input
to applications in the procedures.
TRIM operations are applied properly when the data is fetched from required tables
in database.
Validation of the overall integration of the stored procedure modules as per as the
requirements of the application under test.
The most common tools that are used to perform stored procedures testing are LINQ, SP
Test tool, etc.
Trigger Testing
In trigger testing, a tester needs to ensure the following:
Whether the coding conventions are followed during the coding phase of the
triggers.
Whether the trigger updates the data correctly, once they have been executed.
5
Database Testing
Validating the data types in the database to field values in front-end application.
Validating the length of data field in database to length of data types in the
application.
Checking if there are any unmapped tables or columns in the database from
application field objects.
Naming conventions of database tables and columns are verified, if they are in
accordance with business requirement or not.
Validating the Keys and Indexes in the database, i.e., primary and foreign keys in
tables are defined as per requirement.
Check if the primary keys and their corresponding foreign keys are same in two
tables.
Length and data type of keys and indexes are maintained as per requirement.
If the database server can handle the expected number of transactions as per the
business requirement.
Functional Testing
Functional testing is performed keeping in mind an end-user point of view; whether the
required transactions and operations run by the end-users meet the business
specifications.
6
Database Testing
It performs module testing of database functions, triggers, views, SQL queries etc. This
type of testing validates database tables, data models, database schema etc. It checks
rules of Referential integrity. It selects default table values to check on database
consistency.
The most common techniques used to perform white box testing are condition coverage,
decision coverage, statement coverage, etc.
Coding errors can be detected in white-box testing, so internal bugs in the database can
be eliminated. The limitation of white-box testing is that SQL statements are not covered.
Nonfunctional Testing
Nonfunctional testing involves performing load testing, stress testing, checking minimum
system requirements to meet business specification, risk finding and performance
optimization of database.
Load Testing
The primary target of load testing is to check if most running transactions have
performance impact on the database.
The response time for executing the transactions for multiple remote users.
Time taken by the database to fetch specific records.
Stress Testing
Stress testing is performed to identify the system breakpoint. In this testing, application
is loaded in such a way that the system fails at one point. This point is called the
breakpoint of database system.
The most commonly used stress testing tools are LoadRunner and WinRunner.
Let us take an example of Stress Testing. A CRM application can take a maximum user
load of 50000 concurrent users. Suppose you increase the load to 51000 and make some
transactions such as updating records or adding an entry. As soon as you do the
transaction, the application can sync with the database system. So the next test is to
perform with a user load of 52000. Sometimes, Stress Testing is also called Fatigue
Testing.
8
Database Testing
3. DB Testing Processes
Various SQL statements are used to develop the Test cases. The most common SQL
statement, which is used to perform DB testing, is the Select statement. Apart from this,
various DDL, DML, DCL statements can also be used.
First stage in DB Testing is to check the initial state of the database before starting the
testing process. Then database behavior is tested for defined test cases. In accordance
with the results obtained, test cases are customized.
For successful database testing, the workflow given below is executed by every single test.
Set up Fixture This involves entering the data into the database and check the
current state of the database.
Perform test, verify results and generate results The Test is run and the
output is verified. If the output is as per expected results, the next step is to
generate the results as per requirement. Otherwise, testing is repeated to find the
bugs in database.
9
Database Testing
4. DB Testing Techniques
This chapter explains the most common techniques that are used to perform Database
Testing.
Data types between a foreign key column and a column in other table Indices,
clustered or non-clustered unique or not unique
10
Database Testing
Output: Whether the output contains many records. Zero rows are effected or only
a few records are extracted.
What is the function of Stored Procedure and what a stored procedure is not
supposed to do?
Passing sample input queries to check if a stored procedure extracts correct data.
Stored Procedure Parameters: Call stored procedure with boundary data and
with valid data. Make each parameter invalid once and run a procedure.
Return values: Check the values that are returned by stored procedure. In case
of a failure, nonzero must be returned.
Error messages check: Make changes in such a way that the stored procedure
fails and generate every error message at least once. Check any exception
scenarios when there is no predefined error message.
Trigger Tests
In a Trigger test, the tester must perform the following tasks:
Stored procedures should be called intensively to select, insert, update, and delete
records in different tables to find any conflicts and incompatibility.
11
Database Testing
Type 1: In Type 1 testing, find out the features of the project. For each major
feature, find out the schema, triggers, and stored procedures responsible to
implement that function and put them into a functional group. Then test each group
together.
When a service has a request or saves data, some stored procedures will get called.
Those stored procedures will be the place to start testing and those tables will be
the place to check the test results.
Stress Testing
Stress Testing involves getting a list of major database functions and corresponding stored
procedures. Follow the steps given below for Stress Testing:
Write test scripts to try those functions and every function must be checked at least
once in a full cycle.
Perform the test scripts again and again for a specific time period.
Verifying the log files to check any deadlocks, failure out of memory, data
corruption, etc.
Benchmark Testing
If your database does not have any data problems or bugs, system performance can be
checked. A poor system performance can be found in benchmark testing by checking the
parameters given below:
12
Database Testing
Pick up an existing record, change the values in some fields, and save the record.
(It involves the UPDATE statement or update stored procedures and update
triggers.)
Insert a new menu item in the front-end window. Fill in the information and save
the record. (It involves the INSERT statements or insertion stored procedures and
deletion triggers.)
Pick up an existing record, click on the DELETE or REMOVE button, and confirm the
deletion. (It involves the DELETE statement or deletion stored procedures and
deletion triggers.)
Repeat these test-cases with invalid data and see how the database responds.
13
Database Testing
5. DB Testing Scenarios
In this chapter, we will see some common database test scenarios with respect to various
testing methods.
Verifying the name of database, verifying the data device, log device and dump
device, verifying if enough space allocated for each database and verifying
database option setting.
Names of all the tables in database, column names for each table, column types
for each table, null value check or not. Verify the Key and indexes in each table:
Primary key for each table, foreign keys for each table.
Data types between a foreign key column and a column in other table Indices,
clustered or non-clustered unique or not unique.
Finding out the schema, triggers and stored procedures responsible to implement
that function and make them into a functional group and then each group can be
tested together.
Check data flow and see where you can check the data. Start from the front-end.
Write test scripts to try major functions and every function must be checked at
least once in a full cycle.
Perform the test scripts again and again for a specific time period.
Verifying the log files to check any deadlock, failure out of memory, data corruption,
etc.
Write queries from a front end and issue the searches. Pick up an existing record,
change values in some fields and save the record. (It involves UPDATE statement
or update stored procedures, update triggers.)
14
Database Testing
Insert a new menu item in a front-end window. Fill in information and save the
record. (It involves INSERT statements or insertion stored procedures, deletion
triggers.)
Pick up an existing record, click on the DELETE or REMOVE button, and confirm the
deletion. (It involves DELETE statement or deletion stored procedures, deletion
triggers.)
Repeat these test-cases with invalid data and see how the database responds.
15
Database Testing
6. DB Testing Objects
Schemas, tables, stored procedures, and Triggers are key objects of a database. We
have already shared DB testing types and test scenarios for these data base objects.
Schemas
A database schema defines the structure of a database system in a format supported by
the database management system. A Schema refers to how a database is structured
(composed of database tables in the case of Relational Databases).
In a relational database, the schema consists of tables, fields, views, indexes, packages,
procedures, functions, triggers, types, materialized views, synonyms, database links, and
other elements.
Schemas are generally stored in a data dictionary. Although a schema is defined in text
database language, the term is often used to refer to a graphical depiction of the database
structure. In other words, schema is the structure of the database that defines the objects
in the database.
Star Schema
Snowflakes Schema
Galaxy Schema
Tables in Database
In a relational database, a table is used to organize the information into rows and columns.
Example A Customer table contains information such as customer id, addresses, phone
numbers, and so on as a series of columns.
Each single piece of data is a field in the table. A column consists of all the entries in a
single field, such as the telephone numbers of all the customers. Fields are organized as
records, which are complete sets of information (such as the set of information about a
particular customer), each of which comprises a row.
Stored Procedures
A stored procedure is a series of SQL statements stored in the database in a compiled
form and multiple programs can share it. The use of stored procedures can be helpful in
maintaining data integrity, data control access and improving productivity.
16
Database Testing
Triggers
A database trigger is code that is executed in response to certain events on a particular
table or view in a database. The trigger is mostly used for maintaining the integrity of the
information on the database.
17
Database Testing
7. DB Testing Data Integrity
For checking Data Integrity, you need to perform the following operations:
You need to check major columns in each table and verify if any incorrect data
exists. (Characters in name field, negative percentage, etc.)
Find out inconsistent data and insert them into relevant tables and see if any failure
occurs.
Insert a child data before inserting its parents data. Try to delete a record that is
still referenced by the data in another table.
If a data in a table is updated, check whether the other relevant data is updated as
well. You need to ensure that replicated servers or databases are in sync and
contain consistent information.
18
Database Testing
8. DB Testing Data Mapping
Data mapping in a database is one of the key concept that needs to be validated by every
tester. Usually the testers have to verify the user interface front end field mapping with
the corresponding back end database field.
When you take any action in the front end application, there is a corresponding CRUD
action get invoked, and tester have to check the every invoked action is successful or not.
To check the fields in the UI/Front end forms and mapped consistently with the
corresponding DB table. This mapping information is defined in the requirements
documents as mentioned above.
For any action performed in the front end of an application, a corresponding CRUD
Create, Retrieve, Update and delete action gets initiated at the back end.
A tester will have to check if the right action is invoked and the invoked action in
itself is successful or not.
Step 2: Next is to check for table mapping, column mapping, and data type
mapping.
Step 4: Run each script when records do not exist in destination tables.
Step 5: Run each script when the records already exist in the destination tables.
19
Database Testing
9. DB Testing Performance (Load, Stress)
An application with more response time and poor performance can lead to huge problems.
Database Load Testing is used to find any performance issues before you deploy your
database applications for end users.
Database Load Testing helps you design database application for performance, reliability
and scalability. Load Testing of Database applications involves testing the performance
and scalability of your Database application with varying user load.
Database Load testing involves simulating real-life user load for the target Database
application. It helps you determine how your Database application behaves when multiple
users hits it simultaneously.
Load Testing
The primary target of Load Testing is to check if most running transactions have
performance impact on the database. In load testing, you need to check the following
aspects:
The response time for executing the transactions for multiple remote users should
be checked.
With normal transactions, you should include one editable transaction to check the
performance of the database for these type pf transactions.
With normal transactions, you should include one non-editing transaction to check
performance of database for these type of transactions.
Stress Testing
Stress testing is performed to identify the system breakpoint. Here the application is
loaded in such a way that the system fails at one point. This point is called the breakpoint
of the database system. Stress testing is also known as Fatigue Testing.
The most common stress testing tools are LoadRunner and WinRunner.
20
Database Testing
10. DB Testing Tools
There are various tools provided by vendors that can be used to generate Test data, to
manage Test data and perform database testing like Load Testing and Regression Testing.
SQLUnit
These tools are used to perform
Tools to perform TSQLUnit
regression testing on your database.
Unit Testing DBFit
DBUnit
21
Database Testing
11. DB Testing Database Backup
The most important part of an organizational growth is its data. In case of a system failure,
there is a need to restore the data. Back up is an exact copy of the database, which helps
you to restore your data in case of any data loss.
Consider a finance company which has data related to its customers such as A/C number,
customer names, credit and debits, duration, etc. How would such an organization deal
with the pressure of losing such important information in case of a data failure?
This is the reason you back up the data so that in case of any failure of a disk, disk
controller, etc. you can rely on the backup to restore it into the database.
Example: One of common tool to take data backup is Oracle Recovery Manager
(RMAN) that is an Oracle utility to take database backup.
22
Database Testing
BACKUP VALIDATE is used to test if you are able to make a valid backup of database
files. It ensures:
23
Database Testing
12. DB Testing Recovery
Database recovery testing is used to ensure that the database is recovered. Recovery
testing allows you to find out whether the application is running properly and to check
retrieving invaluable data that would have been lost if your recovery method is not
properly setup.
You also check if several critical processes are running smooth to ensure that the data
recovery will pass smoothly through the testing phase.
Any errors or mistakes in the backup software and you need to resolve these issues
at an earlier stage.
You need to conduct the recovery testing so that you will know what to do in case
of an emergency situation.
You need to check recovery testing needs so that you can plan for an effective
recovery strategy.
You should also know how you can recover the documents.
You need to run the recovery tests in early phase of the project. This allows you to remove
and throw away every type of errors from the system. Here is a list of some of important
points, which should be considered at the time of testing:
The sensitivity of data in database system. More critical the data is, the more
regularly you will need to test the software.
Given below are the common actions performed in Database Recovery Testing:
24
Database Testing
13. DB Testing Security
Database security testing is done to find the loopholes in security mechanisms and also
about finding the vulnerabilities or weaknesses of database system.
The main target of database security testing is to find out vulnerabilities in a system and
to determine whether its data and resources are protected from potential intruders.
Security testing defines a way to identify potential vulnerabilities effectively, when
performed regularly.
Given below are the primary objectives of performing database security testing:
Authentication
Authorization
Confidentiality
Availability
Integrity
Resilience
SQL Injection
This is most common type of attack in a database system where malicious SQL statements
are inserted in the database system and are executed to get critical information from the
database system. This attack takes advantage of loopholes in implementation of user
applications. To prevent this, user inputs fields should be carefully handled.
Denial of Service
In this type of attack, an attacker makes a database system or application resource
unavailable to its legitimate users. Applications can also be attacked in ways that render
the application, and sometimes the entire machine, unusable.
25
Database Testing
Identity Spoofing
In Identity Spoofing, a hacker uses the credentials of a user or device to launch attacks
against network hosts, steal data or bypass access controls to database system.
Preventing this attack requires IT-infrastructure and network-level mitigations.
Data Manipulation
In a data manipulation attack, a hacker changes data to gain some advantage or to
damage the image of database owners.
Penetration Testing
A penetration test is an attack on a computer system with the intention of finding security
loopholes, potentially gaining access to it, its functionality and data.
Risk Finding
Risk Finding is a process of assessing and deciding on the risk involved with the type of
loss and the possibility of vulnerability occurrence. This is determined within the
organization by various interviews, discussions and analysis.
These attacks are a big threat to data as the attackers can get access to important
information from the server database. To check SQL injection entry points into your web
application, find out code from your code base where direct MySQL queries are executed
on the database by accepting some user inputs.
SQL Injection Testing can be performed for Brackets, Commas, and Quotation marks.
Password Cracking
This is the most important check while performing database system testing. To access
critical information, hackers can use a password-cracking tool or can guess a common
26
Database Testing
username/password. These common passwords are easily available on internet and also
password cracking tools exist freely.
Example of most common security standards are ISO 27001, BS15999, etc.
Paros
All HTTP and HTTPS data between server and client, including cookies and form fields, can
be intercepted and modified using these scanners. It is used for Cross-platform, Java
JRE/JDK 1.4.2 or above.
Skipfish
This tool is used to scan their sites for vulnerabilities. Reports generated by the tool are
meant to serve as a foundation for professional web application security assessments. It
is preferred for Linux, FreeBSD, MacOS X, and Windows.
Vega
It is an open source, multiplatform web security tool that is used to find instances of SQL
injection, cross-site scripting (XSS), and other vulnerabilities in web applications. It is
preferred for Java, Linux, and Windows.
27
Database Testing
Wapiti
Wapiti is an open source and web-based tool that scans the web pages of the web
application and check for scripts and forms where it can inject data. It is built with Python
and can detect File handling errors, Database, XSS, LDAP and CRLF injections, Command
execution detection.
Web Scarab
It is written in Java and is used for analyzing the applications that communicate through
HTTP/HTTPS protocols. This tool is primarily designed for developers who can write code
themselves. This tool is not OS dependent.
28
Database Testing
14. DB Testing Challenges
To perform database testing successfully, a tester should collect the requirements from all
the sources, like technical and functional requirements. There is a possibility that a few
requirements are at a high level, so there is a need to breakdown those requirements into
the small parts. Testing database is a complex task and the testers face many challenges
while performing this testing. Most common database testing challenges are:
When you have a list of objects to test, next is to estimate the effort required to design
the tests and execute the tests for each test item. Depending on their design and data
size, some database tests may take a long time to execute.
As the database size is too large, it becomes a big challenge to find out the objects that
have to be tested and those which are to be left out.
Once the database structure changes, you should analyze the impact of the changes and
modify the tests. In addition, if multiple users use the test database, you would not be
sure about the test results so you should ensure that the test database is used for testing
purpose only.
Another challenge in DB testing is that you run multiple tests at the same time. You should
run one test at a time at least for the performance tests. You do not want your database
performing multiple tasks and under-reporting performance.
29
Database Testing
30
Database Testing
15. DB Testing Interview Questions
Some of the common reasons why one needs to perform Database testing are as follows:
To ease the complexity of calls to database backend, developers increase the use
of View and Stored Procedures.
These Stored procedures and Views contain critical tasks such as inserting
customer details (name, contact information, etc.) and sales data. These tasks
need to be tested at several levels.
In a database, data comes from multiple applications and there is a possibility that
harmful or incorrect data is stored in the database. Therefore, there is a need to
check database components regularly. In addition, data integrity and consistency
should be checked regularly.
Structural Database testing It deals with table and column testing, schema
testing, stored procedures and views testing, checking triggers, etc.
31
Database Testing
Q5. Name a few tools that can be used to test Stored Procedures in a
database.
The most common tools that are used to perform stored procedures testing are LINQ, SP
Test tool, etc.
Q6. What is a join in SQL? What are the different types of joins?
Joins are used to connect two or more tables in some logical manner. Common types of
joins include: Inner join, Non-equijoin, Outer join, Self-join, and Cross join.
db_disconnect(query);
Using Output database checkpoints, SQL manual queries options must be selected. Here,
the select query can be written.
Validate the calling procedure name, calling parameters and expected responses
for different sets of input parameters.
32
Database Testing
The tester should call the stored procedure in the database using the EXEC command. If
any parameters are required, they must be passed. Different values of parameters must
be passed to confirm if the stored procedure is executed or not. On calling this command
it must check and verify the nature and behavior of the database.
Example: If the stored procedure is written to populate some table, the table values must
be checked.
Q12. What is an Operator in SQL? How many types of operators are there
in SQL?
Operators are used to specify conditions in an SQL statement and to serve as conjunctions
for multiple conditions in a statement.
Arithmetic Operators
Comparison/Relational Operators
Logical Operators
Set Operators
Operators used to negate conditions
33
Database Testing
Union All operation is similar to Union, but it also shows the duplicate rows.
Q20. Which SQL statements are commonly used to develop test-cases for
database testing?
Various SQL statements are used to develop the Test cases. Most common SQL statement
which is used to perform DB testing is select statement. Apart from this various DDL, DML,
DCL statements can also be used.
34
Database Testing
Growth and restructuring of base tables is not reflected in views. Thus the view can
insulate users from the changes in the database. Hence accounts for logical data
independence.
To minimize redundancy.
To minimize insertion, deletion and update anomalies.
Q24. What is indexing and what are the different kinds of indexing?
Indexing is a technique for determining how quickly specific data can be found. It is used
for query performance optimization. Indexing can be of the following types:
The primary difference between SQL and other conventional programming languages is
that SQL statements specify what data operations should be performed rather than how
to perform them.
Q26. What are stored procedures? What are the advantages of using
them?
Stored procedures are used to perform a user defined operation. A stored procedure can
have a set of compound SQL statements. A stored procedure executes the SQL commands
and returns the result to the client.
35
Database Testing
If a cold backup is performed, the only option available in the event of data file loss is
restoring all the files from the latest backup. All the changes that are performed after the
last backup is lost.
Hot Backup: Some databases cant shut down while making a backup copy of the files,
so cold backup is not an available option. For these types of database we use hot backup.
Q30. You have been provided with a set of tables and asked to create a
new database to store them. While checking the data values in the tables,
what points to be considered for this?
In such a case, you need to test the following aspects:
Multivalued dependencies
Functional dependencies
Candidate keys
Primary keys
Foreign keys
Q31. How do you test if your database is updated when data is entered in
front-end application?
You can go to the database and run a relevant SQL query. In WinRunner, you can use
database checkpoint function. If the application provides view function, then you can verify
the same from the front-end.
36
Database Testing
Q33. What is retesting and how it is different from data driven testing?
Once you execute the test-cases and find the defects that has been already detected and
fixed. Re-execution of the same test with different input values to confirm the original
defect has been successfully removed is called Re-testing.
Q36. What are the key points that should be considered while performing
database recovery testing?
The following key points are to be considered while performing database recovery testing:
The sensitivity of data in database system. More critical the data is, the more
regularly you will need to test the software.
Q37. Name a few tools that are used by a tester to generate test data for a
database system.
The following tools are used to generate test data:
Data Factory
DTM Data Generator
Turbo Data
37
Database Testing
Physical Backups: Physical backup includes taking back up using 3rd party backup
tools like Veritas net back, IBM Tivoli Manager or user manager backups using OS
utilities.
A common tool to take data backup is Oracle Recovery Manager (RMAN) that is an Oracle
utility to take database backup.
Authentication
Authorization
Confidentiality
Availability
Integrity
Resilience
38
Database Testing
Q43. Name a few tools that can be used to perform database security
testing.
The following tools can be used to perform database security testing: Zed Attack Proxy,
Paros, Social Engineer Toolkit, Skipfish, Vega, Wapiti, and Web Scarab.
Q44. What are the common challenges that you face while performing
database testing?
The common challenges that one faces while performing database testing are as follows:
39