Written By :Appsierra

Mon Apr 29 2024

5 min read

Database Testing Automation Involving Python programming + MongoDB

Home >> Blogs >> Database Testing Automation Involving Python programming + MongoDB
Database Testing Automation

Requirements For Performing The Test

These are some of the requirements for performing this test:

  1. Python 3.6 ;
  2. PyCharm or other IDE for Python;
  3. pymongo installed;
  4. pandas installed.

Step 1:Installation of Pymongo

It requires the installation of the connection driver with MongoDB.

In the terminal, type:

$ python -m pip install pymongo

Python MongoDB – Get Started

Step 2: Panda Installation

It is necessary to install the pandas.

In the terminal, type:

pip install pandas

Step 3: Python Class Construction

For Python list to recognize the directory as a package, it must have an empty file named __init__.py.

We import within the class, the connection module with the MongoDB (pymongo):

from pymongo import MongoClient

The class is instantiate:

class MongoAutomation:

You create a connection to MongoDB. In this example, a local base with a bank named cwi-automation is being used, so the local installation of MongoDB is required. But you can access other instances of the bank by only changing the connection string.

def connect_database(self):

    self.db_client = MongoClient(‘localhost’, 27017)

    self.db = self.db_client.cwi_automation

It is now necessary to read the collection(s) that you want to validate the information. In this example, we will make a comparison of the data between two collections using the python module called pandas. The comparison can also be done through a simple for-loop, but it is less performative for large volumes of documents.

def read_collections(self):

    self.collection1 = self.db[collection1].find()

    self.collection2 = self.db[collection2].find()

Also Read: Top Selenium Python Frameworks For Python Testing

It is carried out the import of pandas:

import pandas as pd

In the link below, the documentation regarding pandadataframe:


Data from collection1 and collection2 variables is transformed into dataframes, and a grouping is performed with the concat command ([param1, param2]). After this, the comparison is made, generating a new dataframe. If both collections are the same, the generated dataframe must be empty.

def transform_in_dataframe(self):

 df1 = pd.DataFrame.from_records(self.collection1)

 df2 = pd.DataFrame.from_records(self.collection2)

 self.df_grouped = pd.concat([df1, df2])

def compare_collections(self):

 column_headers = list(self.df_grouped.columns.values)


 self.df_final = self.df_grouped.drop_duplicates(column_headers, keep=False)

In this example, the information regarding _id that MongoDB generates is being removed, thus disregarding different _ids.

Step 4: Test Case Creation

You create a new python language file and you import the unit test module and the class where the methods created above are:

import unittest

import src.compare_collections

The test class is instantiat:

class TestsCompareCollections(unittest.TestCase):

Using unit test TestFixtures, setUp() and tearDown()are created, which will run before and after the scenario runs, respectively. In this example, the setUp() only instantiates the class with the methods, while the tearDown()terminates the connection to the MongoClient.

def setUp(self):

    self.obj = src.compare_collections.CompareCollections()

def tearDown(self):


Once this is done, create the test scenario:

def test_compare_collections(self):

 “””Comparacao entre duas collections do MongoDB”””





Python will identify that this method is a test scenario through the test prefix in the description(test_compare_collections).

In this way, the test class should look like this:

Test scenario created, execution can be done through the IDE or by the terminal. If you still have any technical questions with regard to the information above, feel free to address them in the comments section below. We will try our best to resolve them. The Python compiler package is a tool for analyzing Python source code and generating Python bytecode. 

Note: Because it is only a well-simplified example, no asserts, logs and other validations required in an automated test were contemplated. All this information is available in the unit test documentation and also in the Python 3 documentation.

Also Read: What Is Database Testing? 6 Database Testing Tools

Contact Us

Let our experts elevate your hiring journey. Message us and unlock potential. We'll be in touch.

Get the latest
articles delivered to
your inbox

Our Popular Articles