Add Mocks to Test Class

from unittest.mock import patch

class TestClassroom:
# tests here

Creating Fake Objects

from unittest.mock import patch
from people.Person import Person
from people.Teacher import Teacher
from places.Classroom import Classroom

class TestClassroom:
@patch('people.Teacher', spec=Teacher) @patch('people.Person', spec=Person) def test_classroom_has_teacher(self, fake_person, fake_teacher) -> None: classroom: Classroom = Classroom() assert classroom.has_teacher == False
classroom.add_teacher(fake_teacher) assert classroom.has_teacher == True

Adding a Method Stub

@patch('people.Teacher', spec=Teacher)
@patch('people.Person', spec=Person)
def test_classroom_get_teacher_name(self, fake_person, fake_teacher) -> None:
    # create a method stub for `get_name` method
    fake_teacher.get_name.return_value = "Teacher Person"
classroom: Classroom = Classroom() classroom.add_teacher(fake_teacher)
# assert that the classroom returns the teacher's name assert classroom.get_teacher_name() == "Teacher Person"

Adding a Property Stub

@patch('people.Teacher', spec=Teacher)
@patch('people.Person', spec=Person)
def test_classroom_get_teacher_name(self, fake_person, fake_teacher) -> None:
    # create a property stub for `get_name` property
    type(fake_teacher).name = PropertyMock(return_value="Teacher Person")
classroom: Classroom = Classroom() classroom.add_teacher(fake_teacher)
# assert that the classroom returns the teacher's name assert classroom.get_teacher_name() == "Teacher Person"

Static Classes

@patch('people.Teacher', spec=Teacher)
@patch('people.Person', spec=Person)
def test_teacher_fails_minimum_age_requirement(self, fake_person, fake_teacher) -> None:
    # create a fake version of the static method
    with patch.object(TeacherRules, 'get_minimum_age', return_value=16):
# Add a fake property to the teacher type(fake_teacher).age = PropertyMock(return_value=15) classroom: Classroom = Classroom()
with pytest.raises(ValueError): classroom.add_teacher(fake_teacher)