Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run tests django rest framework tests?

I'm learning django rest framework. I wrote a simple test like this:

from rest_framework import status
from rest_framework.test import APITestCase


class ClinicTestCase(APITestCase):
    def getList(self):
        factory = APIRequestFactory()
        request = factory.get('/Clinic/')
        self.assertEqual(response.status_code, status.OK)

my api returns empty json array for that request. What i don't know is, how do i run this test?

when i use this command:

python manage.py test

i get

Ran 0 tests in 0.000s

as output. It's not written in documentation to how to run the tests.

like image 779
Sefa Avatar asked Apr 14 '15 17:04

Sefa


People also ask

How do I run a test in Django?

Open /catalog/tests/test_models.py.TestCase , as shown: from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

How do I check Django REST framework?

For Django REST Framework to work on top of Django, you need to add rest_framework in INSTALLED_APPS, in settings.py. Bingo..!! Django REST Framework is successfully installed, one case use it in any app of Django.

What is the correct command to run Django REST server?

Setting up Django REST framework Run the command python -m venv django_env from inside your projects folder to create the virtual environment. Then, run source ./django_env/bin/activate to turn it on. Keep in mind that you'll need to reactivate your virtual environment in every new terminal session.


1 Answers

I believe your test methods need to start with test. Change def getList to def testGetList or def test_get_list.

As with other python tests (see https://docs.python.org/2/library/unittest.html#basic-example), if methods do not start with test they will not be run as tests.

like image 146
aensm Avatar answered Sep 28 '22 03:09

aensm