Repository: HypothesisWorks/hypothesis

Issue:
error importing Django, even when test doesn't use Django
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?


Discussion:
You haven't configured [the `SETTINGS` module](https://docs.djangoproject.com/en/2.0/topics/settings/), 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](https://docs.djangoproject.com/en/2.0/topics/testing/overview/#running-tests) (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 :disappointed: 
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.)
@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.
@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!
@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.
Aahhhhh, I see the problem now.  Thanks for investigating and reopening this @dchudz, I'll post a fix later today.
> 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 :smile: 

Resolving patch:
diff --git a/src/hypothesis/internal/compat.py b/src/hypothesis/internal/compat.py
--- a/src/hypothesis/internal/compat.py
+++ b/src/hypothesis/internal/compat.py
@@ -530,6 +530,7 @@ def bad_django_TestCase(runner):
             from django.test import TransactionTestCase
             from hypothesis.extra.django import HypothesisTestCase
             _cases[:] = TransactionTestCase, HypothesisTestCase
-        except ImportError:
-            _cases[:] = type, type
+        except Exception:
+            # Can't use ImportError, because of e.g. Django config errors
+            _cases[:] = (), type
     return isinstance(runner, _cases[0]) and not isinstance(runner, _cases[1])


Test patch: