ApiTestCase | Handy PHPUnit test case for testing JSON APIs | Web Framework library
kandi X-RAY | ApiTestCase Summary
kandi X-RAY | ApiTestCase Summary
[Scrutinizer Code Quality] ApiTestCase is a PHPUnit TestCase that will make your life as a Symfony API developer much easier. It extends basic [Symfony] WebTestCase with some cool features. Thanks to [PHP-Matcher] you can, according to its readme, "write expected json responses like a gangster". We definitely agree. It also uses [Alice] for easy Doctrine fixtures loading.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Build a path from an array of path segments .
ApiTestCase Key Features
ApiTestCase Examples and Code Snippets
Community Discussions
Trending Discussions on ApiTestCase
QUESTION
When I raise a validation error in my serializer the output is prefixed with a 'b' (which I believe is a byte type) and every way I've tried to remove it has failed.
My serializer class: class AppInputTypeSerializer(serializers.Serializer): #value = serializers.CharField(required=True, max_length = 100, min_length = 1)
...ANSWER
Answered 2022-Mar-16 at 16:57Output is an assert failure because of the prefix below.
No, that is not the reason. The reason is because you are comparing a binary string with a dictionary. But even if it was a simple string, that would fail because a string is not a dictionary, even if these look the same.
response.content
is a binary string, not a dictionary since it should eventually result in a binary stream send as HTTP response.
It is better to JSON decode the object, and then check if it is equivalent with the dictionary. You can do this with response.json()
:
QUESTION
I need to test some API endpoints in my app
I.e. I want to create "Bookmark" using Django Rest Framework using "toggle_bookmark/" url
How can I substitute a value for in my test_boomark.py file?
This is my urls.py file:
...ANSWER
Answered 2022-Mar-12 at 09:23You calculate the reverse of the URL, so:
QUESTION
I am trying to add reverse()
to my automated test api calls instead of hard-coding the urls. I have a router in my urls.py
that is directed to a ModelViewSet
and ModelSerializer
.
Here is my urls.py
ANSWER
Answered 2022-Mar-05 at 05:34Did you try reverse('api:votes-detail', kwargs=entry)
? The api:
is only needed if you registered your router using namespace=
argument.
Regardless, I highly recommend using django-extension's show_urls
. Take a look at this SO thread.
QUESTION
I was creating an endpoint using drf to list users. While testing the code, I realized that it calls 7 queries.
models.py:(I think using django's User model will achieve the same result)
...ANSWER
Answered 2022-Feb-22 at 09:05The 7 queries:
- Get the session. To avoid this query, you could use e.g. the
cached_db
session backend. - Get the logged in user's details from the database. This could also be avoided with a different session backend that gets that data from cache (or maybe the session object itself) instead (but you'd have to be careful with e.g. cache invalidation).
- List the users. Since you're listing the users, that's to be expected.
- Get user 1's assigned groups.
- Get user 1's assigned permissions.
- Get user 2's assigned groups.
- Get user 2's assigned permissions.
Queries from 4 to 7 can be avoided either by:
- Eliding the data from the serializer. (You're currently using
fields = "__all__"
.) If you the serializer doesn't serialize the permissions and groups, they're not fetched either. - Adding a suitable
.prefetch_related()
(since these are M2Ms) clause to the viewset's queryset, so these are done in 2 queries, not 2N queries.
QUESTION
I've been trying to test my PUT method in the following APITestcase:
...ANSWER
Answered 2022-Feb-07 at 17:12Shouldn't you add the request body also in a put
request?
Something like this?
QUESTION
can somebody help me. I can't authorize my test user in unittests
...ANSWER
Answered 2022-Feb-03 at 08:36There is better way to do that. You can use client.force_authenticate
. This is included in DRF Base test class. Bacause that, you can focus on testing
QUESTION
I am able to test get_queryset() in ReadOnlyModelViewSet like this.
...ANSWER
Answered 2022-Jan-20 at 05:38You can add another decorator to your test to apply this like so:
QUESTION
from django.urls import reverse
from rest_framework.test import APITestCase
from rest_framework.authtoken.models import Token
from faker import Faker
fake = Faker()
APICLIENT = APIClient()
from factory_djoy import UserFactory
class TestAccount(APITestCase):
def setUp(self):
self.user = UserFactory()
def test_print_name(self):
print(self.user.is_authenticated)
# do something here
...ANSWER
Answered 2022-Jan-19 at 08:11.is_authenticated
does not mean that the user is authenticated on the server side. All User
objects have is_authenticated = True
, it is used to make a distinction between an User
[Django-doc] object, and the AnonymousUser
[Django-doc].
Indeed, by default if you look for request.user
, it will either return an AnonymousUser
object if there is no user attached to the setting, or a User
object if the session is bound to that user.
For the builtin User
model, .is_autenticated
will thus always return True
[GitHub]:
QUESTION
I am trying to create test class for my custom middleware. The project is using Django REST framework. Middleware class works fine when server is running, but when I run test it behaves not quite as I would expect it to do. Maybe I misunderstood something, as I am quite new to testing in Django.
my_middleware.py:
...ANSWER
Answered 2022-Jan-18 at 05:28You need to access the request object from the response with response.wsgi_request
instead of response.request
.
QUESTION
Here I'm writing some TestCase for some queryset to view in api and getting error not a valid function or pattern name. I didn't get any idea what missing here! Is there any solution for this?
views.py
...ANSWER
Answered 2022-Jan-08 at 11:09You used an app_name = 'student_api'
, this means that you should prefix the name of the view with that app_name
, so:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ApiTestCase
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page