Using Pytest with Django

Answered

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 `manage.py 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? 

6 comments
Comment actions Permalink
Official comment

Hello.

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: https://youtrack.jetbrains.com/issue/PY-19727

Comment actions Permalink

Thanks!

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

2) Use pytest run configuration, not Django tests.

8
Comment actions Permalink

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
Comment actions Permalink

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

thanks,

Eric

0
Comment actions Permalink

Hi Egrannan,

So, everything is correct according https://www.jetbrains.com/help/pycharm/pytest.html?
Would it be possible to attach a screenshot showing the issue and a screenshot of your Run/Debug Configuration?

0
Comment actions Permalink

Sergey Karpov Thanks for your answer!

 

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

0

Please sign in to leave a comment.