HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects
MIT License
5.12k stars 520 forks source link

Business logic and test factories #147

Closed hgylfason closed 9 months ago

hgylfason commented 9 months ago

Do you have a reliable way to make sure test factories cover important business logic? To explain what I mean, let's look at the create_user service from the README. It doesn't just create a user; it also sets up a profile and sends a confirmation email. But a standard User model test factory only creates the user, without the extra steps.

Would you have any tips or examples from your projects on how to handle this? Any insights would be very helpful.

RadoRado commented 9 months ago

@hgylfason Hello :wave:

You want to make sure you are testing the code that's actually executing the business logic.

This can be done in various ways.

By mentioning factories, I believe you are asking more about "how to setup the data for our tests".

My suggestion would be to either:

  1. Create the data using the service layer
  2. Or make sure the factories are calling that service layer.

Here are some materials that you may find helpful:

  1. DjangoCon 2022 | Quality Assurance in Django - Testing what matters - https://youtu.be/PChaEAIsQls
  2. https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories-advanced-usage

Let me know if you want me to expand with more details & examples.

Cheers!

RadoRado commented 9 months ago

@hgylfason I'll close this for now. Feel free to reopen, if you need additional info or if you have other questions.

Cheers!