Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
barseghyanartur opened this issue Jan 26, 2023 · 4 comments
Open

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

barseghyanartur opened this issue Jan 26, 2023 · 4 comments
Assignees
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed

Comments

@barseghyanartur
Copy link
Contributor

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

  • A specific TestCase that does all heavy lifting.
  • Proper documentation on testing (ATM - massively missing).
@baseplate-admin
Copy link
Contributor

baseplate-admin commented Apr 19, 2023

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 :

@vitalik vitalik added documentation Improvements or additions to documentation help wanted Extra attention is needed labels Apr 20, 2023
@Zio-n
Copy link

Zio-n commented Aug 22, 2023

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

@bufke
Copy link

bufke commented Jun 22, 2024

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:

  • NinjaTestCase that makes self.client the NinjaClient
  • Automatic handling of json serialization - I think NinjaClient does this already
  • Maybe a DRF-style .data attribute for response json response? I could go either way on this. It would make converting DRF to ninja easier. It's not that hard to use json().
  • What is needed for authentication? I use self.client.force_login myself.
  • Document the above

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

@c4ffein
Copy link
Contributor

c4ffein commented Aug 8, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants