from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
def setUp(self):
Animal.objects.create(name="lion", sound="roar")
Animal.objects.create(name="cat", sound="meow")
def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
lion = Animal.objects.get(name="lion")
cat = Animal.objects.get(name="cat")
self.assertEqual(lion.speak(), 'The lion says "roar"')
self.assertEqual(cat.speak(), 'The cat says "meow"')
class SimpleTest(TestCase):
def test_details(self):
response = self.client.get('/customer/details/')
self.assertEqual(response.status_code, 200)
from django.test import TestCase class SimpleTest(TestCase): def test_details(self):
self.assertFalse(User.objects.filter(name = 'John').exists())
response = self.client.post('/users', {"name": "John"}) self.assertEqual(response.status_code, 200)
self.assertTrue(User.objects.filter(name = 'John').exists())
from django.test import TestCase class SimpleTest(TestCase): def test_details(self):
user_list = self.client.get('/users?name=John'}).content
# assert empty list
response = self.client.post('/users', {"name": "John"})
user_list = self.client.get('/users?name=John').content
# assert NON empty list
Using unittest.TestCase avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them. This can lead to unit tests that pass when run in isolation but fail when run in a suite.
@patch('models.User.objects.create')
def user_should_be_created_with_email():
User.register(name = "John", email="j@me.com")
self.assertEquals(
User.objects.create.calls[2]['email'],
("j@me.com",)
)
./manage.py test
./manage.py test --pattern="tests_unit_*.py"
from django.test import TestCase class SimpleTest(TestCase): def test_details(self):
response = self.client.get('/customer/details/')
self.assertEqual(response.status_code, 200)# extract details
response = self.client.post('/customer/connect', data=details) self.assertEqual(response.status_code, 200)
# validate response
class SimpleTest(MyOwnSuperHeroTestCase):
def test_details(self):
details = self.client.get_customer_details()
result = self.client.connect_customer(details)
# Verify output
children = Person.objects.filter(age__lte=18).all() for child in children: child.age = 18 child.save() self.assertEquals(len(Persons.adults.all()), 1)
children = Person.children.all() for child in children: child.age = 18 child.save() self.assertEquals(len(Persons.adults.all()), 1)
class TwitterConnectorTest(unittest.TestCase):
def setUp():
self.twitter_connector = ...
def should_load_ten_tweets():
tweets = self.twitter_connector.load_tweets()
self.assertTrue(len(tweets) == 10)
class TwitterConnectorTest(unittest.TestCase):
def setup():
self.twitter_connector = ...
def should_load_ten_tweets():
tweets = self.twitter_connector.load_tweets()
assert len(tweets) == 10
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3'
}
}
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}