Django Custom Test Runner

  1. Django Custom Test Runners
  2. Django Run Tests

Generally, PyCharm runs and debugs tests in the same way as other applications, by running the run/debug configurations you have created. When doing so, it passes the specified test classes or methods to the test runner.

In many cases, you can initiate a testing session from a context menu. For this purpose, the Run and Debug commands are provided in certain context menus. For example, these commands are available for a test class, directory, or a package in the Project tool window. They are also available for a test class or method you are currently working on in the editor.

If you run a test for which there is no permanent run/debug configuration, a temporary configuration is created. You can then save such a configuration using the Run/debug configuration dialog if you want to reuse it later.

In order to guarantee that all TestCase code starts with a clean database, the Django test runner reorders tests in the following way: All TestCase subclasses are run first. Then, all other Django-based tests (test cases based on SimpleTestCase, including TransactionTestCase ) are run with no particular ordering guaranteed nor enforced among them. Getting a Django Application to 100% Test Coverage. Code coverage is a simple tool for checking which lines of your application code are run by your test suite. 100% coverage is a laudable goal, as it means every line is run at least once. Coverage.py is the Python tool for measuring code coverage. Running the Test Server. Django is so much handy you will really like it as it provides you with the inbuilt test server. Django will change your habits of web development to the very core. It has given web-developers and test servers inside their pc where they can very easily run their Django projects. Introduction to Python/Django testing: Basic Unit Tests¶. Last post we talked about how to set up and use doc tests inside of Django. Today, in the second post of the series, we’ll be talking about how to use the other testing framework that comes with Python, unittest. Unittest is a xUnit type of testing system (JUnit from the Java world is another example) implemented in Python.

The tests run in the background, so you can execute several tests at the same time.

Django Custom Test Runners

Each running configuration gets its own tab in the Run tool window (the Test Results tab). One tab can aggregate several tests.

Django

Note also that the commands shown in the context menu, are context-sensitive, that is the testing command that shows depends on the test runner and the place where this command is invoked.

Run or debug a test

To start running or debugging a test, you can use the main toolbar or a context menu in the Project tool window or in the editor:

  • Use the main toolbar:

    1. Select the necessary run/debug configuration from the list on the main toolbar.

      Press Alt+Shift+F10 to see the list of available run configurations or Alt+Shift+F9 for debug configurations.

    2. Click Run or Debug to the right of the list. Alternatively, select Run | RunShift+F10 or Run | DebugShift+F9 from the main menu.

  • Use a context menu:

    • Right-click a test file or test class in the Project tool window or open it in the editor, and right-click the background. From the context menu, select Run <class name>/ Run <filename> or Debug....

    • For a test method, open the class in the editor and right click anywhere in the method. The context menu suggests the command Run / Debug <method name>.

And there is a fairly limited bunch of people contributing to both pytest-django and django's test framework. Third, pytest itself does not have complete feature parity with the basic run-tests options of Django's test framework. For example, Django has the -reverse flag to run tests in the reverse order.

Run all tests in a directory

  1. In the Project tool window, select the directory that contains tests to be executed.

  2. From the context menu, select the corresponding run command.

    If the directory contains tests that belong to the different testing frameworks, select the configuration to be used.

    For example, select Run pytest in <directory name>'.

  3. Explore results in the test runner.

For Django versions 1.1 and later, PyCharm supports custom test runner, if this test runner is a class.

By default, all tests are executed one by one. You can change this behavior for the pytest testing framework and execute your tests in parallel.

Run tests in parallel

Custom

Enable test multiprocessing to optimize execution of your pytest tests.

To explicitly specify the number of CPUs for test execution:

  1. Install the pytest-xdist package as described in Install, uninstall, and upgrade packages.

  2. Specify pytest as the project testing framework. See Testing frameworks for more details.

  3. Select Edit configurations... from the list of the run/debug configurations on the main toolbar. In the Run/Debug Configurations dialog, expand the Pytest tests group, and select pytest in <directory name>.

    PyCharm creates this configuration when you run all tests in a directory for the very first time. If you haven't executed the tests yet, click the icon and specify the run/debug Configuration parameters.

  4. In the Run/Debug Configurations dialog, in the Additional Arguments field specify the number of the CPUs to run the tests: -n <number of CPUs> and save the changes.

  5. Now run all the tests in the directory again and inspect the output in the Run tool window. In the shown example, The total execution time is 12s 79ms as compared to 30s 13ms when running the same tests consequentially. The test report provides information about the CPUs used to run the tests and execution time.

Alternatively, you can specify the number of CPUs to run your tests in the pytest.ini file. For example,

If you specify different values of CPU numbers in the pytest.ini file and the run/debug configuration, the latter takes precedence over the settings in the pytest.ini file.

If you can stop a running tests, all running tests stop immediately. Icons of tests in the Run tool window reflect status of the test (passed, failed, aborted).

Terminate test execution

Django Run Tests

  • In the Run tool window, click the Stop button .

  • Alternatively, press Ctrl+F2.

Last modified: 14 September 2021