django.tests.TestCase
django.tests.Client
django.tests.RequestFactory
https://github.com/rbarrois/factory_boy
class YourViewTest(assertions.StatusCodeAssertionsMixin,
assertions.MessagesAssertionsMixin,
testcases.ViewTestCase):
view_class = YourView
view_kwargs = {'some_kwarg': 'value'}
middleware_classes = [
SessionMiddleware,
(MessageMiddleware, testcases.MiddlewareType.PROCESS_REQUEST),
]
def test_post_should_redirect_and_add_message_when_next_parameter(self):
user = UserFactory()
request = self.factory.post(data={'next': '/'}, user=user)
response = self.view(request)
self.assert_redirect(response, '/')
self.assert_message_exists(request, messages.SUCCESS, 'Success!')
class TestModel(models.Model):
field = models.CharField(max_length=100)
file = models.FileField(upload_to='files')
class CreateAPIView(generics.CreateAPIView):
model = models.TestModel
class CreateAPIViewTest(assertions.StatusCodeAssertionsMixin,
files.InMemoryStorageMixin,
restframework.APIViewTestCase):
view_class = CreateAPIView
def test_post_should_create_model(self):
data = {
'field': 'test value',
'file': files.create_inmemory_file('test.txt', content=b'Hello multipart!'),
}
request = self.factory.post(data=data, format='multipart')
response = self.view(request)
self.assert_status_equal(response, status.HTTP_201_CREATED)