Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do assertions in unittest use TestCase.assertEqual not the assert keyword?

Python's built-in unittest module makes assertions with TestCase.assert* methods:

class FooTest(TestCase):     def test_foo(self):         self.assertEqual(1,1)         self.assertNotEqual(1,2)         self.assertTrue(True) 

I have generally used a testrunner such as nose or py.test which allow use of the built-in assert keyword when making assertions:

assert 1 == 1 assert 1 != 2 assert True 

What is the motivation for unittest's TestCase.assert* approach and what are the strengths and weaknesses of this vs asserting with the built-in assert keyword? Are there reasons why unittest's syntax should be favoured?

like image 648
Stephen Emslie Avatar asked Jun 15 '11 16:06

Stephen Emslie


People also ask

What is assert in unittest?

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.

Why are assertions used in unit testing Python?

Python testing framework uses Python's built-in assert() function which tests a particular condition. If the assertion fails, an AssertionError will be raised. The testing framework will then identify the test as Failure. Other exceptions are treated as Error.

What does assert false do in Python?

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.

Should I use Python assert?

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.


2 Answers

The problem with the assert keyword is that it is optimized out, and thus ignored, when Python is run in 'optimized' mode (with the -O argument or with the PYTHONOPTIMIZE environment variable set.) If tests were to use assert then testing with -O would be impossible.

Additionally, the use of the assert methods makes it trivial to report about what the values involved actually were, without having to dig into the stack and the source and figure out what they were supposed to be (which, I believe, is the technique nose and py.test use for this.)

like image 76
Thomas Wouters Avatar answered Oct 18 '22 06:10

Thomas Wouters


I don't see a concrete design decision for this. Looking at the unittest docs it states that

the type specific equality function will be called in order to generate a more useful default error message

So I would say it is an implementation decision to help produce more meaningful errors etc.

like image 32
Nick Martin Avatar answered Oct 18 '22 06:10

Nick Martin