I am writing tests and I have heard some people saying to use self.assertFalse
rather than assert False
. Why is this and are there any advantages to be had?
The assert section ensures that the code behaves as expected. Assertions replace us humans in checking that the software does what it should. They express requirements that the unit under test is expected to meet. Now, often one can write slightly different assertions to capture a given requirement.
assertFalse() in Python is a unittest library function that is used in unit testing to compare test value with false. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is false then assertFalse() will return true else return false.
Assert statements are used to debug code and handle errors. You should not use an assert statement in a production environment. Debugging is a crucial part of programming in any language. When debugging programs in Python, there may be times when you want to test for a certain condition.
The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.
If you run
import unittest class Test_Unittest(unittest.TestCase): def test_assert(self): assert False def test_assertFalse(self): self.assertFalse(True) if __name__ == '__main__': unittest.main()
You get the same logging information, the same failure:
FF ====================================================================== FAIL: test_assert (__main__.Test_Unittest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/unutbu/pybin/test.py", line 6, in test_assert assert False AssertionError ====================================================================== FAIL: test_assertFalse (__main__.Test_Unittest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/unutbu/pybin/test.py", line 8, in test_assertFalse self.assertFalse(True) AssertionError ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=2)
The reason both are handled the same is because unittest.TestCase
defines
failureException = AssertionError
When you say assert False
an AssertionError is raised.
When you say self.assertFalse(True)
, a failureExeception
is raised.
Since these exceptions are the same, there is no apparent difference.
assert
and self.assertFalse
do differ in conventional usage, however.
assert
is used to declare that a certain condition should hold at a certain point in the code. It is used as a crutch during development, but is not meant to be used in production code. If you run python -O my_unittest.py
, all assert statements are ignored. That would subvert your intended usage of assert
, possibly making your unit tests pass even when there is a failure.
Even though (without the -O flag) the result is the same, assert
is not meant to be used in unit test code. Use self.assertTrue
or self.assertFalse
when writing unit tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With