This class extends the standard text test result to capture and display SQL queries executed during test runs. It monitors database backend logs for each test case, appending the captured SQL output to test failures and errors for debugging purposes. The class manages a custom logging handler to isolate query logs per test and formats them for clear presentation in the final test report.
Attributes
| Attribute | Type | Description |
|---|
| logger | logging.Logger | The logging instance for 'django.db.backends' used to capture database queries during test execution. |
| handler | logging.StreamHandler = null | A stream handler that temporarily stores captured SQL output for the current test case. |
Constructor
Signature
def DebugSQLTextTestResult(
stream: any,
descriptions: any,
verbosity: any
)
Parameters
| Name | Type | Description |
|---|
| stream | any | The stream where the test results will be written. |
| descriptions | any | A flag indicating whether to include descriptions of the tests. |
| verbosity | any | The level of detail to include in the test output. |
Methods
startTest()
@classmethod
def startTest(
test: unittest.TestCase
) - > null
Prepares the test environment by attaching a custom SQL logging handler to the Django database backend logger. This ensures that all database queries executed during the test are captured in an isolated buffer.
Parameters
| Name | Type | Description |
|---|
| test | unittest.TestCase | The test case object that is about to be executed. |
Returns
stopTest()
@classmethod
def stopTest(
test: unittest.TestCase
) - > null
Cleans up the logging environment by removing the SQL handler and optionally writing the captured SQL output to the result stream. This is called after each test completes to prevent log leakage between tests.
Parameters
| Name | Type | Description |
|---|
| test | unittest.TestCase | The test case object that has finished executing. |
Returns
addError()
@classmethod
def addError(
test: unittest.TestCase,
err: tuple
) - > null
Records a test error and appends the captured SQL debug information to the error record. This allows developers to see the database state and queries leading up to an unexpected exception.
Parameters
| Name | Type | Description |
|---|
| test | unittest.TestCase | The test case that encountered an error. |
| err | tuple | A tuple containing the exception type, value, and traceback. |
Returns
addFailure()
@classmethod
def addFailure(
test: unittest.TestCase,
err: tuple
) - > null
Records a test failure and attaches the captured SQL queries to the failure details. Use this to diagnose why assertions failed by reviewing the executed SQL statements.
Parameters
| Name | Type | Description |
|---|
| test | unittest.TestCase | The test case that failed an assertion. |
| err | tuple | A tuple containing the failure exception information. |
Returns
addSubTest()
@classmethod
def addSubTest(
test: unittest.TestCase,
subtest: unittest.TestCase,
err: tuple
) - > null
Handles failures or errors within a subtest by appending the current SQL log to the appropriate failure or error list. This ensures granular SQL debugging information is preserved for nested test logic.
Parameters
| Name | Type | Description |
|---|
| test | unittest.TestCase | The parent test case object. |
| subtest | unittest.TestCase | The specific subtest instance being executed. |
| err | tuple | The exception information if the subtest failed, or None if it passed. |
Returns
printErrorList()
@classmethod
def printErrorList(
flavour: string,
errors: list
) - > null
Outputs the list of errors or failures to the stream, including the captured SQL debug text for each entry. This overrides the default behavior to provide a detailed view of database activity alongside stack traces.
Parameters
| Name | Type | Description |
|---|
| flavour | string | A label describing the type of result, typically 'ERROR' or 'FAIL'. |
| errors | list | A list of tuples containing test instances, formatted error strings, and captured SQL text. |
Returns