Last active
September 19, 2018 13:02
-
-
Save mutaimwiti/d937a8e6e3d3a053f95bdf1bd6736528 to your computer and use it in GitHub Desktop.
Example Python test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase | |
from rest_framework import status | |
from rest_framework.test import APIClient | |
class BaseTestCase(TestCase): | |
def setUp(self): | |
self.client = APIClient() | |
def post_login(self, username='johndoe', password='P@$$word'): | |
return self.client.post('/api/auth/login', {'username': username, 'password': password}) | |
class TestLogin(BaseTestCase): | |
def test_successful_login(self): | |
response = self.post_login() | |
self.assertEqual(response.status, status.HTTP_200_OK) | |
def test_missing_username_is_not_allowed(self): | |
response = self.post_login(username='') | |
self.assertEqual(response.status, status.HTTP_422_UNPROCESSABLE_ENTITY) | |
self.assertIn(response.data['errors'], 'An username is required to log in.') | |
def test_missing_password_is_not_allowed(self): | |
response = self.post_login(password='') | |
self.assertEqual(response.status, status.HTTP_422_UNPROCESSABLE_ENTITY) | |
self.assertIn(response.data['errors'], 'A password is required to log in.') | |
def test_username_with_special_chars_is_not_allowed(self): | |
response = self.post_login(username='u$ern@me') | |
self.assertIn(response.data['errors'], 'Special characters are not allowed on the username.') |
This was thorough and well thought.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good work on the test. Done beyond expectations.
You could add some docstrings on each function and class