Dark Launch

Python assertEqual as top-level function

A regular assert statement could be more verbose:

Python
first = 'a'
second = 'aa'
assert first == second
Bash
$ python script.py 
Traceback (most recent call last):
  File "script.py", line 3, in <module>
    assert first == second
AssertionError

Here is a top-level function to test an assertion condition in more detail:

Python
import unittest
 
 
def assert_equal(first, second):
    class MyTestCase(unittest.TestCase):
        def test(self):
            self.assertEqual(first, second)
 
    suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
    unittest.TextTestRunner().run(suite)
 
 
first = 'a'
second = 'aa'
assert_equal(first, second)
Bash
$ python script.py 
F
======================================================================
FAIL: test (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "script.py", line 7, in test
    self.assertEqual(first, second)
AssertionError: 'a' != 'aa'
 
----------------------------------------------------------------------
Ran 1 test in 0.000s
 
FAILED (failures=1)

Basic assert in:

Python
first = 'z'
second = ['a', 'b', 'c']
assert first in second
Bash
$ python script.py 
Traceback (most recent call last):
  File "script.py", line 3, in <module>
    assert first in second
AssertionError

Improved assert in:

Python
import unittest
 
 
def assert_in(first, second):
    class MyTestCase(unittest.TestCase):
        def test(self):
            self.assertIn(first, second)
 
    suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
    unittest.TextTestRunner().run(suite)
 
 
first = 'z'
second = ['a', 'b', 'c']
assert_in(first, second)
Bash
$ python script.py 
F
======================================================================
FAIL: test (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "script.py", line 7, in test
    self.assertIn(first, second)
AssertionError: 'z' not found in ['a', 'b', 'c']
 
----------------------------------------------------------------------
Ran 1 test in 0.000s
 
FAILED (failures=1)