Django-nose not creating test-only models during test runs
Asked Answered
C

2

8

I have django-nose 1.0 installed as the test runner for a Django 1.3.1 project. I'm following the instructions on the pypi page regarding test-only models.

Here is my settings.py testrunner config:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

I've run tests for several months using this testrunner without issue. Now I'm trying to test an abstract class, and I'm using a test-only model, but the specific test I've written throws an error.

According to the documentation, I only need to include the test class in one of the files that is imported during testing. I have the tests sitting in a 'tests' folder and broken out into several smaller testing files. Here is my tests/model_tests.py (models and app intentionally renamed for work reasons):

from django.tests import TestCase
from myapp.models import AbstractFoo

class Foo(AbstractFoo):
    pass


class TestFoo(TestCase):
    def setUp(self):
        self.foo = Foo.objects.create(name="Tester", 
                                      description="This is a test", ...)
    ... [tests follow]

I'm receiving an error in the first line of setUp:

DatabaseError: relation "tests_foo" does not exist
LINE 1: INSERT INTO "tests_foo" ("name", "description", "display...

And if I put a break point into the test and inspect the database, the table 'tests_foo' (or any table with 'foo' in the name) does not exist.

Any ideas about why the test-only model isn't loading?

Claire answered 30/7, 2012 at 22:55 Comment(1)
I'm getting the same error with 1.5 right now. How did you solve this, please?Belshin
H
0

Yup, seems like this is still a problem. Saw it with django==1.6 and django-nose==1.3

One workaround is to put all models in __init__.py in your tests/ folder

Relevant issue on GitHub: django-nose/issues/77

Heintz answered 7/1, 2015 at 19:44 Comment(0)
Y
0

You need to create model in test database, to do this you need to generate migration or create table in database manually. You can check my implementation of second variant https://github.com/erm0l0v/django-fake-model

This code should work as you expect:

from django.tests import TestCase
from myapp.models import AbstractFoo

from django_fake_model import models as f


class Foo(f.FakeModel, AbstractFoo):
    pass


@Foo.fake_me
class TestFoo(TestCase):
    def setUp(self):
        self.foo = Foo.objects.create(name="Tester", 
                                      description="This is a test", ...)
    ... [tests follow]
Ymir answered 30/8, 2017 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.