Assert Function in Python

Yash Soni
2 min readMar 25, 2022

Python has a very powerful testing framework to help you test your code. When you run tests, each test file is executed in isolation from the others and its specific name does not matter. Each test is simply a function that receives no arguments. The function must be decorated with @pytest.mark.test or the equivalent @test. In order to pass, it has to end without raising any exceptions. The 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 a Failure. Other exceptions are treated as Errors.

Some of the built-in assert functions are :

  1. assertEqual(arg1, arg2, msg = None)

Test that arg1 and arg2 are equal. If the values do not compare equally, the test will fail.

def check(x):
if x % 2 == 0:
return "even"
else:
return "odd"

class EvenorOddApp(unittest.TestCase):

def test_case_evencase_check(self):
x = 10
result = check(x)
self.assertEqual("even", result)

2. assertNotEqual(arg1, arg2, msg = None)

Test that arg1 and arg2 are not equal. If the values do compare equally, the test will fail.

class EvenorOddApp(unittest.TestCase):

def test_case_evencase_check(self):
x = 10
result = check(x)
self.assertNotEqual("odd", result)

3. assertAlmostEqual(arg1, arg2, msg = None)

class CheckVolume(unittest.TestCase):
def test_Volume(self):
x = 5.55
result = cube_volume(x)
self.assertAlmostEqual(result, x*x*x )

4. assertTrue(expr, msg = None)

Test that expression is true. If false, the test fails.

def divisibleby7(x):
if x%7==0:
return True
else:
return False

class CheckDivisible(unittest.TestCase):
def test_case_divisible(self):
x = 14
result = divisibleby7(x)
self.assertTrue(result)

5. assertFalse(expr, msg = None)

Test that expression is false. If true, the test fails

def test_case_divisible1(self):
x = 9
result = divisibleby7(x)
self.assertFalse(result)

6. assertIs(arg1, arg2, msg = None)

Test that arg1 and arg2 evaluate to the same object.

def dummy(x):
return x

class CheckDummy(unittest.TestCase):
def test_case_dummy(self):
x = 10
first = dummy(x)
second = dummy(x)
self.assertIs(first, second)

7. assertIsNot(arg1, arg2, msg = None)

Test that arg1 and arg2 don’t evaluate the same object.

def dummy(x):
return x

def dummy1(y):
return y
class CheckDummy(unittest.TestCase):
def test_case_dummy(self):
x = 10
y = 11
first = dummy(x)
second = dummy1(y)
self.assertIsNot(first, second)

8. assertIsNone(expr, msg = None)

Test that expression is None. If None, the test fails

class CheckDummy(unittest.TestCase):
def test_case_dummy(self):
first = None
self.assertIsNone(first)

9. assertIsNotNone(expr, msg = None)

Test that expression is not None. If None, the test fails

class CheckDummy(unittest.TestCase):
def test_case_dummy(self):
first = "yash"
self.assertIsNotNone(first)

10. assertIn(arg1, arg2, msg = None)

Test that arg1 is in arg2.

class CheckDummy(unittest.TestCase):
def test_case_dummy(self):
first = "yash"
second = "yash is smart"
self.assertIn(first, second)

11. assertNotIn(arg1, arg2, msg = None)

Test that arg1 is not in arg2.

class CheckDummy(unittest.TestCase):
def test_case_dummy(self):
first = "yash"
second = "Piyush is smart"
self.assertNotIn(first,second)

These were the functions I learned and have explained with examples. I hope you like this blog.

Thank you!

--

--