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

Tin Tvrtković tinchester at gmail.com
Mon Mar 24 16:25:47 PDT 2014


Thanks for the advice, Jakub, Eric and Chris.

I think I'm going with the test_x as package approach, with a test 
settings.py inside and importable.

I guess working with Maven has colored my thinking. For those who don't 
know, Maven basically allows you to have two sets of assets: 
src/main/{java,scala,resources} and src/test/{java,scala,resources}. For 
unit tests, both sets of directories get merged, but only the main set 
gets packaged and deployed. I've actually thought about playing with 
namespace packages, but since in Python the directory name basically 
determines the package name, I'd need to add another level of 
directories (i.e. main/x/__init.py__ and test/x/__init__.py, there 'x' 
is the package name) but I haven't followed through on this thought 
experiment. :)

As for having a single settings.py, this project is basically supposed 
to be two things:

1) an app for caching Bower packages
2) a way to quickly create a working Django project that uses #1, as a 
convenience to users

(I'm in the middle of a giant refactor to achieve this.)

The thing is, neither of these points need a proper settings.py, except 
for testing. :) #2 needs a settings.py template, which I'll swipe from 
Django proper and add the necessary extra settings and use with the 
startproject command, but I can't really use this for unit testing.

Thanks, folks!

On 24.03.2014 23:58, Chris Jerdonek wrote:
> My approach to Django's settings.py is to use a single settings.py for
> the project, and to have the settings.py file read from a YAML file
> for environment-specific values wherever necessary.  The path to the
> YAML file is read from an environment variable, so when developing and
> testing, you can set this environment variable to whatever is needed
> prior to calling `python manage.py`, etc.
>
> One benefit of this approach is that it lets the settings code be more
> DRY across environments.
>
> --Chris
>
>
>
> On Sun, Mar 23, 2014 at 3:44 PM, Tin Tvrtković <tinchester at gmail.com> wrote:
>> 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. :)
>>
>> Thanks in advance,
>> Tin
>>
>> _______________________________________________
>> testing-in-python mailing list
>> testing-in-python at lists.idyll.org
>> http://lists.idyll.org/listinfo/testing-in-python




More information about the testing-in-python mailing list