Is it possible to run tests with Pycharm

Hello,

I’m trying to not only use Django but also understand the code of the framework. Usually, when I’m with a codebase that I don’t know, I run the tests, change things to see what happen and put a breakpoint and inspect the variables, … I use Pycharm to do that. But one problem I have with the Django code is that I don’t know how to configure Pycharm to run the tests.

I suppose the problem is related to the fact that Django tests are run through the script runtests.py . Has anyone run the Django tests with Pycharm? I’m mean to run the tests of the Django framework itself, I’m able to run the tests of projects that use Django without a problem.

If you are using the paid for version of Pycharm, then you can use it’s Django functionality to create run configurations.

Here is an example of mine whereby I’m specifically testing a single app, in my case, progress_tracking

If you’re using the community edition, then you can create a regular Python run configuration. Here is one that I just tested successfully. Do note that I am running this in Docker, hence my Python interpreter looks a bit funny. I can’t see why it wouldn’t work with a local Python interpreter.

If you want to test only a single app, then you can include the apps test as an argument like this:

python manage.py test my_app.tests

In this instance your app is called my_app and within your app you have a file called tests.py

Thanks for the response but I think I expressed myself wrong. I can run tests with Pycharm for a project y create using Django (where I add apps, models, …). But I can not configure Pycharm to run the tests of the Django project, for example, these https://github.com/django/django/blob/master/tests/asgi/tests.py

My purpose is to debug the tests, change the code to see what happens …

1 Like

I am trying to figure out the same thing @grouchoboy were you able to figure it out?

You can set up a “Python” run/debug configuration on the script tests/runtests.py. This will let you run all (or a subset) of Django’s tests in PyCharm’s run window, and also let you use PyCharm’s integrated debugger on the tests.

Just to be clear: this is for working inside Django’s own codebase (you forked github django/django). If you’re trying to use PyCharm with your own Django project, Conor’s earlier answer will give much better results.

(Unfortunately, I can’t get it to use PyCharm’s nifty test runner tabs for the runtests.py output. If anyone figures that out, please post it.)

Example:

The important parts are:

  • Working directory: $ProjectFileDir$/tests
  • Script: runtests.py (it’ll look for this in the working directory)
  • Script parameters: leave blank to run all tests, or enter a space-separated list of test modules/classes/cases you want to run. Examples:
    • asgi - run all tests in tests/asgi/*
    • asgi.tests.ASGITest.test_get_asgi_application - run a specific test case
    • cache.tests cache.tests_async - use spaces for multiple items
  • The defaults are fine for everything else.

(If you haven’t already, it’s a good idea to create a project-specific virtualenv and use that as your project interpreter.)

Before you can run the tests, be sure to follow the instructions in tests/README.rst to install the necessary requirements into your virtualenv.

When you run the tests, you should see a “Run” output panel something like this: