django-testing

Django Rest Framework testing save POST request data

与世无争的帅哥 提交于 2019-12-04 06:44:14
问题 I'm writing some tests for my Django Rest Framework and trying to keep them as simple as possible. Before, I was creating objects using factory boy in order to have saved objects available for GET requests. Why are my POST requests in the tests not creating an actual object in my test database? Everything works fine using the actual API, but I can't get the POST in the tests to save the object to make it available for GET requests. Is there something I'm missing? from rest_framework import

Difference between TestCase and TransactionTestCase classes in django test

会有一股神秘感。 提交于 2019-12-04 02:45:47
Can you please anyone explain the difference between TestCase class and TransactionTestCase class. I have read the document but its only saying that TestCase run test in DB transaction and uses rollback to 'undo' the test in the DB and If you need to manually manage transactions within your test, you would need to use django.test.TransactionTestCase. Will you please help me to understand the actual difference with an example? I just want to know in what condition the TestCase fails? also whether the rollback are taken place automatically or we have to write statement for rollback? Please help

Django 1.3: Outbox empty during tests

痴心易碎 提交于 2019-12-04 00:40:49
Maybe I don't understand how outbox works but from the documentation I understood that it just catches all outgoing mail during testing. I created a new project with a new application and added the following code. from django.test import TestCase from django.core.mail import send_mail, outbox class SimpleTest(TestCase): def test_basic_addition(self): send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False) self.assertEqual( len( outbox ), 1 ) When I run python manage.py test app_name it gives an assertion error that 0 != 1. Am I doing

Django test client response context None

南楼画角 提交于 2019-12-03 23:21:40
I have moved my Django app from my development machine (OS X, Python 2.6.5, Django 1.2.3) to a staging server (Ubuntu VM, Python 2.6.6, Django 1.2.3). If I now run my test suite on the staging server, two tests fail when using the Django TestClient because response.context is None (but response.content is correct). For example: self.assertEquals(self.session.pk, response.context['db_session'].pk) These test cases pass on the development machine. Has anybody encountered similar problems? Joe J You need to add the test setup statement. import django django.test.utils.setup_test_environment()

How to test a Django model with pytest?

一世执手 提交于 2019-12-03 17:47:31
问题 I am getting started with pytest. I have configured pytest, anyway I couldn't found a resource on Django specific testing with pytest. How do I test a model with pytest_django ? I have already asked a question on unittesting, how do I efficiently test this Django model? I want know how the same tests can be written with py.test? adding below the model and the tests written in unittest. the model under test is, class User(AbstractBaseUser, PermissionsMixin): username = models.CharField(max

difference between django.test.TestCase vs unittest vs django.utils.unittest.TestCase

半腔热情 提交于 2019-12-03 16:30:04
问题 I am still using Django 1.2.1, and I think with the newer Django we don't import unittest and then do unittest.TestCase . Illustration import unittest class TestThis(unittest.TestCase): from django.utils.unittest import TestCase class TestThis(TestCase): from django.test import TestCase class TestThis(TestCase): According to PyCon2011 talk, the second one is slightly more efficient. Here is the diagram showing the relations: So django.utils.unittest and django.test inherit from either

Django testing stored session data in tests

夙愿已清 提交于 2019-12-03 16:06:46
问题 I have a view as such: def ProjectInfo(request): if request.method == 'POST': form = ProjectInfoForm(request.POST) if form.is_valid(): # if form is valid, iterate cleaned form data # and save data to session for k, v in form.cleaned_data.iteritems(): request.session[k] = v return HttpResponseRedirect('/next/') else: ... else: ... And in my tests: from django.test import TestCase, Client from django.core.urlresolvers import reverse from tool.models import Module, Model from django.contrib

Configure Django to find all doctests in all modules?

纵然是瞬间 提交于 2019-12-03 12:12:05
If I run the following command: >python manage.py test Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so: #tests.py from myapp.module1 import _function1, _function2 __test__ = { "_function1": _function1, "_function2": _function2 } If I want to include more doctests, is there an easier way than enumerating them all in this dictionary? Ideally, I just want to have Django find all doctests in all modules in the myapp application. Is there

How to use Django's assertJSONEqual to verify response of view returning JsonResponse

此生再无相见时 提交于 2019-12-03 09:51:05
I'm using Python 3.4 and Django 1.7. I have a view returning JsonResponse . def add_item_to_collection(request): #(...) return JsonResponse({'status':'success'}) I want to verify if that view returns correct response using unit test: class AddItemToCollectionTest(TestCase): def test_success_when_not_added_before(self): response = self.client.post('/add-item-to-collection') self.assertEqual(response.status_code, 200) self.assertJSONEqual(response.content, {'status': 'success'}) However the assertJSONEqual() line raises an exception: Error Traceback (most recent call last): File "E:\Projects

How to test a Django model with pytest?

一笑奈何 提交于 2019-12-03 06:33:32
I am getting started with pytest. I have configured pytest, anyway I couldn't found a resource on Django specific testing with pytest. How do I test a model with pytest_django ? I have already asked a question on unittesting, how do I efficiently test this Django model? I want know how the same tests can be written with py.test? adding below the model and the tests written in unittest. the model under test is, class User(AbstractBaseUser, PermissionsMixin): username = models.CharField(max_length=25, unique=True, error_messages={ 'unique': 'The username is taken' }) first_name = models