mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.19k stars 22.48k forks source link

Issue with "Django Tutorial Part 10: Testing a Django web appl…": What should you test #14506

Open mschoettle opened 2 years ago

mschoettle commented 2 years ago

MDN URL: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing

What information was incorrect, unhelpful, or incomplete?

The section "What should you test?" advocates to test for the model's verbose name, max length etc.

You should test all aspects of your own code, but not any libraries or functionality provided as part of Python or Django.

So for example, consider the Author model defined below. You don't need to explicitly test that first_name and last_name have been stored properly as CharField in the database because that is something defined by Django (though of course in practice you will inevitably test this functionality during development). Nor do you need to test that the date_of_birth has been validated to be a date field, because that is again something implemented in Django.

However you should check the text used for the labels (First name, Last name, Date of birth, Died), and the size of the field allocated for the text (100 chars), because these are part of your design and something that could be broken/changed in future.

This is the only resource where I could find such a suggestion. Most resources recommend to test any custom logic/functions (like __str__, get_absolute_url etc.) which is also recommended in this tutorial after above quoted text.

To me, other things like blank=True or null=True are also design decisions but they are not tested in this example. In addition, for example, calling CharField(max_length=100) and then testing that the field's max_length is indeed 100 is testing Django functionality.

Specific section or headline?

Section "What should you test?"

What did you expect to see?

Honestly, I am not entirely sure but want to raise this (in my opinion) inconsistency and hopefully start a discussion.

The book "Two Scoops of Django" recommends the following for what should be tested in models (Section 24.3.6, page 302): "Models: Creating/updating/deleting of models, model methods, model manager methods."

Did you test this? If so, how?

n/a

MDN Content page report details * Folder: `en-us/learn/server-side/django/testing` * MDN URL: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/learn/server-side/django/testing/index.md * Last commit: https://github.com/mdn/content/commit/c4b6112d856dbb5fa7b929e5034499c605a51e08 * Document last modified: 2022-03-28T14:52:22.000Z
hamishwillee commented 2 years ago

@mschoettle Thanks for raising this, but I don't see any inconsistency.

You could spend all day testing Django, but what you're interested in is that your new code works, and that any changes you made to old code didn't have unexpected side effects.

The statement suggests that you should test your own code but that you shouldn't need to explicitly test Django's.

If the example does not test some parts of the design that it should I would be very happy to take a PR that notes the omission and suggests perhaps the reader fix as a test of their knowledge.

hamishwillee commented 2 years ago

The book "Two Scoops of Django" recommends the following for what should be tested in models (Section 24.3.6, page 302): "Models: Creating/updating/deleting of models, model methods, model manager methods."

P.S. ^^^ Without reading that book I can't judge their logic. As above though, it makes sense if they are recommending you test your design "did I implement the model properly so it can be created", and little sense sense if they are suggesting you validate that Django can create/update models.