Database Testing Automation Involving Python programming + MongoDB
Requirements For Performing The Test
These are some of the requirements for performing this test:
- Python 3.6 ;
- PyCharm or other IDE for Python;
- pymongo installed;
- 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:
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.
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.
self.collection1 = self.db[collection1].find()
self.collection2 = self.db[collection2].find()
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.
df1 = pd.DataFrame.from_records(self.collection1)
df2 = pd.DataFrame.from_records(self.collection2)
self.df_grouped = pd.concat([df1, df2])
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:
The test class is instantiat:
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.
self.obj = src.compare_collections.CompareCollections()
Once this is done, create the test scenario:
“””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.
Our Popular Articles