Python assertEqual as top-level function

A regular assert statement could be more verbose:

first = 'a'
second = 'aa'
assert first == second
$ 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:

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)
$ 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:

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

Improved assert in:

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)
$ 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)
View this page on GitHub.
Posted .

Comments

Leave a Reply