Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error importing Django, even when test doesn't use Django #1167

Closed
fkromer opened this issue Mar 15, 2018 · 8 comments
Closed

error importing Django, even when test doesn't use Django #1167

fkromer opened this issue Mar 15, 2018 · 8 comments
Assignees
Labels
bug something is clearly wrong here

Comments

@fkromer
Copy link
Contributor

fkromer commented Mar 15, 2018

If created a hypothesis test like follows:

import unittest
from hypothesis import given
from <custom-composite-strategy-module> import rc_visard_configurations

class TestComponentState(unittest.TestCase):
    @given(rc_visard_configurations())
    def test_config_does_not_crash_components(self, custom_composite_value_set):
        ...

If I run the test case with unittest I get an error which seem to relate to the Django test runner.

❯ python -m unittest test_fuzzying.TestComponentState.test_config_does_not_crash_components
E
======================================================================
ERROR: test_config_does_not_crash_components (test_fuzzying.TestComponentState)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_fuzzying.py", line 30, in test_config_does_not_crash_components
    def test_config_does_not_crash_components(self, rc_visard_config):
  File "/usr/local/lib/python2.7/dist-packages/hypothesis/core.py", line 978, in wrapped_test
    if bad_django_TestCase(runner):  # pragma: no cover
  File "/usr/local/lib/python2.7/dist-packages/hypothesis/internal/compat.py", line 530, in bad_django_TestCase
    from django.test import TransactionTestCase
  File "/usr/local/lib/python2.7/dist-packages/django/test/__init__.py", line 5, in <module>
    from django.test.client import Client, RequestFactory
  File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 11, in <module>
    from django.contrib.auth import authenticate, login, logout, get_user_model
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py", line 6, in <module>
    from django.middleware.csrf import rotate_token
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "/usr/local/lib/python2.7/dist-packages/django/utils/cache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "/usr/local/lib/python2.7/dist-packages/django/core/cache/__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

----------------------------------------------------------------------
Ran 1 test in 0.022s

FAILED (errors=1)

Have I missed something or is there some issue with the Django integration?

@Zac-HD
Copy link
Member

Zac-HD commented Mar 15, 2018

You haven't configured the SETTINGS module, or the config isn't being picked up. I'm not a django expert, but that's probably because you're not using the Django test runner (it's based on unittest, but not identical). We do test that Hypothesis works with the Django runner!

Regardless, it's not a Hypothesis bug so I hope that helps 😞

@Zac-HD Zac-HD closed this as completed Mar 15, 2018
@fkromer
Copy link
Contributor Author

fkromer commented Mar 16, 2018

The weird thing is that there is no Django used in this project at all. I have a Python package which uses pytest and other Python packages for blackbox testing of a remote system. I added Hypothesis tests to this package. (Right now I use unittest as test runner of the Hypothesis tests only because I am not sure about the compatibility of the pytest fixtures and hypothesis yet.)

@dchudz
Copy link
Member

dchudz commented Mar 16, 2018

@fkromer It looks to me like this really is happening because you have Django installed but not properly configured, as @Zac-HD said. You can work around it by either getting rid of Django from your virtual environment (seems like a good idea if it's not used in this project!) or configuring it correctly.

(I'm not saying it's not a bug. Indeed it seems possible we should consider this one, and thanks for reporting! Just suggesting a way out for you.)

It's coming from here: https://github.com/HypothesisWorks/hypothesis-python/blob/eb8da64f29991e2d557c079a53b33f30446e7e19/src/hypothesis/internal/compat.py#L530, where we import django stuff to check if your test case is a django test case. Unfortunate that importing that gives us an exception that isn't ImportError. ☹️ We handle ImportError in that code just fine.

@fkromer
Copy link
Contributor Author

fkromer commented Mar 16, 2018

@dchudz @Zac-HD Oh, I didn't ran it in a venv but the system level environment (I know, not good). Probably the previous owner of my development workstation had Django installed. Thanks!

@dchudz dchudz changed the title Django configuration error during test run with unittest error importing Django, even when test doesn't use Django Mar 17, 2018
@dchudz
Copy link
Member

dchudz commented Mar 17, 2018

@Zac-HD I think I want to reopen this, since really it would be better if our attempt to import Django didn't break us when the test doesn't want to use Django at all (in the non-ImportError case). (Or at least make it less surprising to users who don't understand why we're trying to do anything with Django.)

I know you do the most to keep our issues organized, so I'm happy to be overridden (especially if you have time to explain, but even if not).

I'm waffling between enhancement and bug for the tag, but went with the latter. Could be legibility too, if we opt for a clearer message but not actually avoiding the error. Happy to be overridden on any of that too.

@dchudz dchudz reopened this Mar 17, 2018
@dchudz dchudz added the bug something is clearly wrong here label Mar 17, 2018
@Zac-HD
Copy link
Member

Zac-HD commented Mar 18, 2018

Aahhhhh, I see the problem now. Thanks for investigating and reopening this @dchudz, I'll post a fix later today.

@Zac-HD Zac-HD self-assigned this Mar 18, 2018
@Zac-HD
Copy link
Member

Zac-HD commented Mar 18, 2018

Right now I use unittest as test runner of the Hypothesis tests only because I am not sure about the compatibility of the pytest fixtures and hypothesis yet.

You can definitely use Pytest and Hypothesis; just don't use function fixtures and @given on the same test 😄

@DRMacIver
Copy link
Member

You can definitely use Pytest and Hypothesis; just don't use function fixtures and @given on the same test 😄

Even this is fine really, depending on what you're doing with function fixtures. It's only a problem if you need set up/tear down per example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

No branches or pull requests

4 participants