Using Pytest with Django


When using Pytest with Django, anywhere that a dynamic test configuration would be generated (e.g. right-click a test case and "Run test"), the default ` test` is always run. This conflicts with pytest, which expects to be called as `pytest test.module` even when running with the django integration.

Is there a way to have Pycharm play nicely with Pytest? 

Official comment


If you have test case extending Django test case PyCharm will always use Django test runner, but you can create run configuration manully.


There is an issue about it, please vote for it:

Has this issue been solved? I'm trying to debug django on PyCharm using pytest, but even though I've set the Default test runner to be pytest, it still doesn't seem to use it.

I'm using PyCharm Professional 2018.2.4




Hi Egrannan,

So, everything is correct according
Would it be possible to attach a screenshot showing the issue and a screenshot of your Run/Debug Configuration?


I believe it's set up according to that page.Under Settings -> Python Integrated Tools I have pytest selected as the default test runner. From the command line running pytest works fine to run tests.

The problem seems to be that under pycharm the test database isn't being initialized for django. The error I get is:

django.db.utils.ProgrammingError: (1146, "Table 'test_srprdsdb.commod' doesn't exist")

Here's a screenshot of the run/debug configuration:





1) Enable Do not use Django test runner under Settings | Languages & Frameworks | Django.

2) Use pytest run configuration, not Django tests.


Sergey Karpov Thanks for your answer!


However, that doesn't help much, because when you click on a test package in PyCharm (say in ""), then PyCharm will run the package with "Django test" run configuration, not pytest.


Please sign in to leave a comment.