django-testing

With py.test, database is not reset after LiveServerTestCase

假如想象 提交于 2019-12-22 05:01:32
问题 I have a number of Django tests and typically run them using py.test. I recently added a new test case in a new file test_selenium.py . This Test Case has uses the LiveServerTestCase and StaticLiveServerTestCase classes (which is a first for me, usually I am using just TestCase ). Adding this new batch of tests in this new file has caused subsequent tests to start failing in py.test (when before they all passed). It appears that the database is not being "reset" after the LiveServerTestCase

Using coverage, how do I test this line?

强颜欢笑 提交于 2019-12-20 17:39:39
问题 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 = [ '--with-coverage', '--cover-html', '--cover-package=apps.users,apps.activities', ]

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

做~自己de王妃 提交于 2019-12-20 17:27:35
问题 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

Django test RequestFactory vs Client

穿精又带淫゛_ 提交于 2019-12-20 09:56:14
问题 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 filter_backends = (IsOwnerFilterBackend,) def perform_create(self, serializer): serializer.save(owner=self.request.user) What are

how to get request object in django unit testing?

ε祈祈猫儿з 提交于 2019-12-20 08:34:34
问题 I have a function as def getEvents(eid, request): ...... Now I want to write unit test for the above function separately (without calling the view). So how should I call the above in TestCase . Is it possible to create request ? 回答1: See this solution: from django.utils import unittest from django.test.client import RequestFactory class SimpleTest(unittest.TestCase): def setUp(self): # Every test needs access to the request factory. self.factory = RequestFactory() def test_details(self): #

Testing django application with several legacy databases

走远了吗. 提交于 2019-12-19 07:32:16
问题 I have django application with 5 legacy databases. Almost all models are set with the meta attribute managed=False . Since managed=False is set, migrations for each model have been created with the option managed=False . And since, django test runner picks existing migrations for each model to create test tables in test_databases, it simply doesn't create anything. I tried creating test.py settings file with the following workarounds: from web_services.settings.dev import * from django.test

Mocking a Django Queryset in order to test a function that takes a queryset

早过忘川 提交于 2019-12-18 12:05:50
问题 I have a utility function in my Django project, it takes a queryset, gets some data from it and returns a result. I'd like to write some tests for this function. Is there anyway to 'mock' a QuerySet? I'd like to create an object that doesn't touch the database, and i can provide it with a list of values to use (i.e. some fake rows) and then it'll act just like a queryset, and will allow someone to do field lookups on it/filter/get/all etc. Does anything like this exist already? 回答1: Not that

Difference between TestCase and TransactionTestCase classes in django test

▼魔方 西西 提交于 2019-12-14 00:20:41
问题 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

Authentication failed in Django test

懵懂的女人 提交于 2019-12-13 06:38:34
问题 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): self.browser.quit() def test_login_page(self): self.browser.get('http://localhost:8000') self.assertIn('Hiren->Login', self

Why does Django Redirect Test Fail?

匆匆过客 提交于 2019-12-12 22:08:55
问题 I have a view unit test that is failing and I can't figure out the reason why. I believe it has something to do with the test database. The view in question is the default Django login view, django.contrib.auth.views.login. In my project, after the user logs in, they are redirected to a page that show which members are logged in. I've only stubbed out that page. Here is the unit test: from django.test import TestCase from django.contrib.auth.models import User from django.test.client import