Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Using Assert Methods in Unittest: Validating Test Results | Mastering Unittest Framework
Python Structural Programming

bookUsing 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

MethodCheckExample
assertEqual(a, b)a == bassertEqual(sum([1, 2, 3]), 6)
assertNotEqual(a, b)a != bassertNotEqual(1, 2)
assertTrue(x)bool(x) is TrueassertTrue(isinstance(123, int))
assertFalse(x)bool(x) is FalseassertFalse(isinstance("hello", int))
assertIs(a, b)a is ba = 1, b = a
assertIsNone(x)x is Nonebook.price = None
assertIn(a, b)a in bassertIn(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.

Tarefa

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) returns 5 using assertEqual. Name the method: test_add_equal.
  • Check that add(2, 2) does not return 5 using assertNotEqual. Name the method: test_add_not_equal.
  • Check that is_even(4) returns True using assertTrue. Name the method: test_is_even_true.
  • Check that is_even(5) returns False using assertFalse. Name the method: test_is_even_false.
  • Check that the result of get_item([10, 20, 30], 1) is the same object as 20 using assertIs. Name the method: test_get_item_is.
  • Check that get_item([1, 2, 3], 10) returns None using assertIsNone. Name the method: test_get_item_is_none.
  • Check that 2 is in the list [1, 2, 3] using assertIn. Name the method: test_in_list.
  • Check that the result of add(1, 2) is an instance of int using assertIsInstance. 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.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 2
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookUsing Assert Methods in Unittest: Validating Test Results

Deslize para mostrar o menu

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

MethodCheckExample
assertEqual(a, b)a == bassertEqual(sum([1, 2, 3]), 6)
assertNotEqual(a, b)a != bassertNotEqual(1, 2)
assertTrue(x)bool(x) is TrueassertTrue(isinstance(123, int))
assertFalse(x)bool(x) is FalseassertFalse(isinstance("hello", int))
assertIs(a, b)a is ba = 1, b = a
assertIsNone(x)x is Nonebook.price = None
assertIn(a, b)a in bassertIn(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.

Tarefa

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) returns 5 using assertEqual. Name the method: test_add_equal.
  • Check that add(2, 2) does not return 5 using assertNotEqual. Name the method: test_add_not_equal.
  • Check that is_even(4) returns True using assertTrue. Name the method: test_is_even_true.
  • Check that is_even(5) returns False using assertFalse. Name the method: test_is_even_false.
  • Check that the result of get_item([10, 20, 30], 1) is the same object as 20 using assertIs. Name the method: test_get_item_is.
  • Check that get_item([1, 2, 3], 10) returns None using assertIsNone. Name the method: test_get_item_is_none.
  • Check that 2 is in the list [1, 2, 3] using assertIn. Name the method: test_in_list.
  • Check that the result of add(1, 2) is an instance of int using assertIsInstance. 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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 2
single

single

some-alt