Questions tagged [django-unittest]

django-unittest tag refers to writing unit tests in Django, tests that are expressed as methods on a Python class that subclasses unittest.TestCase or Django's customized TestCase.

django-unittest tag refers to writing unit tests in Django, tests that are expressed as methods on a Python class that subclasses unittest.TestCase or Django’s customized TestCase.

329 questions
0
votes
1 answer

Check the instance of a Mocked method called once

My function looks for a Actor object in the database and calls its do_something() method with a passed argument. from my_app.models import Actor def my_function(id, stuff): actor = Actor.objects.get(id=id) return…
0
votes
1 answer

How can I test function with request.is_ajax() and request.method=='POST' in Django using unittest?

I have next function def edit_resolution_text_view(self, request): if request.method == 'POST' and request.is_ajax(): resolution = request.POST.get('resolution') donation_id = request.POST.get('donation') …
user10898133
0
votes
1 answer

Mock patch results in error -- TypeError: object() takes no parameters in python 3

This is a bit complicated because I'm debugging some code written a long time ago in python 2.7 In progress of migrating to Python 3 (I know, I know) and facing this problem when trying to fix unit tests The problem is I'm getting an error…
JChao
  • 2,178
  • 5
  • 35
  • 65
0
votes
0 answers

In django unittests how do you specify a database that you are modifying?

In django, while testing, how do you resolve the error 'Database connections to {blah} are not allowed in this test. Add {blah class} to ensure proper test isolation and silence this failure'?
0
votes
0 answers

Django self.client.get() fails while HttpResponse() succeeds

This is in tests.py class QuizDetailView(TestCase): def test_quiz_detail_has_html(self): x = 1 # initial quiz path = reverse('quiz:detail', args=(x,)) print('\n** quiz:detail = ', path) print('** HttpResponse = ',…
0
votes
2 answers

AttributeError: 'TestSuite' object has no attribute 'client'

I'm writing Django unit test for Login form. Below is my sample code. from unittest import TestCase from django.contrib.auth.models import User class TestSuite(TestCase): def setUp(self): self.credentials = { 'username':…
Amlanjyoti
  • 85
  • 2
  • 8
0
votes
1 answer

Django test pagination EmptyPage

I am trying to write a function that tests if we exceed the page limit it returns the last page, but I'm a little stuck Here is my function: def test_pagination_returns_last_page_if_page_out_of_range(self): response =…
Yacine Rouizi
  • 1,360
  • 2
  • 15
  • 31
0
votes
1 answer

How to mock a method inside another module during unit testing

In one of my test case the flow requires that a customer provision process wherein the call goes to the api.py file where in the response is saved from the function create_t_customer like following: In api.py file it is using the method…
0
votes
2 answers

Django - Testing Carrying Over POST Variables

I am working on doing some code coverage unit testing for my Django application when I ran into this interesting predicament. tests/test_views.py class TestLogin(TestCase): def setUp(self): self.client = Client() self.url =…
Jeremy Trpka
  • 322
  • 1
  • 4
  • 20
0
votes
1 answer

Load/dump fixture from/to json string in django unittest

I have a custom model in django with overriden to_python() and get_db_prep_save() methods. I discovered a bug: when dumping and reloading data it was inconsistent. The bug is fixed but I want to unittest it with simple json string. My question is:…
makozaki
  • 3,772
  • 4
  • 23
  • 47
0
votes
1 answer

Django UnitTest check for Permission with self.user.has_perm in test_views.py for View(LoginRequiredMixin, PermissionRequiredMixin, ListView)

I Basically followed the Mozilla How-to: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing#Views_that_are_restricted_to_logged_in_users To get a Complete Picture here you can see the corresponding urls and view in views.py…
0
votes
0 answers

Unable to Mock REST API function with Python Mock in Django

I have an API which use ElasticSearch to get data. I have to write test cases for this. These tests will be run using Circle CI. When we run test cases on test cases on Circle CI, test failed as ElasticSearch is not installed on Circle CI. Now we…
Hassan Ejaz
  • 183
  • 2
  • 16
0
votes
1 answer

How to prevent coverage.py in Django from resetting coverage between runs?

Searched the docs, but couldnt find a way to do this. I've been running my test suite with the following command: coverage manage.py run test tests This will run all tests in the 'tests' folder. Following this, to measure coverage I use the report…
DannyMoshe
  • 6,023
  • 4
  • 31
  • 53
0
votes
1 answer

Django Test what form a formset uses

If I have a form like: class MyForm(modelForm): # Form stuff And a formset like: MyFormSet = modelformset_factory( MyModel, form=MyForm, max_num=6, validate_max=True, ) Is there a way to test that form=MyForm? def…
Carl Brubaker
  • 1,602
  • 11
  • 26
0
votes
1 answer

How to test a view using the request object

I need to make tests to see if my view is working properly. However, I did not find how to use the request object in the test file. My view function is: def track_view(request): user_email, user_order = request.session['email'],…
Matheus Sant'ana
  • 563
  • 2
  • 6
  • 23