Django’s style guide says “for testing boolean values” to prefer assertIs
(matching the examples in the tutorial), but in practice the situation is more nuanced. Per this PR discussion I’m proposing a clarification intended to save some review cycles.
As I understand it, the rationale for preferring assertIs
is:
- it rigorously only accepts the boolean
True
, not other truthy expressions.
This may be another:
- Discouraging
assertTrue
as a rule of thumb encourages more appropriate assertions, e.gassertEqual(len(qs), 1)
versusassertTrue(len(qs) == 1)
, which may be tempting for those with pytest backgrounds.0 != 1
is more informative output thanFalse is not true
.
But I’m suggesting the following example is unnecessarily precise because the boolean is not produced by Django:
self.assertIs(os.path.exists(app_path), True)
Others have suggested this, too. (I’ll mention we even have this case written the other way with assertTrue in our suite, but to be clear, I’m not suggesting any sort of audit of existing tests and would suggest closing a PR doing so.)
If we clarify this point in the style guide, then some contributors could save a trip there entirely, and those we do refer there would get a more nuanced rationale.
Something like:
diff --git a/docs/internals/contributing/writing-code/coding-style.txt b/docs/internals/contributing/writing-code/coding-style.txt
index 20605aef56..b0c075c436 100644
--- a/docs/internals/contributing/writing-code/coding-style.txt
+++ b/docs/internals/contributing/writing-code/coding-style.txt
@@ -110,9 +110,11 @@ Python style
expression matching.
Use :meth:`assertIs(…, True/False)<unittest.TestCase.assertIs>` for testing
- boolean values, rather than :meth:`~unittest.TestCase.assertTrue` and
- :meth:`~unittest.TestCase.assertFalse`, so you can check the actual boolean
- value, not the truthiness of the expression.
+ boolean values produced by Django, rather than :meth:`~unittest.TestCase.assertTrue`
+ and :meth:`~unittest.TestCase.assertFalse`, so you can check the actual
+ boolean value, not the truthiness of the expression. (For boolean values
+ produced elsewhere, e.g. the Python standard library, either assertion is
+ acceptable.)
* In test docstrings, state the expected behavior that each test demonstrates.
Don't include preambles such as "Tests that" or "Ensures that".
Thoughts?