Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,40 @@ This `my_reader.py` should implement a class inherited from AbstractReaderClass
return test_data # return the list of TestCaseData to DataDriver


To facilitate dynamic data-driven testing using data from a database,
it also can integrate a custom reader for Databases with the DataDriver library.
Below is a simple example with `MySQL` to set up and utilize this custom reader within your testing environment.

.. code :: python

from DataDriver.AbstractReaderClass import AbstractReaderClass
from DataDriver.ReaderConfig import TestCaseData
import mysql.connector

class MySQLDatabaseReader(AbstractReaderClass):
def __init__(self, reader_config):
super().__init__(reader_config)
self.db_config = {
'host': self.kwargs.get('host', 'localhost'),
'user': self.kwargs.get('user', 'your_username'),
'password': self.kwargs.get('password', 'your_password'),
'database': self.kwargs.get('database', 'test_db')
}

def get_data_from_source(self):
test_data = []
connection = mysql.connector.connect(**self.db_config)
cursor = connection.cursor()
cursor.execute("SELECT id, value1, value2, tag FROM test_cases") # Adjust this query for your table structure

for row in cursor.fetchall():
args = {'${var_1}': str(row[1]), '${var_2}': str(row[2])}
test_data.append(TestCaseData(f'test {row[0]}', args, [row[3]]))

cursor.close()
connection.close()
return test_data

See other readers as example.


Expand Down