The page of the published news on my blog, is available to any user. I use pytest to check if the page is accessible to an anonymous user. Url is formed by using the id of the news, which I pass in the address parameters (as a tuple).
In the result I got this Type error. test_pages_availability[news:detail-news] - TypeError: expected string or bytes-like object
I had tried to format args to str, puted a coma after arg for tuple format, it didn't helped
code of test
@pytest.mark.django_db
@pytest.mark.parametrize(
'name, args',
(
('news:detail', pytest.lazy_fixture('news')),
('news:home', None),
('users:login', None),
('users:logout', None),
('users:signup', None),
)
)
def test_pages_availability(client, name, args):
if args is not None:
url = reverse(name, args=(news.id,))
else:
url = reverse(name)
response = client.get(url)
assert response.status_code == HTTPStatus.OK
` fixture
@pytest.fixture
def news():
news = News.objects.create(
title='Новость',
text='Невероятное событие',
date=datetime.today,
)
return news
class NewsDetail(generic.DetailView):
model = News
template_name = 'news/detail.html'
def get_object(self, queryset=None):
obj = get_object_or_404(
self.model.objects.prefetch_related('comment_set__author'),
pk=self.kwargs['pk']
)
return obj
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.user.is_authenticated:
context['form'] = CommentForm()
return context
Traceback:
request = <FixtureRequest for <Function test_pages_availability[news:detail-news]>>
def fill(request):
item = request._pyfuncitem
fixturenames = getattr(item, "fixturenames", None)
if fixturenames is None:
fixturenames = request.fixturenames
if hasattr(item, 'callspec'):
for param, val in sorted_by_dependency(item.callspec.params, fixturenames):
if val is not None and is_lazy_fixture(val):
item.callspec.params[param] = request.getfixturevalue(val.name)
value = <built-in method today of type object at 0x00007FFE9CA83650>
def parse_date(value):
"""Parse a string and return a datetime.date.
Raise ValueError if the input is well formatted but not a valid date.
Return None if the input isn't well formatted.
"""
match = date_re.match(value)
E TypeError: expected string or bytes-like object
..\venv\lib\site-packages\django\utils\dateparse.py:75: TypeError
Your data=
parameter is not a date, it is a function to convert it into a date
. You need to call the method, so:
@pytest.fixture
def news():
return News.objects.create(
title='Новость',
text='Невероятное событие',
date=datetime.today(),
)
For a default=…
[Django-doc] you can indeed pass a callable that Django will then call, but not as a value, the value is then the result of the function call, not the function itself.