Open lgabs opened 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
@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
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.
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...
Thanks @manoj-punchh, but in my case I use Firebase for auth check, which needs to be mocked obviously :/
@MustafaBadilli were you able to figure out how to mock firebase auth for unit testing a django ninja api endpoint?
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?
No, unfortunately @karkir0003
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
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.