{
"$type": "site.standard.document",
"canonicalUrl": "https://rednafi.com/python/use-assertis-to-check-literal-booleans/",
"description": "Test literal booleans correctly in Python unittest using assertIs instead of assertTrue/assertFalse to avoid truthy/falsy confusion.",
"path": "/python/use-assertis-to-check-literal-booleans/",
"publishedAt": "2022-01-24T00:00:00.000Z",
"site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
"tags": [
"Python",
"TIL",
"Testing"
],
"textContent": "I used to use Unittest's self.assertTrue / self.assertFalse to check both literal\nbooleans and truthy/falsy values in Unittest. Committed the same sin while writing\ntests in Django.\n\n> I feel like assertTrue and assertFalse are misnomers. They don't specifically check\n> literal booleans, only truthy and falsy states respectively.\n\nConsider this example:\n\nIn the above snippet, I've used assertTrue and assertFalse to check both literal\nbooleans and truthy/falsy values. However, to test the literal boolean values, assertIs\nworks better and is more explicit. Here's how to do the above test properly:\n\nNotice how I've used self.assertIs in the is_true and is_false methods to explicitly\ntest out the literal boolean values. The is_truthy and is_falsy methods were kept\nunchanged from the previous snippet.\n\nFurther reading\n\n- [Tweet by Drewrey Lupton]\n\n\n\n\n[tweet by drewrey lupton]:\n https://twitter.com/chieftanbonobo/status/741689567590395905",
"title": "Use 'assertIs' to check literal booleans in Python unittest"
}