Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test error only with pycharm - Not the terminal | apps aren't loaded yet

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?

like image 347
Michael Bruce Avatar asked Jun 09 '16 15:06

Michael Bruce


People also ask

Is PyCharm and Django same?

Django belongs to "Frameworks (Full Stack)" category of the tech stack, while PyCharm can be primarily classified under "Integrated Development Environment".

What is Django TestCase?

The best base class for most tests is django. test. TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a user interacting with the code at the view level.


3 Answers

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.

like image 85
IVI Avatar answered Oct 05 '22 19:10

IVI


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.

like image 35
Matthew Hegarty Avatar answered Oct 05 '22 20:10

Matthew Hegarty


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

I faced similar issues with Behave tests...

like image 34
Edwin Avatar answered Oct 05 '22 21:10

Edwin