-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
Models located in tests.py are not created #15
Comments
I've mitigated the problem in one of our projects by simple defining my own testrunner that adds the required behaviour:
|
Now that |
Hm, looks strange, but issue still exists I created fresh application with following tests.py content
with django_nose.NoseTestSuiteRunner receive: DatabaseError: no such table: testapp_testmodel with standard django test runner tests passed i used latest django-nose version from git |
Hm, ever work around not helps. Checkout: For me "./manage/py test" not succed neither for jbalogh or rozza django_nose But it works for with usual django test runner (just comment out the TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' line in settings.py) |
kmmbvnr - for my hack to work you need to add a setting: |
I stumbled on this too. I tried what @shanx did but it didn't work. Here's how I fixed it for my project:
@jbalogh, what do you think? Should this be part of stock django_nose? Wanna pull request? |
Sorry. I spoke to soon. This subclass gets app discovery wrong. What doesn't work even with this is the ability to delete instances of models defined and created inside the tests. Suppose, again, that you have this test:
The problem appears to be because related objects in the models The error I get is this one:
and as far as I can work out this happens because, the django.db.base.Model's delete method tries, incorrectly, to extract all related models which it does by a cache proxy pattern. What it finds is a related model for the model created in (any)
and it has, as you can expect two fields: one
It has just one field which is a
If you do the above hack the internal cache gets correctly set before the actual test runner starts. So, it seems that there is something non-trivial that |
The root cause is that django-nose runs database setup in a nose plugin's begin method, and that happens before tests get collected. Your tests.py files haven't been imported so the models don't exist and they won't get db tables.
|
My hack / fix has been moved to: https://github.com/sbook/django-nose/commit/2cbc87170eb99df162d87ef040c9ae429dfea182 and requires |
@rozza can you see if your use case works in master? I think we covered all of ours and it should also automatically pick up test-only applications assuming they're structured like a normal Django app |
@dcramer: I tried out master with my models-in-tests.py project and they're not picked up. I can't find anything with grepping and reading the readme that suggests how to set it up. Separate app? But that means I have to put it in the INSTALLED_APPS list (which I don't want). And I can't find something like @rozza's TEST_INSTALLED_APPS. |
I'd just like to reask @reinout 's question. Is there a way to get test-only models created without putting the test app in |
I'm having a swing at this. |
I am sorry guys, but it still does not work for me, still get the error message, that the underlying model table does not exist. Am I supposed to use any specific nose TestCase class or can I use the standard Django TestCase class? |
@schacki The updates worked fine for me, test models are loading in as expected from tests.py. Make sure the updated package is the one being read on your pythonpath - if you are using virtualenv, for example, you might still have the old django-nose in your site-packages or something and it may be taking precedence over a localized copy of the new version. Also make sure any old .pyc files are cleaned up. P.S. thanks dev's - this was an enormous help! |
not working for me. test_models.py: from django.db import models
from django.test import TestCase
class MyModel(models.Model):
one_field = models.CharField(max_length=100)
class MyModelTest(TestCase):
def test_create_succeed(self):
MyModel.objects.create(one_field='aaa') output: DatabaseError: relation "tests_mymodel" does not exist
LINE 1: INSERT INTO "tests_mymodel" ("one_field") VALUES ('aaa') RET... |
I am doing further tests, and I observed some things.
if I run
Both commands doesn't works. I believe it is somehow related to #77 |
@zvictor I'm experiencing that behavior. Models in tests.py will work if I do |
Confirming this issue. Works Does not work Will only get Relevant environment Python 2.7.3 |
Digging through nose, the problem is that if you don't specify the modules to run the tests on, nose uses nose.loader.TestLoader.loadTestsFromDir() to do the discovery and creation of nose.suite.ContextSuite(). This function is a generator, so it does not import all the tests up front. This means that database setup happens before all the tests have been found. If you specify the modules up front that you want to test, then nose creates all the nose.suite.CreateSuite() objects up front via nose.loader.TestLoader.lostTestsFromModule(). Database setup then happens after this. The original fix for this issue only fixed creating the test models in this case. I think the fix for the test discovery case would be to add a new plugin to django-nose that provides a non-lazy version of loadTestsFromDir(). |
Is a plugin really the best option, seeing that this is just plain I think, test models are mostly important for reusable apps. Should
|
What makes nose's behaviour incorrect though? There's nothing wrong with being lazy in the test discovery and evaluation in the general case, it's only with Django that we care about doing the test discovery upfront because we only want to do the database setup and teardown once, not per test. If I were nose, I don't see why I would want to change the behaviour just for Django. |
Nose is being inconsistent right now. Run tests one way, you get one
|
I get your point now. I'm not trying to say that this is an upstream issue from nose. I fear I've used the name "nose" instead of "django-nose". I apologize for the confusion. That said, a plugin seems like a fine idea. |
This is the docstring of loadTestsFromDir:
I am afraid that importing modules upfront is going to cause some unexpected behavior for anything upstream that depends on this. I don't think there is an option here, and I believe the risk is neglectible. I'm going to try to fix this and pull request anyway, to see what everyone else thinks. This problem has made me clutter my fabfiles for long enough. |
I've made a pull request #111 to fix this. If anyone could try it out and give feedback as to whether it fixes their tests, it would help integration. |
Adding an app_label to the test only model fixes the problem for me. |
The issue is closed, but I still experience this problem. It only works for me when I specify the app I want to test. |
@GeorgeErickson very interesting. Maybe there's a better fix to be made for this issue. |
How is this supposed to work? it doesn't seem to work for me. in my
I do
I have a recent pypi install of django-nose |
I could only get this working by this hack: which is |
Hello, Is this feature working? The README says so but I'm struggling to test a simple abstract model.
My file structure:
# In models.py:
class OwnedModel(models.Model):
owner_user = models.ForeignKey(User, null=True,
related_name="%(app_label)s_%(class)s_ownerships")
class Meta:
abstract = True
# In tests/test_models.py:
class _OwnedModelTestModel(OwnedModel):
class Meta:
app_label = 'app_name'
class OwnedModelTestCase(TestCase):
def setUp(self):
self.instance = _OwnedModelTestModel()
self.instance.save() The traceback:
|
I am getting the exact same error with @anentropic . Should we just try to hack it for now? |
@anentropic and @cansin, the app_label attribute needed to be of an existing app in the project, so using |
Guys, do we have a fix for this now? Reiterating the question - |
Reopening since many people struggle with this. For my own reusable apps, I add a sample app and project to the repo, and run django-nose against the sample project. See the django-nose repo, and https://github.com/jwhitlock/drf-cached-instances. Because there are work arounds, this feels like a feature request, not a bug. |
If this is considered a feature request, than perhaps the documentation should be updated. The docs specify that this behavior works while clearly there are some caveats: |
Is there any traction on this as still getting the error. The docs say it's possible. Normal django tests do it. It's clearly not a feature but a bug as it goes against the documentation and expected behaviour. |
Use coverage native implementation instead of using django_nose package, due to custom model creation is not working (know issue) jazzband/django-nose#15 Add custom testing settings implementation in b2wars.settings.test
Use coverage native implementation instead of using django_nose package, due to custom model creation is not working (know issue) jazzband/django-nose#15 Add custom testing settings implementation in b2wars.settings.test
Models that exists only for testing purpose are not created when tests running by django_nose
Problem exists because setup_databases called before any test module have been imported.
This could be fixed by moving setup_databases call into prepareTest in Plugin
The text was updated successfully, but these errors were encountered: