vitalik / django-ninja

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

Documentation about unit tests #258

Open lgabs opened 2 years ago

lgabs commented 2 years ago

I'm quite new to django, and now I'm working on a django project and I want to use django-ninja. However, I could not find much documentation about how to make unit tests using this framework. Is there any? Maybe it could be good to add some in the official documentation.

vitalik commented 2 years ago

Hi @lgabs well generally testing django-ninja based project is no different than testing any other django project but, yeah.. I guess I'll add some section about some specific testing tools

manoj-punchh commented 2 years ago

@lgabs you can make use of unittest For example:

# Create your tests here.
import unittest
from django.test import Client

class MyUnitTest(unittest.TestCase):
    def setUp(self):
        # Every test needs a client.
        self.client = Client()

    def test_product_health(self):
        # Issue a GET request.
        response = self.client.get('/api/v1/products/ping')
        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)
        # Check that the rendered json contains valid data.
        self.assertEqual(response.json().get('data'), 'Healthy')

$ ./manage.py test APP_NAME

Since it's under Django so you can also leverage Django default unit test.

from django.test import TestCase
class MyUnitTest(TestCase):
    pass
smyja commented 2 years ago

Hi @lgabs well generally testing django-ninja based project is no different than testing any other django project but, yeah.. I guess I'll add some section about some specific testing tools

Would be great.

mshemuni commented 1 year ago

Can you please write an example for testing a web API with authentication? I use APIKeyQuery. But neither appending the apikey value directly to the URL nor adding it params(data) worked. I get 401...

MustafaBadilli commented 1 year ago

Thanks @manoj-punchh, but in my case I use Firebase for auth check, which needs to be mocked obviously :/

karkir0003 commented 10 months ago

@MustafaBadilli were you able to figure out how to mock firebase auth for unit testing a django ninja api endpoint?

karkir0003 commented 10 months ago

Hi @lgabs well generally testing django-ninja based project is no different than testing any other django project but, yeah.. I guess I'll add some section about some specific testing tools

where can I find info on this? I have a custom FirebaseAuth class that inherits HttpBearer and overrides authenticate function. An endpoint called /columns uses auth = FirebaseAuth(). I'd like to mock FirebaseAuth in my unit test. Any code pointers you could point me to?

MustafaBadilli commented 10 months ago

No, unfortunately @karkir0003

karkir0003 commented 10 months ago

No, unfortunately @karkir0003

Thanks for the help. I was fortunately able to find a workaround that worked for my use case through using pytest and monkeypatch