Files
codeql/python/ql/src/Testing/ImpreciseAssert.qhelp
2018-11-19 15:10:42 +00:00

38 lines
1.6 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>The class <code>unittest.TestCase</code> provides a range of assertion methods. As well as the general forms <code>assertTrue()</code> and <code>assertFalse()</code>
more specific forms such as <code>assertGreaterEquals()</code> and <code>assertNotIn()</code> are provided.
By using the more specific forms it is possible to get more precise and informative failure messages in the event of a test failing. This can speed up the debugging process.
</p>
</overview>
<recommendation>
<p>Replace all calls to <code>assertTrue()</code> and <code>assertFalse()</code> that do not provide a custom failure message with a more specific variant.
Alternatively, provide a tailored failure message using the <code>assertTrue(condition, message)</code> form.
</p>
</recommendation>
<example>
<p>In this example, <code>assertTrue()</code> and <code>assertFalse()</code> are used.</p>
<sample src="ImpreciseAssert.py" />
<p>
This will make it more difficult to determine what has gone wrong when <code>self.assertTrue(1 in [])</code> fails.
The failure message "AssertionError: False is not true" is not very helpful.
</p>
<p>A more useful error message can be generated by changing the asserts to the more specific forms as in the following example.</p>
<sample src="ImpreciseAssert2.py" />
<p>In this case, the failure message "AssertionError: 1 not found in []" is much more informative.</p>
</example>
<references>
<li>Python library reference: <a href="https://docs.python.org/library/unittest.html#unittest.TestCase.assertEqual">TestCase.assertEqual</a>.</li>
</references>
</qhelp>