Understanding the "Django tests" configuration

Answered

When I want to run tests on my Django project, I can easily go to the command line and run `python manage.py test`. I can also run tests on specific packages (and functions) with `python manage.py test some.package.name`.

How does that directly translate to setting up a "Django tests" configuration? It doesn't seem immediately obvious, or clear, and so I'm just using a generic Python configuration to run manage.py (which works reasonably well).

10 comments

If you create a Django Test run configuration, then you'll inherit the extra tooling we have for test-oriented run windows. (Sidenote: I really need to adjust the audio levels in those screencasts.)

0

I looked at the video links, but they are for generic Python testing. I'm specifically referring to the "Django tests" configuration. I really am looking for a dead-simple explanation of how to translate testing my Django project (which I do now by simply running `python manage.py test` in a shell) into the "Django tests" configuration. Seeing how simple running the tests are on the command line, I was hoping the "Django tests" configuration would also be simple, but it is not. I've seen the reference you linked to, but it doesn't give me any simple, clear explanation.

Perhaps that is not what the "Django tests" configuration is for, despite the name, and I should continue to use my pure "Python" configuration?

0

Sorry, it wasn't clear to me what stage you were at on doing test running in PyCharm. I'm still not quite sure which direction to take the answer. Did you follow the instructions on creating a "Django Tests" run configuration and ran into a bug? Or you're asking more of a question about the concept itself?

I'll presume from your answer that you understand the concept of running tests in PyCharm, it's just the Django part that you need.

As a suggestion, I ran the official Django cookiecutter and here a screenshot:

The cookiecutter is a way to get you directly to the end state you're looking for: a Django project, with some models and views, with some working tests, and most importantly, with everything wired up to open it in PyCharm and click the green button to run the tests.

Alternatively, go to Edit Configurations, click the +, choose Django Tests, and fill it in to look similar to my screenshot. You can then run that run configuration and it will perform like the normal PyCharm test running that you're famiiliar with.

0

I still have not been able to get this to work. When I run the "Django tests" configuration I've created, which looks virtually identical to the Python configuration I've been using to run tests, I get this error message block once for each of my test modules:

Error
ImportError: Failed to import test module: ...some_test_module_...
Traceback (most recent call last):
File "c:\python36\Lib\unittest\loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "c:\python36\Lib\unittest\loader.py", line 369, in _get_module_from_name
__import__(name)
...
from django.contrib.auth.models import Permission
File "... \lib\site-packages\django\contrib\auth\models.py", line 6, in <module>
from django.contrib.contenttypes.models import ContentType
File "... \lib\site-packages\django\contrib\contenttypes\models.py", line 138, in <module>
class ContentType(models.Model):
File "... \lib\site-packages\django\db\models\base.py", line 113, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

 

My other configuration basically just runs `python manage.py test` (as one would on the command line) without issues.

Why this doesn't work out of the box is very frustrating.

0

Hi, i run into the same problem. In my case i didnt have django settings module set. Try to add this variable DJANGO_SETTINGS_MODULE in your Django Test Configuration into Enviroment variables.

0

I already have that set under Settings -> Languages and Frameworks -> Django -> click project -> Settings

Setting it a second time as an environment variable has no effect. Neither does also setting it in Configuration -> Custom Settings

Why does this question show up as "answered" when it is not?

0

One year later: different version of PyCharm, different version of Django, still same error message on every test: "RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."

0

I had the exact same problem and was able to solve it by adding more "Environment variables" in the "Run/Debug Configurations" window.
In my case I was missing SECRET_KEY and DATABASE_URL.

0

Thanks for the comment, Daniel. I already have a number of settings like that defined in a .env file (including PORT, DJANGO_LOG_LEVEL, etc.) which then gets read by manage.py using the dotenv package, so they're already present whenever I use manage.py. As a test, I added SECRET_KEY and DATABASE_URL explicitly as env vars to my configuration, and it didn't make a difference for me: I still get the same error message.

0

If you are still having this problem, I just ran through the same symptoms. In the end, I found under the Project Structure, my Modules configuration for Django was wrong. The settings file and project root were incorrectly defined. Hope it helps!

 

0

Please sign in to leave a comment.