class TestPatchClass(TestCase): def setUp(self): self.obj = PatchClass() @patch('class_under_test.PatchClass.m_2') def test_patch_as_decorator(self, m_2_mock): self.obj.m_1() m_2_mock.assert_called()
class TestPatchObjects(TestCase): def setUp(self): self.obj = PatchClass() def test_patch_object_class_attribute_1(self, spec=True): with patch.object(self.obj, 'attribute_1') as acm: self.obj.m_4() self.assertEqual(10, self.obj.get_attribute_1()) def test_patch_private_objects_attribute_2(self, spec=True): with patch.object(self.obj, '_PatchClass__attribute_2') as acm: with patch.object(self.obj, '_PatchClass__get_attribute_2') as fn_cm: self.obj.m_4() self.assertEqual(20, self.obj.get_attribute_2()) fn_cm.return_value = 100 self.assertEqual(fn_cm.return_value, self.obj._PatchClass__get_attribute_2())
class TestPatchClass(TestCase): def setUp(self): self.obj = PatchClass() def test_patch_as_context_manager_with_incorrect_import(self): with patch('PatchClass.m_2') as cm: self.obj.m_1() cm.assert_called() def test_patch_as_context_manager_with_correct_assert(self): with patch('class_under_test.PatchClass.m_2') as cm: self.obj.m_1() cm.assert_called() def test_patch_as_context_manager_with_incorrect_assert(self): with patch('class_under_test.PatchClass.m_2') as cm: self.obj.m_1() cm.assert_not_called()
def setUp(self): self.obj = PatchClass()