-
Notifications
You must be signed in to change notification settings - Fork 347
Should automatically call django.setup() on django 1.7 #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ran into this too today, fixed in conftest also. |
Thanks for the report. The issue is a bit odd though, calling django.setup() is already done before any test runs: https://github.com/pelme/pytest_django/blob/master/pytest_django/plugin.py#L129 Can you provide some additional context where this happens? Please provide the full py.test command output |
On Django 1.7c1 getting |
@pelme I just ran into this as well. The cursor wrapper referred to there happens during test running correct? During test discovery for example, if you're importing models and something funny is going on then you can run into some crazy corner cases. I ran into this by having models that use django-simple-history. This happened on django 1.7c1 |
There probably are/will be a ton of problems with third party Django apps that does not support Django 1.7 properly with regard to the new app loading. If The solution is to defer any actions that require access to other applications to I discussed this briefly with Aymeric Augustin during Djangocon, and that is the advice he gave to the best of my understanding for how to port applications to Django 1.7. If you are unsure whether a problem with app cache is a bug in your code, a third party app or pytest-django, please provide full tracebacks of the failures and we can discuss each case. Please provide the entire terminal output to make it easier to understand where things go wrong. |
So here's an example that I'm running into.
so ya, simple_history is messing with the app_configs dictionary but during normal use it ends up working because models aren't imported until after AppConfig has been setup. In https://github.com/pelme/pytest_django/blob/master/pytest_django/plugin.py#L76
then that particular set of errors goes away (I'm fighting a thing with FactoryBoy as well...) |
Added ropeproject folders to gitignore, Added call to setup() during loading. This FAILS some tests, but I'm posting it up to get some help with those failures.
Hmm. It looks like django-simple-history is intentionally hooking into the app config even when it is not ready, to be able to dynamically create the history model. It also looks like django-simple-history is using some kind of heuristic to find the proper module for the models. This might be the problem in this case since you are not using a plain models.py. I am not familiar with django-simple-history internals, but it uses internal Django API:s and uses a lot of meta things. This might be an edge case that breaks with how it detects models, but I think it can possibly be fixed by moving some parts of the initialization to This bug might be an error of implicit module loading (the very error that the Django 1.7 app loading should fix). This may work when invoked from runserver just because of the import's happens in another order then, which does not trigger the error. What happens if you just run a python shell (not manage.py shell, just python) and run "from form5500 import models as f5500_models" ? Does that work? Could you open an issue on django-simple-history to see if they think this is a bug? |
(Btw, just running setup() whenever pytest-django is loaded will not work. pytest-django is very careful to not load Django when Django is not imported, and this would just break that feature. setup() is called properly, but after test collection, but always before a test runs) |
Oh, darn. I didn't realize that was a very deliberate feature. I'll close my PR then since it's completely the wrong solution in that case. On that feature though, doesn't the normal django test running fully load settings then do test discovery? As for importing those models through the normal shell I get I guess the end result for my tests is just update my conftest.py module to force loading of django settings before test discovery? Or should I update my PR to make that an option? I have a feeling that during the whole 1.7 upgrade there are going to be a lot of third party modules that have these kinds of issues. |
Do you mean that this is something that pytest-django could do? @paulcollinsiii |
@blueyed No, I mean that django-simple-history should probably move any initialization that depends on having app config ready from import time to its I left a comment in the django-simple-history issue too. I think that the order of things should be
The "Import things" step can be omitted when running a web server for instance, since you do not really care about the modules, and django.setup() will import all things needed. This is currently how it works in pytest-django. If anyone else is having 1.7 issues, please post test outputs and we can try to debug/discuss them to find the root cause. |
Okay so I'm still in a bit of a quandary as to what to do. py.test goes through and loads my tests, which in turn see my factory_boy tests which then go out to my models. If I guess my point is, if you're using factory boy with your tests, or if your tests import your models then in django 1.7+ that loading order breaks AppConfig slightly. Probably not noticeably initially except in these corner cases. In pre-1.7 cases I can see why you don't want django to go through all it's import machinations during test discovery but in an AppConfig world even in the case of unit tests it makes sense to me to have django load all it's settings early. It's also entirely possible I'm missing an obvious use case here, but if not then I'm happy to go and re-work my PR a bit to get the tests passing and have setup() called before test discovery. |
@pelme I'm sorry, but I haven't explained this sufficiently clearly when we talked.
If you run into However, if pytest imports modules and runs into I would encourage everyone attempting to debug this to run |
@aaugustin Thanks for clearing it up. I will look into this to change to that behavior. |
This patch is still not working properly with regard to loading settings from within the test directories.
Thanks everybody for helping out with this.
Please open new issues if there are still problems! |
I just pulled in the latest commit. My import os
from django import setup
def pytest_configure():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
setup() If i leave out the django.setup part, I get this when running py.test:
|
I see, configuring DJANGO_SETTINGS_MODULE in pytest_configure is not really supported/tested at the moment, but should be easy to support properly. Also, you should not need to call setup() explicitly then, pytest-django could take care of it. I opened a new issue to track this this: #146. |
@dustinfarris Out of curiosity, why do you need to set |
@ionelmc to offload the sys.path boilerplate to pytest. |
@ionelmc I guess I put it elsewhere, (tox.ini, etc..), but since conftest.py is already there, why not use it. |
@dustinfarris I was intrigued, cause I always put it in tox.ini as I test with a single project. |
I am closing this issue since it has been fixed basically and new issues should be / have been opened separately. |
For now I have this in my conftest.py:
Without it I have these mind-boggling errors (on django 1.7b4):
The text was updated successfully, but these errors were encountered: