django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

Documentation about unit tests

Open lgabs opened this issue 2 years ago • 9 comments

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.

lgabs avatar Oct 21 '21 21:10 lgabs

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

vitalik avatar Oct 22 '21 06:10 vitalik

@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

manoj-punchh avatar Feb 24 '22 15:02 manoj-punchh

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.

smyja avatar May 17 '22 16:05 smyja

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...

mshemuni avatar Jun 11 '23 03:06 mshemuni

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

MustafaBadilli avatar Sep 11 '23 19:09 MustafaBadilli

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

karkir0003 avatar Nov 23 '23 21:11 karkir0003

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?

karkir0003 avatar Nov 23 '23 21:11 karkir0003

No, unfortunately @karkir0003

MustafaBadilli avatar Nov 24 '23 06:11 MustafaBadilli

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

karkir0003 avatar Nov 25 '23 03:11 karkir0003