Validation Examples

Introduction

One of the most important uses of database connectivity in automation testing is database validation. Database validation ensures that the data stored in the database matches the expected results after an application performs an operation.

For example, after a user registers through an application, an automation test can verify that the user’s information has been correctly stored in the database. Similarly, after updating or deleting records through an API, database validation confirms that the changes have been successfully applied.

Automation engineers commonly validate database records after UI testing, API testing, ETL testing, data migration, and integration testing. These validations help ensure data integrity and improve the overall reliability of the application.

In this tutorial, you’ll learn practical database validation examples using JavaScript with MySQL and MongoDB.


What is Database Validation?

Database validation is the process of verifying that the data stored in a database matches the expected application behavior.

It confirms that database operations such as insert, update, delete, and retrieve are working correctly.


Why Perform Database Validation?

Database validation helps testers:

  • Verify backend data.

  • Confirm API operations.

  • Validate UI actions.

  • Detect data corruption.

  • Verify business rules.

  • Improve application quality.

  • Prevent regression issues.


Example 1: Validate User Exists (MySQL)

const [rows] =

await connection.execute(

"SELECT * FROM users WHERE id = ?",

[1]

);

console.log(

rows.length > 0

);

Sample Output

true

Example 2: Validate User Name

const [rows] =

await connection.execute(

"SELECT name FROM users WHERE id = ?",

[1]

);

console.log(

rows[0].name

);

Sample Output

John

Example 3: Validate Record Count

const [rows] =

await connection.execute(

"SELECT COUNT(*) AS total FROM users"

);

console.log(

rows[0].total

);

Sample Output

50

Example 4: Validate Updated Record

const [rows] =

await connection.execute(

"SELECT age FROM users WHERE id = ?",

[1]

);

console.log(

rows[0].age

);

Sample Output

30

Example 5: Validate Record Deletion

const [rows] =

await connection.execute(

"SELECT * FROM users WHERE id = ?",

[10]

);

console.log(

rows.length

);

Sample Output

0

MongoDB Validation Examples

Example 1: Validate Document Exists

const user =

await collection.findOne({

name: "John"

});

console.log(

user !== null

);

Sample Output

true

Example 2: Validate Email Address

const user =

await collection.findOne({

name: "John"

});

console.log(

user.email

);

Sample Output

john@example.com

Example 3: Validate Number of Documents

const total =

await collection.countDocuments();

console.log(

total

);

Sample Output

120

Example 4: Validate Updated Document

const user =

await collection.findOne({

name: "John"

});

console.log(

user.age

);

Sample Output

31

Example 5: Validate Deleted Document

const user =

await collection.findOne({

name: "Deleted User"

});

console.log(

user

);

Sample Output

null

API and Database Validation Example

Suppose an API creates a new user.

Automation flow:

  1. Send POST request.

  2. Verify response status code.

  3. Query the database.

  4. Compare database values with API response.

const response =

await axios.post(

"/users",

{

name: "Alice"

}

);

const [rows] =

await connection.execute(

"SELECT * FROM users WHERE name = ?",

["Alice"]

);

console.log(

rows[0].name ===

response.data.name

);

Sample Output

true

Validation Checklist

Automation engineers typically validate:

ValidationPurpose
Record ExistsVerify insertion
Record CountVerify total records
Field ValuesVerify stored data
Updated ValuesVerify updates
Deleted RecordsVerify deletion
Data TypesVerify stored data types
Business RulesVerify application logic

Real-World Automation Uses

Database validation is commonly used for:

  • API testing.

  • UI testing.

  • Backend verification.

  • Data migration testing.

  • ETL validation.

  • User registration verification.

  • Payment verification.

  • Order verification.

  • Regression testing.

  • Integration testing.


Common Mistakes

Validating Only the UI

The user interface may display the correct information while the database contains incorrect data. Always validate the backend when required.


Hardcoding Expected Values

Avoid hardcoded values when test data changes frequently. Use dynamic or test-generated data where possible.


Ignoring Deleted Records

After a delete operation, verify that the record no longer exists in the database.


Forgetting Database Cleanup

Remove test data after execution to keep the test environment clean and consistent.


Best Practices

  • Validate both API responses and database records.

  • Use parameterized queries.

  • Keep validation logic reusable.

  • Handle database errors using try...catch.

  • Close database connections after validation.

  • Use dynamic test data whenever possible.

  • Clean up test records after execution.


Conclusion

Database validation is a critical part of automation testing because it verifies that backend operations have been completed successfully. By checking inserted, updated, retrieved, and deleted data directly in the database, automation engineers can ensure that applications work correctly beyond the user interface.

Whether using MySQL or MongoDB, strong database validation techniques help improve application reliability, detect hidden defects, and build trustworthy automation frameworks.


Frequently Asked Questions (FAQs)

What is database validation?

Database validation is the process of verifying that data stored in a database matches the expected application behavior.


Why is database validation important in automation testing?

It confirms that backend operations are successful and that data is stored, updated, or deleted correctly.


Can database validation be performed after API testing?

Yes. It is a common practice to validate the database after API requests to ensure backend consistency.


Which databases are commonly validated in JavaScript automation?

Automation engineers commonly validate MySQL, MongoDB, PostgreSQL, SQL Server, and Oracle databases.


What should be validated in a database?

You should validate record existence, field values, record counts, updated data, deleted records, data types, and business rules.


Key Takeaways

  • Database validation verifies backend data accuracy.

  • Validate inserted, updated, retrieved, and deleted records.

  • Use parameterized queries for secure database access.

  • Validate both API responses and database records.

  • Check record counts and field values.

  • Verify deleted records no longer exist.

  • Handle database errors using try...catch.

  • Clean up test data after execution.

  • Database validation improves automation reliability.

  • Mastering database validation is essential for JavaScript, Node.js, API testing, and automation engineering.