Joseph Victor Zammit
A minimal Django testing
styleguide
Django Day Copenhagen 2023
@jvzammit
What is it
that
troubles
us?
"Every task your organization
has to do repeatedly should be
scalable (linear or better) in terms
of human input.
Policies are a wonderful tool for
making process scalable."
[Software Engineering at Google]
Styleguide gains and tradeo
ff
s
What should our style guide be about?
1. What to test
2. How to write them tests
1. What to test
The Beyoncé rule
If you like it put a test on it.
Martin Fowler’s Test Pyramid
martinfowler.com
Source:
[Software Engineering at Google]
Testing Trophy by Kent C. Dodds
@kentcdodds
Source:
[Software Engineering at Google]
2. How to write tests
Thinking about the test (1)
“Errors are much more
common in the
neighbourhood of logical
conditions than they are in the
locus of sequential
processing statements.”
[Pressman]
Thinking about the test (2)
Thinking about the test (3)
Thinking about the test (4)
Equivalence partitioning is a black-box
testing method that divides the input
domain of a program into classes of data
from which test cases can be derived.
[Pressman]
DO’s and DONT’s
DO: agree on test folder structure
DO: agree on test structure
could be better
"When orders created today are retrieved"
hmmm
An alternative to
Given/When/Then
is
Arrange/Act/Assert
DO: test case and tests
naming conventions
and
DO: Use verbose test
function names
DO: a test should test only one behaviour
DON’T: put logic
in tests, and:
DON’T: try to DRY
Source:
grugbrain.dev
DO: follow
the LoB
principle
DON’T use
fi
xtures, instead DO use factories
DON’T use numbered variables
DON’T have tests relying on system clock
DO: Use clear failure messages
DO: Test behaviour not internal logic/state
}
DO use mock but DON’T over-mock
Agree on best practices when using unittest.mock
• Mock carefully
• Do not use mock to, for example:
• create request objects -> django.test.RequestFactory, DRF’s APIRequestFactory
• Otherwise "autospec"
• DO: Use a convention for where to apply mocks
• e.g. prefer class/function decorators to avoid with statement context manager
• DO: Use a convention for naming mocks
• e.g. mock_make_request vs p_make_request vs make_request
• Decide on packages like requests_mock, and apply uniformly throughout codebase
DO: play the game of test coverage
“Every work day we
increase our test coverage
is a victory, and every day
the coverage goes down
is a loss."
[Two Scoops of Django]
One rule: no commit can lower test coverage
• What is coverage?
• What should we cover?
• How to enforce in teams?
• non-negative di
ff
-coverage
Takeaways
- style guide
- what tests & how to write them
write tests you want to read
Code is read far more than it is written, so make sure you
Tak!
Thank you audience!
Thank you organisers!
Joseph Victor Zammit
@jvzammit

A minimal Django testing styleguide

  • 1.
    Joseph Victor Zammit Aminimal Django testing styleguide Django Day Copenhagen 2023 @jvzammit
  • 2.
  • 3.
    "Every task yourorganization has to do repeatedly should be scalable (linear or better) in terms of human input. Policies are a wonderful tool for making process scalable." [Software Engineering at Google]
  • 4.
  • 5.
    What should ourstyle guide be about? 1. What to test 2. How to write them tests
  • 6.
  • 7.
    The Beyoncé rule Ifyou like it put a test on it.
  • 8.
    Martin Fowler’s TestPyramid martinfowler.com Source: [Software Engineering at Google]
  • 9.
    Testing Trophy byKent C. Dodds @kentcdodds Source: [Software Engineering at Google]
  • 11.
    2. How towrite tests
  • 12.
    Thinking about thetest (1) “Errors are much more common in the neighbourhood of logical conditions than they are in the locus of sequential processing statements.” [Pressman]
  • 13.
  • 14.
  • 15.
    Thinking about thetest (4) Equivalence partitioning is a black-box testing method that divides the input domain of a program into classes of data from which test cases can be derived. [Pressman]
  • 16.
  • 17.
    DO: agree ontest folder structure
  • 18.
    DO: agree ontest structure could be better "When orders created today are retrieved" hmmm An alternative to Given/When/Then is Arrange/Act/Assert
  • 19.
    DO: test caseand tests naming conventions and DO: Use verbose test function names
  • 20.
    DO: a testshould test only one behaviour
  • 21.
    DON’T: put logic intests, and: DON’T: try to DRY Source: grugbrain.dev
  • 22.
  • 23.
  • 24.
    DON’T use numberedvariables DON’T have tests relying on system clock
  • 25.
    DO: Use clearfailure messages
  • 27.
    DO: Test behaviournot internal logic/state }
  • 28.
    DO use mockbut DON’T over-mock Agree on best practices when using unittest.mock • Mock carefully • Do not use mock to, for example: • create request objects -> django.test.RequestFactory, DRF’s APIRequestFactory • Otherwise "autospec" • DO: Use a convention for where to apply mocks • e.g. prefer class/function decorators to avoid with statement context manager • DO: Use a convention for naming mocks • e.g. mock_make_request vs p_make_request vs make_request • Decide on packages like requests_mock, and apply uniformly throughout codebase
  • 29.
    DO: play thegame of test coverage “Every work day we increase our test coverage is a victory, and every day the coverage goes down is a loss." [Two Scoops of Django] One rule: no commit can lower test coverage • What is coverage? • What should we cover? • How to enforce in teams? • non-negative di ff -coverage
  • 31.
    Takeaways - style guide -what tests & how to write them
  • 32.
    write tests youwant to read Code is read far more than it is written, so make sure you
  • 33.
    Tak! Thank you audience! Thankyou organisers! Joseph Victor Zammit @jvzammit