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