Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load fixtures from third-party app for django in tests?

I know fixtures can be loaded in the tests.py like this:

fixtures = ['example.json']

Django automatically search all the apps' fixture directory and load the fixtures.

However, I create a reusable app named accounts and in accounts I also have the fixtures/example.json file. But when I installed the accounts app and write it into the INSTALLED_APP settings, the fixture cannot be loaded. I am curious why it happens.

Django == 1.8.2

like image 689
Fu Jian Avatar asked Sep 01 '25 17:09

Fu Jian


1 Answers

You might be able to load those fixtures if:

  1. Your app fixtures are accessible, if the app is in the same project structure they are directly accessible
  2. If the app is installed by setup.py the files should be included with package_data={'mypkg': ['fixtures/*.json']} depending on your configuration
  3. You have to know where your virtualenv is located in the local system, this way you can tell Django in settings where fixtures are located, imagine virtualenv is located in parent directory from project, you can point to fixtures like this:

Settings.py:

ENV_DIR = os.path.join(os.path.dirname(__file__), '..')
FIXTURE_DIRS = (
   os.path.join(VENV_DIR, "virtualenv/lib/python2.7/site-packages/yourapp/test/fixtures"),
)

Note that it will depends on local system, not a clean solution, but it works, pay attention if you are running tests on remote systems like jenkins because you'll have to change this configuration according to the server deployment settings.

like image 88
danius Avatar answered Sep 04 '25 12:09

danius