Unit tests Python examples

ssuserd327fe1 24 views 8 slides May 07, 2023
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

Presentation with examples of unit tests on Python.


Slide Content

ПРОВЕДЕНИЕ UNIT ТЕСТОВ Москва, 2023 г.

2 Виды тестирования

Создание Unit теста 3 import unittest class TestStringMethods ( unittest.TestCase ): def test_upper ( self ): self.assertEqual (' foo '. upper (), 'FOO') if __ name __ == '__ main __': unittest.main () python -m unittest Запуск класса из консоли: Пример кода для тестового класса:

Тестирование простейшей функции 4 Тестируемая функция: Тестовый класс: import unittest from current import current_calculation class TestCurrent ( unittest.TestCase ): def test_positive ( self ): self.assertEqual ( current_calculation (380, 10), 38) self.assertEqual ( current_calculation (220, 2), 110) self.assertEqual ( current_calculation (12.2, 2), 6.1) if __ name __ == '__ main __': unittest.main () def current_calculation (voltage, resistance): return round(voltage / resistance, 6)

Тестирование простейшей функции 5 Тестируемая функция: Тестовый класс: import unittest from current import current_calculation class TestCurrent ( unittest.TestCase ): def test_positive ( self ): self.assertEqual ( current_calculation (380, 10), 38) self.assertEqual ( current_calculation (220, 2), 110) self.assertEqual ( current_calculation (12.2, 2), 6.1) def test_negative_resistance ( self ): self.assertRaises ( ValueError , current_calculation , 100, -2) if __ name __ == '__ main __': unittest.main () def current_calculation (voltage, resistance): if resistance < 0: raise ValueError ("Resistance can`t be negative") return round(voltage / resistance, 6)

Тестирование простейшей функции 6 Тестируемая функция: Тестовый класс: import unittest from current import current_calculation class TestCurrent ( unittest.TestCase ): def test_positive ( self ): self.assertEqual ( current_calculation (380, 10), 38) self.assertEqual ( current_calculation (220, 2), 110) self.assertEqual ( current_calculation (12.2, 2), 6.1) def test_zero_division ( self ): self.assertRaises ( ZeroDivisionError , current_calculation , 10, 0) def test_negative_resistance ( self ): self.assertRaises ( ValueError , current_calculation , 100, -2) if __ name __ == '__ main __': unittest.main () def current_calculation (voltage, resistance): if resistance == 0: raise ZeroDivisionError ("Division by zero") if resistance < 0: raise ValueError ("Resistance can`t be negative") return round(voltage / resistance, 6)

Тестирование простейшей функции 7 Тестируемая функция: Тестовый класс: import unittest from current import current_calculation class TestCurrent ( unittest.TestCase ): def test_positive ( self ): self.assertEqual ( current_calculation (380, 10), 38) self.assertEqual ( current_calculation (220, 2), 110) self.assertEqual ( current_calculation (12.2, 2), 6.1) def test_type ( self ): self.assertRaises ( TypeError , current_calculation , " Hello World", 2) self.assertRaises ( TypeError , current_calculation , 220, [12]) def test_zero_division ( self ): self.assertRaises ( ZeroDivisionError , current_calculation , 10, 0) def test_negative_resistance ( self ): self.assertRaises ( ValueError , current_calculation , 100, -2) if __ name __ == '__ main __': unittest.main () def current_calculation (voltage, resistance): if type(voltage) not in [ int , float]: raise TypeError ("Voltage must be real number") if type(resistance) not in [ int , float]: raise TypeError ("Resistance must be real number") if resistance == 0: raise ZeroDivisionError ("Division by zero") if resistance < 0: raise ValueError ("Resistance can`t be negative") return round(voltage / resistance, 6)

Документация библиотеки 8 https :// docs.python.org/3/library/unittest.html unittest — Unit testing framework