9
i’ve used Django test for my entire life and now i am using Py.test. I agree that pytest is much cleaner than django itself.
The benefit from pytest is fixture in another file. It makes my test case compact by let them be my input parameters.
In Django unittest you can still use fixtures in other file by using the attribute fixtures = ['appname/fixtures/my_fixture.json']
Pytest will show you the assertion output if the error occur. Django unittest does not. I have to put the breakpoint on my own and investigate the error.
Did you tried to change the --verbose
param on python manage.py tests
?
A few tips:
-
There is a package called
pytest-django
that will help you integrate and using django with pytest. -
I think that if you use classes will you not need to use the
factory = APIRequestFactory()
, the test methods itself they have a parameter calledclient
that is a interface to the pythonrequests
module to access your views.import pytest from model_mommy import mommy @pytest.fixture() def user(db): return mommy.make(User) class SiteAPIViewTestSuite: def test_create_view(self, client, user): assert Site.objects.count() == 0 post_data = { 'name': 'Stackoverflow' 'url': 'http://stackoverflow.com', 'user_id': user.id, } response = client.post( reverse('sites:create'), json.dumps(post_data), content_type='application/json', ) data = response.json() assert response.status_code == 201 assert Site.objects.count() == 1 assert data == { 'count': 1, 'next': None, 'previous': None 'results': [{ 'pk': 1, 'name': 'Stackoverflow', 'url': 'http://stackoverflow.com', 'user_id': user.id }] }