Fixed #24118 -- Added --debug-sql option for tests.

Added a --debug-sql option for tests and runtests.py which outputs the
SQL logger for failing tests. When combined with --verbosity=2, it also
outputs the SQL for passing tests.

Thanks to Berker, Tim, Markus, Shai, Josh and Anssi for review and
discussion.
This commit is contained in:
Marc Tamlyn 2015-01-10 22:52:59 +00:00
parent 68a439a18d
commit b5c1a85b50
8 changed files with 204 additions and 16 deletions

View file

@ -286,7 +286,7 @@ For example, suppose that the failing test that works on its own is
.. code-block:: bash
$ ./runtests.py --bisect basic.tests.ModelTest.test_eq
$ ./runtests.py --bisect basic.tests.ModelTest.test_eq
will try to determine a test that interferes with the given one. First, the
test is run with the first half of the test suite. If a failure occurs, the
@ -302,7 +302,7 @@ failure. So:
.. code-block:: bash
$ ./runtests.py --pair basic.tests.ModelTest.test_eq
$ ./runtests.py --pair basic.tests.ModelTest.test_eq
will pair ``test_eq`` with every test label.
@ -313,7 +313,7 @@ the first one:
.. code-block:: bash
$ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
$ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
You can also try running any set of tests in reverse using the ``--reverse``
option in order to verify that executing tests in a different order does not
@ -321,8 +321,16 @@ cause any trouble:
.. code-block:: bash
$ ./runtests.py basic --reverse
$ ./runtests.py basic --reverse
If you wish to examine the SQL being run in failing tests, you can turn on
:ref:`SQL logging <django-db-logger>` using the ``--debug-sql`` option. If you
combine this with ``--verbosity=2``, all SQL queries will be output.
.. code-block:: bash
$ ./runtests.py basic --debug-sql
.. versionadded:: 1.8
The ``--reverse`` option was added.
The ``--reverse`` and ``--debug-sql`` options were added.

View file

@ -1449,6 +1449,14 @@ This may help in debugging tests that aren't properly isolated and have side
effects. :ref:`Grouping by test class <order-of-tests>` is preserved when using
this option.
.. django-admin-option:: --debug-sql
.. versionadded:: 1.8
The ``--debug-sql`` option can be used to enable :ref:`SQL logging
<django-db-logger>` for failing tests. If :djadminopt:`--verbosity` is ``2``,
then queries in passing tests are also output.
testserver <fixture fixture ...>
--------------------------------

View file

@ -625,8 +625,9 @@ Tests
allows you to test that two JSON fragments are not equal.
* Added options to the :djadmin:`test` command to preserve the test database
(:djadminopt:`--keepdb`) and to run the test cases in reverse order
(:djadminopt:`--reverse`).
(:djadminopt:`--keepdb`), to run the test cases in reverse order
(:djadminopt:`--reverse`), and to enable SQL logging for failing tests
(:djadminopt:`--debug-sql`).
* Added the :attr:`~django.test.Response.resolver_match` attribute to test
client responses.

View file

@ -439,6 +439,8 @@ Messages to this logger have the following extra context:
* ``request``: The request object that generated the logging
message.
.. _django-db-logger:
``django.db.backends``
~~~~~~~~~~~~~~~~~~~~~~

View file

@ -355,7 +355,7 @@ behavior. This class defines the ``run_tests()`` entry point, plus a
selection of other methods that are used to by ``run_tests()`` to set up,
execute and tear down the test suite.
.. class:: DiscoverRunner(pattern='test*.py', top_level=None, verbosity=1, interactive=True, failfast=True, keepdb=False, reverse=False, **kwargs)
.. class:: DiscoverRunner(pattern='test*.py', top_level=None, verbosity=1, interactive=True, failfast=True, keepdb=False, reverse=False, debug_sql=False, **kwargs)
``DiscoverRunner`` will search for tests in any file matching ``pattern``.
@ -386,6 +386,11 @@ execute and tear down the test suite.
and have side effects. :ref:`Grouping by test class <order-of-tests>` is
preserved when using this option.
If ``debug_sql`` is ``True``, failing test cases will output SQL queries
logged to the :ref:`django.db.backends logger <django-db-logger>` as well
as the traceback. If ``verbosity`` is ``2``, then queries in all tests are
output.
Django may, from time to time, extend the capabilities of the test runner
by adding new arguments. The ``**kwargs`` declaration allows for this
expansion. If you subclass ``DiscoverRunner`` or write your own test
@ -402,7 +407,7 @@ execute and tear down the test suite.
subclassed test runner to add options to the list of command-line
options that the :djadmin:`test` command could use.
The ``keepdb`` and the ``reverse`` arguments were added.
The ``keepdb``, ``reverse``, and ``debug_sql`` arguments were added.
Attributes
~~~~~~~~~~