13
votes

Using Pycharm, I get some different results, then running commands from the terminal. I noticed that after I updated my view, I got a "Apps aren't loaded yet" exception ONLY when running the test from the IDE.

If I run the test via terminal "python3 manage.py test", there is no issue.

IDE runs tests no problem with this view file

def wine_data(request):
    return HttpResponse("<html><title>Wine Data</title></html>")

If I change the view file to this:

def wine_data(request):
    return render(request, 'wine_data.html')

I get this error (only when ran from IDE)

/home/codeamend/Projects/python/OldBullTavern/venv/bin/python /opt/pycharm-professional/helpers/pycharm/utrunner.py /home/codeamend/Projects/python/OldBullTavern/obt/wine/tests.py true Testing started at 11:38 AM ...

Error Traceback (most recent call last): File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/utils.py", line 86, in getitem return self._engines[alias] KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/codeamend/Projects/python/OldBullTavern/obt/wine/tests.py", line 16, in test_wine_data_loads_correct_html response = wine_data(request) File "/home/codeamend/Projects/python/OldBullTavern/obt/wine/views.py", line 6, in wine_data return render(request, 'wine_data.html') File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/shortcuts.py", line 67, in render template_name, context, request=request, using=using) File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/loader.py", line 96, in render_to_string template = get_template(template_name, using=using) File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/loader.py", line 26, in get_template engines = _engine_list(using) File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/loader.py", line 143, in _engine_list return engines.all() if using is None else [engines[using]] File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/utils.py", line 110, in all return [self[alias] for alias in self] File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/utils.py", line 110, in return [self[alias] for alias in self] File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/utils.py", line 101, in getitem engine = engine_cls(params) File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/backends/django.py", line 31, in init options['libraries'] = self.get_templatetag_libraries(libraries) File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/backends/django.py", line 49, in get_templatetag_libraries libraries = get_installed_libraries() File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/template/backends/django.py", line 132, in get_installed_libraries for app_config in apps.get_app_configs()) File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/apps/registry.py", line 137, in get_app_configs self.check_apps_ready() File "/home/codeamend/Projects/python/OldBullTavern/venv/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Process finished with exit code 0

Any Ideas?

3
So, what you're looking for is to be able to run an unittest (involving Django stuff) from your IDE (as you would run it via manage.py test). How are you attempting to run it (from the IDE console or by right clicking it)? I guess you have Pycharm community (unpaid) edition?CristiFati
FWIW, I'm hitting this in the Professional edition of PyCharm that worked last week. Still working it out.LarrikJ
Well, downgrading from 2017.1.3 to 2017.1.2 fixed mine, so that's not helpful.LarrikJ

3 Answers

3
votes

This seems like a Virtual Environment loading error. Have you applied the correct Virtual Environment to your testing env? Check this article and try running it again. http://exponential.io/blog/2015/02/10/configure-pycharm-to-use-virtualenv/

Also on another note, if you're testig for correct template loading or GET requests feel free look at my tutorial.

1
votes

Do you have the correct "Working directory" values and "Target" for your tests?

I faced similar issues with Behave tests...

1
votes

This worked for me:

  1. Run the test (it fails with the error)

  2. Click the dropdown next to the test name and select 'Edit configurations'

  3. Click on the test name in the left hand panel of the 'Run/Debug configurations' dialog.

  4. Click the minus sign to delete the test. Click OK

  5. Now re-run the test and it passes for me.

My hunch is that dodgy test config gets cached under .idea/ and explicitly deleting the test removes or updates it.

Another workaround is to manually create a Django test:

  1. Edit Configurations

  2. Add new configuration (click the '+' button)

  3. Select 'Django Tests'

  4. Under 'Target', enter the path to the test module (e.g. 'your_app.your_module.tests')

  5. Click OK, and you should be able to run this configuration.