Integration testing DRF - django

I'm using Django with DRF as just an API--it doesn't serve any FE assets of any kind. I know the Django testing suite is built on the Python native unittest library and plan on using it for unit testing.
When it comes to integration testing, is it sufficient or should something like Behave be used?
If unittest is sufficient, should it be used in conjunction with some kind of faker?

If you want to test the API contract, you need:
to read the relevant documentation
have a way to generate models you need. My advice is to use factory-boy.
This setup will test your urlconfs, views, serializers and to some extent your models, without the need for real server and without the need to mock API responses.

Related

DRF based API external blackbox testing

I'm involved in the Django rest framework based project.
Django and DRF are fantastic - I heavily cover complicated parts of my code with unit tests, using built-in DRF and Django testing tools.
We also have a QA team in my project. The team is using pytest to test the developed API. QAs don't know Django or DRF. They just treat the API as a balckbox and don't rely on any Django tools.
I looked at their code once and saw how much work is being done on their side. Most of the time they test not my code, but rather they test Django functionality.
We have CRUD for complicated models. It's so easy for me to create CRUD with DRF, since I have most of the tools bult-in, while testers create models for various requests and responses and it's really complicated on their side. When it comes to authentication, they have fancy fixtures, while i use 'force_authenticate'... In the end their code looks more complicated than mine.
My question: is it really helpful to test DRF API externally, provided it's already covered with many unittests?

What are the best practices for testing a FastAPI project with JWT authentication?

I have FastAPI project which does the following:
Register users(unauthenticated)
CRUD operations on user resources(authenticated)
What I want to do:
Develop an automated testing framework to unit test all the APIs
Run that on a devops platform like jenkins regularly
Run it locally before deployment
Some specific doubts I have:
For testing, should I use the OpenAPI generated client sdk or the FastAPI client or use the python requests module?
Authentication should happen only once and reuse the JWT token or fresh authentication for each API test ?
Should I use the same pydantic modules for dev and testing or re-write them to ensure that no issues were introduced in case of updated models ?
How to do proper unit testing when one API requires result from multiple other APIs?
Use the built-in TestClient
Use a fixture and let pytest sort it out for you; if it's too slow to reauthenticate each time, change the scope of the fixture to a larger scope (i.e. class, module, session, etc.). You can also use FastAPI's dependency_overrides to let your tests run with static authentication configured (so that you can skip actually authenticating in most of your tests).
You should test what you use in your application. Nothing specific written for your tests, except if necessary to make your application testable in a better way.
The only "proper" thing is that your API has been tested to work as you expect it to work. Do multiple API calls if necessary, but move them into fixtures to get composable sets of dependencies for tests (i.e. the result of the fixture can be cached and re-used for all tests in a test class if necessary). If you have tests that depend on a customer being created, create a fixture that creates a customer and use that fixture in the tests where a created customer is necessary.

Reason to use Postman for Django Rest Framework

I'm used to test Django Rest Framework apps with the test tools available directly in Django and DRF. It's possible to setup a dummy client and expose all the REST methods.
At the same time, I see many posts talking about Postman for API testing. I fail to see where the advantage would be.
Is there any reason for me, a single developer, to use Postman? Or perhaps there is only an advantage for shared projects?

Testing Angular and Django Rest Framework apps

Let's say I have a frontend application written in Angular and a backend application written in Django and Django Rest Framework. I created unit tests for backend application (with pytest) and I'm about to create some functional tests for the frontend application. The thing is that the frontend app needs access to the backend app in order to work correctly. I can write some mocks to handle that, but I am not sure if this is the best way to do that.
My question is, what is the best way to handle that? Should I use a single git repo for both applications or maybe a better way is to use two repositories, but then how to handle the tests for frontend application?
I was also thinking about using selenium with pytest, but then I would have to use a single repository. I am a little bit confused and would really use some good advice. Thanks!
Unit tests as the name suggests is testing separate units of the code in isolation. Meaning that it does not have to depend on any other part, else, you wouldn't know if the test is failing for that particular unit or the units it depends on.
As a result, all tests should mock the request to the backend and return valid responses (or invalid, if you're testing for error handling). The same applies to any other external service that the unit depends on.

Can Django REST Framework serializers be used for other web-frameworks?

I want to use JSON serilization in my aiohttp.web application. But I haven't found any libraries for that task in aiohttp universe.
If there are any problem to use serilizers from django-rest-ramework with aiohttp?
Django REST framework is very closely tied to django itself. You could probably hack a setup where you were able to use a little of DRF with aiohttp but you would obviously lose most of what DRF does: routing, db/model integration, views, viewsets, authentication.
Much better to use one of the many other serialization and deserialization libraries available in python. I would recommend pydantic (admission: I developed pydantic), but there are loads of other such libraries.
The power of DRF comes from it's completeness and first class integration with django. Without that it's not a good choice. It's also very slow.