I am new to django unittest
and pytest
. However, I started to feel that pytest
test case is more compact and clearer.
Here is my test cases:
class OrderEndpointTest(TestCase):
def setUp(self):
user = User.objects.create_superuser(username='admin', password='password', email='[email protected]')
mommy.make(CarData, _quantity=1)
mommy.make(UserProfile, _quantity=1, user=user)
def test_get_order(self):
mommy.make(Shop, _quantity=1)
mommy.make(Staff, _quantity=1, shop=Shop.objects.first())
mommy.make(Order, _quantity=1, car_info={"color": "Black"}, customer={"name": "Lord Elcolie"},
staff=Staff.objects.first(), shop=Shop.objects.first())
factory = APIRequestFactory()
user = User.objects.get(username='admin')
view = OrderViewSet.as_view({'get': 'list'})
request = factory.get('/api/orders/')
force_authenticate(request, user=user)
response = view(request)
assert 200 == response.status_code
assert 1 == len(response.data.get('results'))
And here is the pytest
version
def test_get_order(car_data, admin_user, orders):
factory = APIRequestFactory()
user = User.objects.get(username='admin')
view = OrderViewSet.as_view({'get': 'list'})
request = factory.get('/api/orders/')
force_authenticate(request, user=user)
response = view(request)
assert 200 == response.status_code
assert 1 == len(response.data.get('results'))
The benefit from pytest
is fixture
in another file. It makes my test case compact by let them be my input parameters.
Are they any benefit of using Django unittest
than pytest
?
Update: 1July2017
Update: 5July2017
Update: 1Sep2017
Update: 29Sep2017
Update: 26Dec2017
- Pytest reduces your problem when fixtures got mutated over the test.
I got
testcases
that run individually passed, but fail when run thoroughly. - 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.
- Pytest allow you to use real database with simple decorator. Django test does not. You have to create your own customized command for your job
- Pytest is generic. Being an generic it means you feel comfortable to
work with project outside the Django. For example when you have to
build micro-service such as Flask + 3rd parties like APScheduler,
PyRad, ... etc. I mention this because my backend life uses Django 50%
The rest of the is
Python
and infra - Pytest is not using multiple inheritance to create my fixtures
- Unittest takes advantage on
gitlab-ci
over Pytest when used withDocker
as a runner by smoothly execute without any extra configurations. problem
pytest
. The others useDjango test
I do follow it. Therefore I am start the question on here and discuss. – RefinesetUp()
and reuse by inheritance. Are you talking on the same fixtures? – Refinemommy
– Refinereset_transaction
inpytest
. After certain time I will put it here and it might change my mind! – Refinemommy.make
returns created instance. So this code is cleanershop = mommy.make(Shop, _quantity=1); mommy.make(Staff, _quantity=1, shop=shop)
– Bield