Introduction to Testing in Python
Alexander Levin
Data Scientist
class Rectangle:
# Constructor of Rectangle
def __init__(self, a, b):
self.a = a
self.b = b
# Area method
def get_area(self):
return self.a * self.b
# Usage example
r = Rectangle(4, 5)
print(r.get_area())
>> 20
class RedRectangle(Rectangle):
self.color = 'red'
unittest
- built-in Python framework for test automation (it is installed with Python
).unittest
- not only for unit tests alone.unittest
Python
distribution)pytest
test_
Third-party package (has to be installed separately from the Python
distribution)
Less assertion methods
Test of the exponentiation operator:
import unittest
# Declaring the TestCase class
class TestSquared(unittest.TestCase):
# Defining the test
def test_negative(self):
self.assertEqual((-3) ** 2, 9)
.assertEqual()
, .assertNotEqual()
.assertTrue()
, .assertFalse()
.assertIs()
, .assertIsNone()
.assertIsInstance()
, .assertIn()
.assertRaises()
unittest
- OOP-based built-in Python framework for test automationunittest
unittest.TestCase
Introduction to Testing in Python