FAQ Database Discussion Community


Unusual error in Django tests wrongly thinking instance doesn't exist

python,django,django-testing,django-tests
I have very customised Django app that checking to see if a user can change a ForeignKey to certain values. In this instance, a User belongs to a Workgroup and an Item can also belong to a Workgroup, and consequently, when a User makes an Item, they can only put...

Fixture inheritance in django TestCase

django,django-testing
I have a very simple scenario from django.test import TestCase class BaseTest(TestCase): fixtures = ('users.json',) ... class SpecificTest(BaseTest): fixtures = ('transactions.json',) ... transactions has an FK to users and when SpecificTest attempts to load the fixtures I get an IntegrityError IntegrityError: Problem installing fixtures: The row in table 'app_transactions' with...

Django Test Client not working with Database calls

django-testing
I'm using the django.test.Client to write tests for django's views In the Django's view: usr = User.objects.get(id=2) It does not returns the User object, instead raises error ObjectDoesNotExist. In dbshell same query works fine. What's the catch here ?...

Django get_signed_cookie on manually signed cookie

python,django,cookies,django-rest-framework,django-testing
I'm using get_signed_cookie in one of my views, and it works. The problem is in testing it, i'm manually signing my cookie in my test using Django's django.core.signing module with the same salt as my actual code (i'm using a salt that's in my settings). When i do non-automated tests,...

Django test RequestFactory vs Client

django,unit-testing,django-views,django-rest-framework,django-testing
I am trying to decide whether I should use Django's Client or RequestFactory to test my views. I am creating my server using DjangoRESTFramework and it's really simple, so far: class SimpleModelList(generics.ListCreateAPIView): """ Retrieve list of all route_areas or create a new one. """ queryset = SimpleModel.objects.all() serializer_class = SimpleModelSerializer...

Authentication failed in Django test

python,django,django-testing
In Django I tried to create a user and then I tried to login that user using selenium , but when I run the test it failed , It was showing authentication error. Here is my code : class LoginFunctionalTest(unittest.TestCase): def setUp(self): self.browser = webdriver.Firefox() self.browser.implicitly_wait(3) # self.browser.get('http://localhost:8000') def tearDown(self):...

Django testing wastes too much time on test database creating

python,django,testing,django-testing
We are try to cover tests on an old, big project which has more than 500 tables in database, and that waste too much time on database creating(more than 1 hour on my RMBP) and db migrations. We are using PostgreSQL as the Database, cause some GIS about service needs...

Django: How to associate a user with a request factory

python,django,testing,django-testing,django-tests
I am writing some tests for a form on a Django site. I want to test that a logged in user is able to use the form assuming correct input. To that end, I am using a Django request factory. I can't seem to get my test user logged in...

Write test for views containing os.remove in django

python,django,django-testing,django-tests
I have a function based view function in django that receives an ID from a model, retrieve a file address and delete it using os.remove image = Images.objects.get(id=image_id) os.remove(image.file) the image_id is valid and is a part of my fixture. what's the best way to write a test for this...

How to handle CommandError exceptions in Django tests

django,django-testing,django-manage.py,django-tests
I'm creating a Management Command in which I raise a CommandError exception when something goes wrong. One of my test cases executes the command in a way that it will raise the exception so the result of the test is Error. How can I make it pass?...

Using coverage, how do I test this line?

python,django,django-testing
I have a simple test: class ModelTests(TestCase): def test_method(self): instance = Activity(title="Test") self.assertEqual(instance.get_approved_member_count(), 0) My problem is that coverage still shows get_approved_member_count line as NOT tested: How do I satisfy the above for coverage? To run the tests I'm using Django Nose with Coverage: TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' NOSE_ARGS = [...

No module named .. while running Django tests with PyCharm

django,pycharm,django-testing
I have the following error when running Django's tests with PyCharm 4.5 ImportError: No module named date_utils. Here is a short overview the project tree: . ├── manage.py ├── settings.py ├── app1 │   ├── models.py │   ├── __init__.py #Empty ├── utils │   ├── __init__.py #Empty │   ├── date_utils.py In app1.models.py,...

Django test client does not handle exceptions?

python,django,http-status-code-500,django-testing
I've the need to write some tests for custom handler404 and handler500 in Django, using django test dummy client. The first is easy to test while I have problems with the second. Basically, problem is that Django test client does not catch the exception and does not route to the...

How to create table during Django tests with managed = False?

python,django,django-testing,django-1.7,django-migrations
From the oficial documentation: For tests involving models with managed=False, it’s up to you to ensure the correct tables are created as part of the test setup. I don't know how to create the tables as part of the test setup. I found this question and the accepted answer doesn't...

Django - Writing Tests: Unit testing extra variables passed to generic views

django-class-based-views,django-testing
I would like to know if there is a way to use Django's test client to verify that the extra variables that are passed from a generic view are correct. For example, given the code below, how would I write a test for list_year() to ensure that the template is...