3

I am trying to follow best practices which I often find in GitHub repositories: I would like to have a /src and a /test folder at the top level in a project. (random example https://github.com/bitcoin/bitcoin). I am not sure how to configure Django to accept that though.

In particular, Django is expecting tests to be inside the project folder, but ideally, these tests should be outside of /src/project_name and inside /test.

Any suggestions are greatly appreciated. Thank you so much!

project_root
  |-- src
  |   |-- project_name
  |       |-- app_name
  |       |   |-- views.py
  |       |   |-- serializers.py
  |       |   |-- etc...
  |       |-- manage.py
  |-- test
      |-- project_name
          |-- test_feature1.py
          |-- test_feature2.py
Andrei Cioara
  • 3,404
  • 5
  • 34
  • 62
  • The path /src/django-project doesn't exist in the above visualisation. And you may want to take a look at [this](https://stackoverflow.com/questions/22841764/best-practice-for-django-project-working-directory-structure). I think there are some configuring options deep in the post – PrinceOfCreation Feb 22 '19 at 14:27
  • What makes you think that "having a /src and a /test folder at the top level of a project" is a universal "best practice" ? How you layout your code depends on the kind of project (is it a lib ? a framework ? a plugin ? an application ?) and the techno(s) used - and of course on personal preferences -, not on some absolute, universal golden rule. Django expects your tests to be at some given places, so just put your tests there. – bruno desthuilliers Feb 22 '19 at 14:31
  • This could be incredibly useful. Our test files are very large and contain lots of data for mocking API calls etc. Having them all in an isolated `/tests` folder would make it much faster for us to copy changes to our production server. – Ethan Posner Aug 12 '22 at 20:54

2 Answers2

4

I found an answer! Use the pytest-django module. They have some documentation about changing your test directory through the pythonpath environment variable here. All of your django tests should also be backwards compatible with pytest, so no changes to the individual tests should be required.

So to test from the /test directory,

  1. Install pytest-django: pip install pytest-django (also make sure to add pytest-django==4.5.2 to your requirements.txt file). This also installs the latest version of pytest.
  2. Move all tests from your /src/<app_name>/tests/ directories into /test. It should look like the following:
project_root
  |-- src
  |   |-- project_name
  |       |-- app_name
  |       |   |-- views.py
  |       |   |-- serializers.py
  |       |   |-- settings.py
  |       |   |-- etc...
  |       |-- manage.py
  |-- test
      |-- test_feature1.py
      |-- test_feature2.py
  1. Create a pytest.ini file in the root directory of your project. It must specify the DJANGO_SETTINGS_MODULE and pythonpath environment variables:
[pytest]
DJANGO_SETTINGS_MODULE = app_name.settings
pythonpath = src test  # adds /src and /test directories to the pythonpath
  1. Run pytest (or python -m pytest). This replaces python src/manage.py test src.

Hope this helps!

Ethan Posner
  • 343
  • 1
  • 2
  • 14
0

I don't know, but there could be some configuring options over in this GitHub. As also mentioned, why can't the tests be where Django expects them? That way, if you work with other libraries in the future, there should be compatibility if they work with Django.

PrinceOfCreation
  • 389
  • 1
  • 12
  • Including tests as part of a handoff (commercial bundles) might be subject to restriction. I personally prefer my tests to live in an isolated directory where extended configuration and inheritance can be done throughout the entire project rather than mixing and matching testing solutions between integration areas and django apps. – whardier Jan 25 '23 at 00:23