Skip to main content

DebugSQLTextTestResult

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

AttributeTypeDescription
loggerlogging.LoggerThe logging instance for 'django.db.backends' used to capture database queries during test execution.
handlerlogging.StreamHandler = nullA stream handler that temporarily stores captured SQL output for the current test case.

Constructor

Signature

def DebugSQLTextTestResult(
stream: any,
descriptions: any,
verbosity: any
)

Parameters

NameTypeDescription
streamanyThe stream where the test results will be written.
descriptionsanyA flag indicating whether to include descriptions of the tests.
verbosityanyThe 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

NameTypeDescription
testunittest.TestCaseThe test case object that is about to be executed.

Returns

TypeDescription
null

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

NameTypeDescription
testunittest.TestCaseThe test case object that has finished executing.

Returns

TypeDescription
null

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

NameTypeDescription
testunittest.TestCaseThe test case that encountered an error.
errtupleA tuple containing the exception type, value, and traceback.

Returns

TypeDescription
null

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

NameTypeDescription
testunittest.TestCaseThe test case that failed an assertion.
errtupleA tuple containing the failure exception information.

Returns

TypeDescription
null

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

NameTypeDescription
testunittest.TestCaseThe parent test case object.
subtestunittest.TestCaseThe specific subtest instance being executed.
errtupleThe exception information if the subtest failed, or None if it passed.

Returns

TypeDescription
null

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

NameTypeDescription
flavourstringA label describing the type of result, typically 'ERROR' or 'FAIL'.
errorslistA list of tuples containing test instances, formatted error strings, and captured SQL text.

Returns

TypeDescription
null