1#!/usr/bin/env python
2#
3# Copyright 2017 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Driver test library."""
18
19import mock
20import unittest
21
22
23class BaseDriverTest(unittest.TestCase):
24    """Base class for driver tests."""
25
26    def setUp(self):
27        """Set up test."""
28        self._patchers = []
29
30    def tearDown(self):
31        """Tear down test."""
32        for patcher in reversed(self._patchers):
33            patcher.stop()
34
35    def Patch(self, *args, **kwargs):
36        """A wrapper for mock.patch.object.
37
38        This wrapper starts a patcher and store it in self._patchers,
39        so that we can later stop them in tearDown.
40
41        Args:
42          *args: Arguments to pass to mock.patch.
43          **kwargs: Keyword arguments to pass to mock.patch.
44
45        Returns:
46          Mock object
47        """
48        patcher = mock.patch.object(*args, **kwargs)
49        self._patchers.append(patcher)
50        return patcher.start()
51