django-unittest

django how to assert url pattern resolves to correct class based view function

送分小仙女□ 提交于 2021-02-08 12:40:30
问题 I have a class based view class HomePage(View): def get(self, request): return HttpResponse('<p>This is content.</p>') and url-pattern defined as below: urlpatterns = patterns('', url(r'^$', HomePage.as_view()), ) To this pattern resolves to current view function, I wrote a test like this: class HomePageTest(TestCase): def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertIsInstance(found.func, HomePage) By running this unittest I am getting following error: self

Django Unit Testing in Visual Studio 2017 or 2019:

别说谁变了你拦得住时间么 提交于 2021-01-27 07:10:46
问题 Django needs setup code so unit tests on models that need a database will work. Standard Django unit test do that silently in the background but not with Visual Studio. I have not found a good way to provide setup code that will work for starting test runs from both the Visual Studio Test Explorer and with django manage.py test . Visual Studio Test Explorer does not call setUpModule/tearDownModule nor tearDownClass. The following code works from within VS, but I find it ugly, AND it doesn't

How to fix “TypeError: argument of type 'ConnectionHandler' is not iterable” when running a django test?

偶尔善良 提交于 2020-03-24 03:28:32
问题 When I do ```python -m unittest`` inside of my users app, I get this error: TypeError: argument of type 'ConnectionHandler' is not iterable I was customizing my User model in django and I wanted to make a test for it. I already did the migrations and the custom User model works good (I did with a new database), so I expected to run the test successfully. Here is my test code: from django.test import TestCase from django.contrib.auth import get_user_model # Create your tests here. class

Django unit test client response has empty context

ぃ、小莉子 提交于 2019-12-30 08:03:17
问题 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'

Django Unit Testing taking a very long time to create test database

放肆的年华 提交于 2019-12-29 11:45:42
问题 For some time now, my unit testing has been taking a longer than expected time. I have tried to debug it a couple of times without much success, as the delays are before my tests even begin to run. This has affected my ability to do anything remotely close to test driven development (maybe my expectations are too high), so I want to see if I can fix this once and for all. When a run a test, there is a 70 to 80sec delay between the start and the actual beginning of the test. For example, if I

Running django tutorial tests fail - No module named polls.tests

北战南征 提交于 2019-12-28 02:38:26
问题 I'm playing with django 1.6 tutorial but i can't run tests. My project (name mydjango) and app structure (name is polls) are as shown below in a virtualenv. (.nja files are just created by ninja-ide the ide I'm using) . ├── __init__.py ├── manage.py ├── mydjango │ ├── __init__.py │ ├── __init__.pyc │ ├── mydjango.nja │ ├── settings.py │ ├── settings.pyc │ ├── templates │ │ └── admin │ │ └── base_site.html │ ├── urls.py │ ├── urls.pyc │ ├── wsgi.py │ └── wsgi.pyc ├── polls │ ├── admin.py │ ├──

Django testing the html of your homepage against the content of a response

时光怂恿深爱的人放手 提交于 2019-12-22 19:01:29
问题 If I have a test like so... def test_home_page_returns_correct_html(self): request = HttpRequest() response = home_page(request) expected_html = render_to_string('home.html', request=request) self.assertEqual(response.content.decode(), expected_html) As soon as I add a form in curly braces, e.g. {{ form }} The above test will fail as the .html file will not render the form only the response will. Thus causing the assertion to not match. Is there a way round this so that I can still test my

Django testing the html of your homepage against the content of a response

末鹿安然 提交于 2019-12-22 19:01:10
问题 If I have a test like so... def test_home_page_returns_correct_html(self): request = HttpRequest() response = home_page(request) expected_html = render_to_string('home.html', request=request) self.assertEqual(response.content.decode(), expected_html) As soon as I add a form in curly braces, e.g. {{ form }} The above test will fail as the .html file will not render the form only the response will. Thus causing the assertion to not match. Is there a way round this so that I can still test my

Django can't destroy and create test databases properly

≯℡__Kan透↙ 提交于 2019-12-21 20:44:58
问题 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'... . -------------------------------------------------------

How to change Tox command with command-line parameters

独自空忆成欢 提交于 2019-12-19 17:44:11
问题 How do you append options to the command Tox runs by appending that option to Tox? Specifically, how do you run a specific Django unittest with Tox? I'm trying to wrap Tox around some Django unittests, and I can run all unittests with tox , which runs django-admin.py test --settings=myapp.tests.settings myapp.tests.Tests . However, I'd like to run a specific test at myapp.tests.Tests.test_somespecificthing , which would mean telling Tox to append ".test_somespecificthing" to the end of the