django-rest-framework-json-api
django-rest-framework-json-api copied to clipboard
Convert all tests to pytest
Currently some tests are written in unit test and some in pytest.
As pytest is more flexible all tests should be converted to use pytest to be consistent.
It also needs to be considered whether pytest_runner
in setup.py is really a good thing or whether just simply running pytest
would not be more "pytesty".
Is this consistent with Django's testing guidelines? It appears to prefer unittest. This is, after all, a Django-centric project.
what we use internally for testing doesn't affect the user so that's up to DJA to decide.
We simply have to make sure that a DJA user can use unittest to write tests. DJA already supports this by allowing DRF testing configuration.
Why pytest over unittest: unittest is a Java construct copied into the python world where as pytest has been written with Python in mind and makes writing test so much simpler (especially the fixture part)
Also see pytest-django for more details.
I guess my point is that as a "Django developer" I would prefer to use the Django core mechanism for testing unless there's a strong reason to do otherwise. I have found the tests in DJA to be quite confusing given the mixture of styles. But I don't have a lot of experience with python testing frameworks....
Whenever I review PRs or write PRs myself the most apparent issue of DJA is that the testing code is very messy and confusing for contributors how to write a test and where.
The issues I see at this point:
- mixed styles of tests (Pytest, Unittest)
- mixture between example app and test itself
- unclear naming
- test data shared across several modules (once test data needs to be adjusted totally unrelated tests fail)
- relatively low test coverage
I might have some time at hand to work on this. First I want though that we agree on how to do it before I start.
Following points I see as a way forward:
- create tests module where test will reside (like in DRF)
- use pytest styled tests
- each module defines its own test data
- use snapshot testing where applicable
- strip of the example app of all test related items
- aiming at 100% test coverage or otherwise mark code with no cover if testing is not applicable.
This can be done gradually. So test module by test module can be moved to tests and converted to pytest. Once all is done the example app can be cleaned up
@n2ygk This issue has been open for a while and the main issue here I guess was whether we can agree on converting tests to pytest . My point is that pytest has been around quite a while in the Python community and has been well established. In my experience it makes writing tests much simpler. And if we take the effort to clean up the tests we should also take the opportunity to convert them to pytest style.
What is your take on this? Also comment on what you think is important when cleaning up the tests which I might not have mentioned yet.
@sliverc I agree with all your points 100%.
django rest framework suggest pytest like asserts
anything required? or we are ok to close it
there is actually a long way till this issue is closed. Goal is that all tests in example/tests
will be converted to pytest style and moved to tests
folder. Do not have much time at hand but will continue to work on this step by step. Assigned it to me so this is clear.
if you want helping hand i can do some. i did something like this to DRF
there is actually a long way till this issue is closed. Goal is that all tests in
example/tests
will be converted to pytest style and moved totests
folder. Do not have much time at hand but will continue to work on this step by step. Assigned it to me so this is clear.
you can use me as helping hand.
@auvipy Thanks for the offer. Currently it is not 100% clear how the conversions of the tests will happen. So usually while I am converting some tests I figure out on the way best practices. So for now I think it makes most sense I keep working on it.
I think at a later stage help to work on a cleaning-up example app would certainly be appreciated. But this cannot happen as long as the tests are still depending on the example app.
ok.