vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
6.9k stars 418 forks source link

[Feature request] Add `django-ninja` specific `TestCase` #664

Open barseghyanartur opened 1 year ago

barseghyanartur commented 1 year ago

Is your feature request related to a problem? Please describe. A django-ninja specific TestCase is needed. There are simply too many things to consider, when testing. Authentication is being one of them. CSRF tokens - another. It does not work as is (standard Django TestCase fails with 4xx errors ).

Describe the solution you'd like

baseplate-admin commented 1 year ago

I think ninja has testing.

Refer to ninja.testing

Proper documentation on testing (ATM - massively missing).

Yep this is really problematic..Would you like to do a PR ?


Usually this is how i am currently handling testing

from django.test import TestCase
from ninja.testing import TestClient
from apps.characters.models import CharacterModel
from apps.api.urls import api

class APITestCase(TestCase):
    def setUp(self):
        CharacterModel.objects.create(
            mal_id=1, kitsu_id=1, anilist_id=1, name="Spike", name_kanji="", about="Hello"
        )

    def test_character_api_response_given_mal_id(self):
        res = self.client.get("/api/v1/characters", {"mal_id": 1})
        actual_data = res.json()
        actual_items = actual_data["items"][0]
        assert actual_items["id"] == 1
        assert actual_items["kitsu_id"] == 1
        assert actual_items["anilist_id"] == 1

references :

Zio-n commented 1 year ago

Any update on official documentation for testing with Django-ninja?

bufke commented 2 months ago

What features do folks want? Maybe I could work on this. Right now I write my tests very simplistically. I use django TestCase and post with content_type="application/json"

I would guess features are:

My opinion would be to make something very small and nice and well documented. Avoid too many features.

c4ffein commented 4 weeks ago

@bufke I came up with something custom for 2 items of this list when implementing the RealWorld Demo App for Django Ninja ( the helper is here and you can check one of the test files here)

So I reimplemented those slightly differently as 2 PR, as we don't have exactly the same use case if it ends-up here:

Not sure if this is the right approach, but that's what I ended-up implementing myself in my repo. I'm really not sure we want to have those in Ninja though, I enjoy the framework because it's KISS, as that makes it easier to both learn and adapt to your needs... Anyway, I think splitting this issue into multiple PR to see what gets traction or not may be the right way to do