Using Assert Methods in Unittest: Validating Test Results
The assert methods are part of the unittest.TestCase class and are used to check conditions in your tests.
Simply put, each test method in the Test class concludes with a statement such as self.assert.
Commonly Used assert Methods
| Method | Check | Example |
|---|---|---|
| assertEqual(a, b) | a == b | assertEqual(sum([1, 2, 3]), 6) |
| assertNotEqual(a, b) | a != b | assertNotEqual(1, 2) |
| assertTrue(x) | bool(x) is True | assertTrue(isinstance(123, int)) |
| assertFalse(x) | bool(x) is False | assertFalse(isinstance("hello", int)) |
| assertIs(a, b) | a is b | a = 1, b = a |
| assertIsNone(x) | x is None | book.price = None |
| assertIn(a, b) | a in b | assertIn(2, [1, 2, 3]) |
| assertIsInstance(a, b) | isinstance(a, b) | assertIsInstance(123, int) |
Also, assertRaises(Error, func, *args, **kwargs) is used to test that an error is raised. For example:
with self.assertRaises(ValueError):
int("xyz")
This checks that converting "xyz" to integer raises ValueError.
Swipe to start coding
Your task is to practice using different assert methods in the unittest framework. Complete the TestFunctions class by writing separate test methods for each of the following checks. Use the exact method names given below for each check.
- Check that
add(2, 3)returns5usingassertEqual. Name the method:test_add_equal. - Check that
add(2, 2)does not return5usingassertNotEqual. Name the method:test_add_not_equal. - Check that
is_even(4)returnsTrueusingassertTrue. Name the method:test_is_even_true. - Check that
is_even(5)returnsFalseusingassertFalse. Name the method:test_is_even_false. - Check that the result of
get_item([10, 20, 30], 1)is the same object as20usingassertIs. Name the method:test_get_item_is. - Check that
get_item([1, 2, 3], 10)returnsNoneusingassertIsNone. Name the method:test_get_item_is_none. - Check that
2is in the list[1, 2, 3]usingassertIn. Name the method:test_in_list. - Check that the result of
add(1, 2)is an instance ofintusingassertIsInstance. Name the method:test_add_is_instance.
Write one test method for each check and use only the specified method names above. Each test method should use only one assert method.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you explain the difference between assertEqual and assertIs?
What happens if an assertion fails in a test?
Can you give more examples of using assertRaises?
Genial!
Completion tasa mejorada a 3.13
Using Assert Methods in Unittest: Validating Test Results
Desliza para mostrar el menú
The assert methods are part of the unittest.TestCase class and are used to check conditions in your tests.
Simply put, each test method in the Test class concludes with a statement such as self.assert.
Commonly Used assert Methods
| Method | Check | Example |
|---|---|---|
| assertEqual(a, b) | a == b | assertEqual(sum([1, 2, 3]), 6) |
| assertNotEqual(a, b) | a != b | assertNotEqual(1, 2) |
| assertTrue(x) | bool(x) is True | assertTrue(isinstance(123, int)) |
| assertFalse(x) | bool(x) is False | assertFalse(isinstance("hello", int)) |
| assertIs(a, b) | a is b | a = 1, b = a |
| assertIsNone(x) | x is None | book.price = None |
| assertIn(a, b) | a in b | assertIn(2, [1, 2, 3]) |
| assertIsInstance(a, b) | isinstance(a, b) | assertIsInstance(123, int) |
Also, assertRaises(Error, func, *args, **kwargs) is used to test that an error is raised. For example:
with self.assertRaises(ValueError):
int("xyz")
This checks that converting "xyz" to integer raises ValueError.
Swipe to start coding
Your task is to practice using different assert methods in the unittest framework. Complete the TestFunctions class by writing separate test methods for each of the following checks. Use the exact method names given below for each check.
- Check that
add(2, 3)returns5usingassertEqual. Name the method:test_add_equal. - Check that
add(2, 2)does not return5usingassertNotEqual. Name the method:test_add_not_equal. - Check that
is_even(4)returnsTrueusingassertTrue. Name the method:test_is_even_true. - Check that
is_even(5)returnsFalseusingassertFalse. Name the method:test_is_even_false. - Check that the result of
get_item([10, 20, 30], 1)is the same object as20usingassertIs. Name the method:test_get_item_is. - Check that
get_item([1, 2, 3], 10)returnsNoneusingassertIsNone. Name the method:test_get_item_is_none. - Check that
2is in the list[1, 2, 3]usingassertIn. Name the method:test_in_list. - Check that the result of
add(1, 2)is an instance ofintusingassertIsInstance. Name the method:test_add_is_instance.
Write one test method for each check and use only the specified method names above. Each test method should use only one assert method.
Solución
¡Gracias por tus comentarios!
single