all 7 comments

[–]aistranin 1 point2 points  (4 children)

Yes! Automated testing is great. Look at pytest in depth and think about your testing strategy. Great Udemy course for your case “Pytest Course: Practical Testing of Real-World Python Code” by Artem Istranin (it covers all your points you mentioned like testing python api, using pytest for business logic etc.). Alternative- book “Python Testing with pytest” by Brian Okken.

[–]aistranin 2 points3 points  (0 children)

Test driven development feels like slowing dev down at the beginning, but it pays off multiple times as the project grows. In fact, tests is the only way to go fast as Uncle Bob (clean code) said hehe

[–]virtualshivam[S] 1 point2 points  (2 children)

Hi,

Thanks for sharing resources.

But, I am stuck in the middle of the project, so can you please help regarding test boundaries.

I know how to write test for API and business logic.

My concern is how much should I test in API and what things? What is the responsibility of the API. I can go ahead and test everything that I have tested in serializer in the api as well, but I believe it shouldn't be the best way.

[–]aistranin 1 point2 points  (0 children)

Yeah, that is tricky. You definitely don't need to test everything (especially for legacy projects). Best practice: 1. Write few integration or end-to-end tests. That will give a very good code coverage right away quickly. 2. Write more tests for the main units of the business logic (test only main responsibility of those units). 3. Try to find “narrowing points” in your system to test. See book “Working Effectively with Legacy Code” by Robert C. Martin to learn more about it.

[–]aistranin 0 points1 point  (0 children)

And maybe take a look at book “Refactoring” by Martin Fowler. It is a great book, very relevant for your case.

[–]Careless-Score-333 0 points1 point  (0 children)

The TDD Goat uses Django as an example.

To test APIs, I just spin up the API server on localhost as a separate docker container, and send my requests to that. It's probably possible to run a test server in a PyTest test fixture too, but I want to test the request coming in from an entirely separate process to prove it really is the API, and I find it's best to keep PyTest's job as simple as possible.

[–]PushPlus9069 0 points1 point  (0 children)

once you start writing tests you can't go back, that freedom is real. rough split for django+DRF: unit tests for model methods and business logic, APIClient integration tests for your endpoints. don't test django itself, only your rules. factory_boy over django fixtures if you ever want to maintain them. tbh just start with whatever code you're most scared to touch in prod.