[TIP] pytest + Django: Where to put the test settings.py?

Eric Larson eric at ionrock.org
Mon Mar 24 09:35:07 PDT 2014


Tin Tvrtković <tinchester at gmail.com> writes:

> Hi everyone,
>
> this little project of mine (https://github.com/Tinche/bower-cache) uses
> Django, together with pytest for testing. I use the first layout from
> http://pytest.org/latest/goodpractises.html; a separate top-level
> directory for tests, and I'd like to keep my test data and the actual
> app code separate.
>
> I do, however, need a valid Django settings.py in order to run the
> tests. My first instinct was to put it into the tests directory and
> refer to it as tests.settings in my pytest.ini, but this doesn't work
> unless I make the tests directory a proper Python package (with
> __init__.py) which pytest explicitly suggests to avoid. The other option
> would be to put it somewhere into the app itself, but then this means
> I'm mixing test and normal code, which is something I'm trying to avoid
> in the first place.
>
> I was wondering if there was a generally-accepted way of solving this
> problem. Any advice from the community would be appreciated. :)
>

We add in the top level conftest.py:

  import os


  def pytest_configure():
      os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

I'm not sure if your pytest.ini file is essentially doing the same thing
or not.

HTH,

Eric

--
Sent with my mu4e



More information about the testing-in-python mailing list