• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Driver test library."""
17import unittest
18
19from unittest import mock
20
21
22class BaseDriverTest(unittest.TestCase):
23    """Base class for driver tests."""
24
25    def setUp(self):
26        """Set up test."""
27        self._patchers = []
28
29    def tearDown(self):
30        """Tear down test."""
31        for patcher in reversed(self._patchers):
32            patcher.stop()
33
34    def Patch(self, *args, **kwargs):
35        """A wrapper for mock.patch.object.
36
37        This wrapper starts a patcher and store it in self._patchers,
38        so that we can later stop them in tearDown.
39
40        Args:
41          *args: Arguments to pass to mock.patch.
42          **kwargs: Keyword arguments to pass to mock.patch.
43
44        Returns:
45          Mock object
46        """
47        patcher = mock.patch.object(*args, **kwargs)
48        self._patchers.append(patcher)
49        return patcher.start()
50