diff --git a/Readme.rst b/Readme.rst index 42554a7..019bb1e 100644 --- a/Readme.rst +++ b/Readme.rst @@ -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.