pytest-django

How to test redirection in Django using pytest?

落爺英雄遲暮 提交于 2020-01-14 10:14:08
问题 I already know that one can implement a class that inherits from SimpleTestCase , and one can test redirection by: SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix='', fetch_redirect_response=True) However, I am wondering what is the way I can check for redirection using pytest: @pytest.mark.django_db def test_redirection_to_home_when_group_does_not_exist(create_social_user): """Some docstring defining what the test is

How to test views with pytest whose views has LoginRequired and some specific user dependencies

馋奶兔 提交于 2020-01-06 07:14:15
问题 I am testing a view and while testing this I am getting this error self = <django.db.models.fields.AutoField: id>, value = '' def get_prep_value(self, value): from django.db.models.expressions import OuterRef value = super().get_prep_value(value) if value is None or isinstance(value, OuterRef): return value > return int(value) E ValueError: invalid literal for int() with base 10: '' /usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:965: ValueError And I think I am

How to run tests in django using database with data?

左心房为你撑大大i 提交于 2020-01-02 03:48:07
问题 I want to test my views using data from postgres localhost database (with already loaded data). I'm using tox with pytest and pytest-django. My question: How to set up / connect to local database to get all the data model schema and data itself? Or maybe it is better to use factory_boy? Or to load whole data from .sql script (if yes, how)? Example of my test: def test_foo_view(custom_client_login): response = custom_client_login.get('/foo/bar/123/') assert response.status_code == 200 assert

Docker runner pytest does not collect testcases

痞子三分冷 提交于 2019-12-24 12:05:17
问题 I am successfully put ssh-key to docker runner and it can git clone to do dependencies installation. If I use ordinary build-in python manage.py test it works fine. But I am now working with pytest . I can run pytest on ly command line and got results normal. $ pytest ===================================================================================== test session starts ===================================================================================== platform darwin -- Python 3.6.4,

I can not run Django Python pytest under PyCharm

大憨熊 提交于 2019-12-24 08:23:13
问题 $ pytest portal/ =============================================== test session starts =============================================== platform darwin -- Python 3.6.0, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 Django settings: config.settings.local (from ini file) rootdir: /Users/el/Code/siam-sbrand/portal, inifile: pytest.ini plugins: django-3.1.2 collected 87 items portal/apps/commons/tests.py ...... portal/apps/price_list_excel_files/tests.py ssssssssssss portal/apps/price_lists/tests.py s....

Django test tables are not being created

走远了吗. 提交于 2019-12-22 23:21:40
问题 I'm trying to write test cases for my django project but when I run "$ ./manage.py test" command its creating test database but its not creating any tables and I'm getting an error that table does't exists. Any suggestions are welcome. Here is my model which i have created through "./manage.py inspectdb > models.py" class MyCustomModel(models.Model): name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) class Meta: managed = False db_table = 'MY_TABLE' 回答1: Your

Mocking a RelatedManager in Django 2

做~自己de王妃 提交于 2019-12-12 16:30:29
问题 This question is directly related to this question, but that one is now outdated it seems. I am trying to test a view without having to access the database. To do that I need to Mock a RelatedManager on the user. I am using pytest and pytest-mock . models.py # truncated for brevity, taken from django-rest-knox class AuthToken(models.Model): user = models.ForeignKey( User, null=False, blank=False, related_name='auth_token_set', on_delete=models.CASCADE ) views.py class ChangeEmail(APIView):

How to follow Django redirect using django-pytest?

早过忘川 提交于 2019-12-10 21:23:16
问题 In setting up a ArchiveIndexView in Django I am able to successfully display a list of items in a model by navigating to the page myself. When going to write the test in pytest to verify navigating to the page "checklist_GTD/archive/" succeeds, the test fails with the message: > assert response.status_code == 200 E assert 301 == 200 E + where 301 = <HttpResponsePermanentRedirect status_code=301, "text/html; charset=utf-8", url="/checklist_GTD/archive/">.status_code test_archive.py:4:

Django connections object does not see the tables of a second database during testing with pytest-django

不想你离开。 提交于 2019-12-10 12:45:33
问题 Bottom Line: My Django connections object does not see the table relations of a second database during testing with pytest-django. Overview: I have a problem where my Django connections object seems to get the wrong database information. I stumbled upon this issue when I was querying on a table in the 'customers' DB and Django told me the relation does not exist. With the settings.py database section was set up like below: DATABASES = { 'default': { 'NAME': 'user_data', 'ENGINE': 'django.db

django-pytest setup_method database issue

社会主义新天地 提交于 2019-12-07 03:50:31
问题 I have the following set up on Ubuntu 14.04: python 2.7.6 django 1.7 [though I reproduced the same behaviour with django 1.9 too] pytest-django 2.8.0 [also tested with 2.9.1] pytest 2.7.2 [also tested with 2.8.3] And the following test code: import pytest from django.db import connection import settings from pollsapp.models import Question original_db_name = settings.DATABASES["default"]["NAME"] @pytest.mark.django_db class TestExperiment(object): def setup_method(self, method): # it's not