django-unittest

Overriding decorator during unit test in python

你。 提交于 2019-12-18 16:53:28
问题 I have a django class based view that I'm decorating. Unfortunately that decorator makes outside calls to do status checks which is outside the scope of what the unit test should do so I want to override the decorator to do nothing during my unit tests. Here is my decorator: decorators.py def status_check(func): @wraps(func) def wrapped(request, *args, **kwargs): uri = settings.SERVER_URI status_code = None bad_status = [404, 500] try: response = requests.head(uri) except requests

Django Unitest Checking Value Of Template Variable

痴心易碎 提交于 2019-12-11 07:49:19
问题 ) Suppose I have {{registered}} variable in template. I wrote a piece of test: def nice_test(): response = self.client.post(reverse('app:register;), {'username': 'dupa'} and there I want to check value of variable registered in response. How to do it ? 回答1: The response from the test client has access to the template context used. def nice_test(): response = self.client.post(reverse('app:register'), {'username': 'dupa'}) self.assertEqual(response.context['registered'], '<expected value>')

Setting pycharm run django unittest

不问归期 提交于 2019-12-10 23:31:20
问题 I can not run Django Python pytest under PyCharm After I got the answer from the above URL. I am now trying to fully utilize my IDE functions as much as I can. Here is another project in my company that using Django UnitTest . This is the command line I used. $ python manage.py test --settings=eneos.config.settings.local Actually --settings=eneos.config.settings.local is not required, because I put that config in the wsgi.py already. But explicit is better than implicit when I ask the

django - HttpRequest object has no attribute 'session'

爷,独闯天下 提交于 2019-12-10 16:35:42
问题 I can't seem to get sessions working. Django complains that HttpRequest objects have no attribute called 'session'. In the documentation it clearly states that if you have the middleware enabled, and the django.contrib.sessions in your installed apps, then you're good to go. I am getting this error using unit tests. In my views.py: def home_page(request): response = render(request, 'home.html', {'message_text' : request.session.get('message_text', ''), 'ip_address' : request.session.get('ip

How to add authentication token in header of `APIClient` in `django rest_framework test`

岁酱吖の 提交于 2019-12-08 21:36:23
问题 I am using oauth2_provider for my rest_framework . I am trying to write test case for my api. I have obtained an access token. But I am not able to authenticate user using access token in APIClient I am looking to get this curl command work with APIClient . curl -H "Authorization: Bearer <your_access_token>" http://localhost:8000/api/v1/users/current/ I have tried client.get('/api/v1/users/current/', headers={'Authorization': 'Bearer {}'.format(self.access_token)}) and client.credentials(HTTP

Django 1.6, Transaction.commit_on_success with Multiprocessing is not working

人走茶凉 提交于 2019-12-08 08:14:28
问题 I am new in Django and trying to write some test cases. In my code I am doing some transaction. For that purpose I locked my that code using select_for_update in django. Now I want to test it whether lock is working correctly or not. I am running 2 or more processes simultaneously so that it allows only first process and wait here for finish first process and then other process proceeds. #here XYZ and ABC are models. @transaction.commit_on_success def transaction_func(): exp1 = ABC(a = 5)

Django can't destroy and create test databases properly

萝らか妹 提交于 2019-12-04 13:14:14
When I try to run my unittest, this is what I get: python manage.py test dbank --settings=databank_web.settings.dqs.dev_hooman Creating test database for alias 'default'... Creating test database for alias 'global'... Creating test database for alias 'optin_db'... Creating test database for alias 'vpd3'... Creating test database for alias 'user_db'... Creating test database for alias 'vpd1'... Creating test database for alias 'vpd2'... . ---------------------------------------------------------------------- Ran 1 test in 0.327s OK Destroying test database for alias 'default'... Warning: Table

What is the clean way to unittest FileField in django?

女生的网名这么多〃 提交于 2019-12-03 01:35:51
问题 I have a model with a FileField. I want to unittest it. django test framework has great ways to manage database and emails. Is there something similar for FileFields? How can I make sure that the unittests are not going to pollute the real application? Thanks in advance PS: My question is almost a duplicate of Django test FileField using test fixtures but it doesn't have an accepted answer. Just want to re-ask if something new on this topic. 回答1: There are several ways you could tackle this

Django unit test client response has empty context

强颜欢笑 提交于 2019-12-01 03:15:34
I have a unit test that's failing in an assertion that passes in another test in the same test case class. Here's the passing test: def test_home(self): c = Client() resp = c.get('/') self.assertEqual(resp.status_code, 200) self.assertTrue('a_formset' in resp.context) Here's the failing test: def test_number_initial_number_of_forms(self): c = Client() resp = c.get('/') self.assertEqual(resp.context['a_formset'].total_form_count(), 1) In the second test, I get the error TypeError: 'NoneType' object has no attribute '__getitem__' . If I execute the second test as def test_number_initial_number

Overriding decorator during unit test in python

拥有回忆 提交于 2019-11-30 14:00:40
I have a django class based view that I'm decorating. Unfortunately that decorator makes outside calls to do status checks which is outside the scope of what the unit test should do so I want to override the decorator to do nothing during my unit tests. Here is my decorator: decorators.py def status_check(func): @wraps(func) def wrapped(request, *args, **kwargs): uri = settings.SERVER_URI status_code = None bad_status = [404, 500] try: response = requests.head(uri) except requests.ConnectionError as err: LOGGER.error('Server is hosed! Oh Noes! Error: %s ' % (err)) raise Http404 except