Best Python code snippet using uiautomator
fastbootsubp_unittest.py
Source:fastbootsubp_unittest.py  
1# Copyright 2017 The Android Open Source Project2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Unit test for fastboot interface using subprocess library."""15import subprocess16import unittest17import fastboot_exceptions18import fastbootsubp19from mock import patch20CREATE_NO_WINDOW = 0x0800000021class TestError(subprocess.CalledProcessError):22  def __init__(self):23    pass24class FastbootSubpTest(unittest.TestCase):25  ATFA_TEST_SERIAL = 'ATFA_TEST_SERIAL'26  TEST_MESSAGE_FAILURE = 'FAIL: TEST MESSAGE'27  TEST_MESSAGE_SUCCESS = 'OKAY: TEST MESSAGE'28  TEST_SERIAL = 'TEST_SERIAL'29  TEST_VAR = 'VAR1'30  TEST_MESSAGE = 'TEST MESSAGE'31  def setUp(self):32    fastbootsubp.FastbootDevice.fastboot_command = 'fastboot'33  # Test FastbootDevice.ListDevices34  @patch('subprocess.check_output', create=True)35  def testListDevicesOneDevice(self, mock_fastboot_commands):36    mock_fastboot_commands.return_value = self.TEST_SERIAL + '\tfastboot'37    device_serial_numbers = fastbootsubp.FastbootDevice.ListDevices()38    mock_fastboot_commands.assert_called_once_with(39        ['fastboot', 'devices'], creationflags=CREATE_NO_WINDOW)40    self.assertEqual(1, len(device_serial_numbers))41    self.assertEqual(self.TEST_SERIAL, device_serial_numbers[0])42  @patch('subprocess.check_output', create=True)43  def testListDevicesTwoDevices(self, mock_fastboot_commands):44    mock_fastboot_commands.return_value = (self.TEST_SERIAL + '\tfastboot\n' +45                                           self.ATFA_TEST_SERIAL + '\tfastboot')46    device_serial_numbers = fastbootsubp.FastbootDevice.ListDevices()47    mock_fastboot_commands.assert_called_once_with(48        ['fastboot', 'devices'], creationflags=CREATE_NO_WINDOW)49    self.assertEqual(2, len(device_serial_numbers))50    self.assertEqual(self.TEST_SERIAL, device_serial_numbers[0])51    self.assertEqual(self.ATFA_TEST_SERIAL, device_serial_numbers[1])52  @patch('subprocess.check_output', create=True)53  def testListDevicesTwoDevicesCRLF(self, mock_fastboot_commands):54    mock_fastboot_commands.return_value = (self.TEST_SERIAL + '\tfastboot\r\n' +55                                           self.ATFA_TEST_SERIAL + '\tfastboot')56    device_serial_numbers = fastbootsubp.FastbootDevice.ListDevices()57    mock_fastboot_commands.assert_called_once_with(58        ['fastboot', 'devices'], creationflags=CREATE_NO_WINDOW)59    self.assertEqual(2, len(device_serial_numbers))60    self.assertEqual(self.TEST_SERIAL, device_serial_numbers[0])61    self.assertEqual(self.ATFA_TEST_SERIAL, device_serial_numbers[1])62  @patch('subprocess.check_output', create=True)63  def testListDevicesMultiDevices(self, mock_fastboot_commands):64    one_device = self.TEST_SERIAL + '\tfastboot'65    result = one_device66    for _ in range(0, 9):67      result += '\n' + one_device68    mock_fastboot_commands.return_value = result69    device_serial_numbers = fastbootsubp.FastbootDevice.ListDevices()70    mock_fastboot_commands.assert_called_once_with(71        ['fastboot', 'devices'], creationflags=CREATE_NO_WINDOW)72    self.assertEqual(10, len(device_serial_numbers))73  @patch('subprocess.check_output', create=True)74  def testListDevicesNone(self, mock_fastboot_commands):75    mock_fastboot_commands.return_value = ''76    device_serial_numbers = fastbootsubp.FastbootDevice.ListDevices()77    mock_fastboot_commands.assert_called_once_with(78        ['fastboot', 'devices'], creationflags=CREATE_NO_WINDOW)79    self.assertEqual(0, len(device_serial_numbers))80  @patch('subprocess.check_output', create=True)81  def testListDevicesFailure(self, mock_fastboot_commands):82    mock_error = TestError()83    mock_error.output = self.TEST_MESSAGE_FAILURE84    mock_fastboot_commands.side_effect = mock_error85    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:86      fastbootsubp.FastbootDevice.ListDevices()87    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))88  # Test FastbootDevice.Oem89  @patch('subprocess.check_output', create=True)90  def testOem(self, mock_fastboot_commands):91    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS92    command = 'TEST COMMAND'93    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)94    message = device.Oem(command, False)95    mock_fastboot_commands.assert_called_once_with(96        ['fastboot', '-s', self.TEST_SERIAL, 'oem', command],97        stderr=subprocess.STDOUT,98        creationflags=CREATE_NO_WINDOW)99    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)100  @patch('subprocess.check_output', create=True)101  def testOemErrToOut(self, mock_fastboot_commands):102    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS103    command = 'TEST COMMAND'104    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)105    message = device.Oem(command, True)106    mock_fastboot_commands.assert_called_once_with(107        ['fastboot', '-s', self.TEST_SERIAL, 'oem', command],108        stderr=subprocess.STDOUT,109        creationflags=CREATE_NO_WINDOW)110    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)111  @patch('subprocess.check_output', create=True)112  def testOemFailure(self, mock_fastboot_commands):113    mock_error = TestError()114    mock_error.output = self.TEST_MESSAGE_FAILURE115    mock_fastboot_commands.side_effect = mock_error116    command = 'TEST COMMAND'117    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)118    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:119      device.Oem(command, False)120    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))121  # Test FastbootDevice.Upload122  @patch('subprocess.check_output', create=True)123  def testUpload(self, mock_fastboot_commands):124    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS125    command = 'TEST COMMAND'126    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)127    message = device.Upload(command)128    mock_fastboot_commands.assert_called_once_with(129        ['fastboot', '-s', self.TEST_SERIAL, 'get_staged', command],130        creationflags=CREATE_NO_WINDOW)131    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)132  @patch('subprocess.check_output', create=True)133  def testUploadFailure(self, mock_fastboot_commands):134    mock_error = TestError()135    mock_error.output = self.TEST_MESSAGE_FAILURE136    mock_fastboot_commands.side_effect = mock_error137    command = 'TEST COMMAND'138    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)139    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:140      device.Upload(command)141    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))142  # Test FastbootDevice.Download143  @patch('subprocess.check_output', create=True)144  def testDownload(self, mock_fastboot_commands):145    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS146    command = 'TEST COMMAND'147    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)148    message = device.Download(command)149    mock_fastboot_commands.assert_called_once_with(150        ['fastboot', '-s', self.TEST_SERIAL, 'stage', command],151        creationflags=CREATE_NO_WINDOW)152    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)153  @patch('subprocess.check_output', create=True)154  def testDownloadFailure(self, mock_fastboot_commands):155    mock_error = TestError()156    mock_error.output = self.TEST_MESSAGE_FAILURE157    mock_fastboot_commands.side_effect = mock_error158    command = 'TEST COMMAND'159    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)160    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:161      device.Download(command)162    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))163  # Test FastbootDevice.GetVar164  @patch('subprocess.check_output', create=True)165  def testGetVar(self, mock_fastboot_commands):166    mock_fastboot_commands.return_value = (167        self.TEST_VAR + ': ' + self.TEST_MESSAGE)168    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)169    message = device.GetVar(self.TEST_VAR)170    mock_fastboot_commands.assert_called_once_with(171        ['fastboot', '-s', self.TEST_SERIAL, 'getvar', self.TEST_VAR],172        stderr=subprocess.STDOUT,173        shell=True,174        creationflags=CREATE_NO_WINDOW)175    self.assertEqual(self.TEST_MESSAGE, message)176  @patch('subprocess.check_output', create=True)177  def testGetVarFailure(self, mock_fastboot_commands):178    mock_error = TestError()179    mock_error.output = self.TEST_MESSAGE_FAILURE180    mock_fastboot_commands.side_effect = mock_error181    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)182    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:183      device.GetVar(self.TEST_VAR)184    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))185  # Test FastbootDevice.Reboot186  @patch('subprocess.check_output', create=True)187  def testGetVar(self, mock_fastboot_commands):188    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)189    message = device.Reboot()190    mock_fastboot_commands.assert_called_once_with(191        ['fastboot', '-s', self.TEST_SERIAL, 'reboot-bootloader'],192        creationflags=CREATE_NO_WINDOW)193  @patch('subprocess.check_output', create=True)194  def testGetVarFailure(self, mock_fastboot_commands):195    mock_error = TestError()196    mock_error.output = self.TEST_MESSAGE_FAILURE197    mock_fastboot_commands.side_effect = mock_error198    device = fastbootsubp.FastbootDevice(self.TEST_SERIAL)199    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:200      device.Reboot()201    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))202if __name__ == '__main__':...fastbootsh_unittest.py
Source:fastbootsh_unittest.py  
1# Copyright 2017 The Android Open Source Project2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Unit test for fastboot interface using sh library."""15import unittest16import fastboot_exceptions17import fastbootsh18from mock import patch19import sh20class FastbootShTest(unittest.TestCase):21  ATFA_TEST_SERIAL = 'ATFA_TEST_SERIAL'22  TEST_MESSAGE_FAILURE = 'FAIL: TEST MESSAGE'23  TEST_MESSAGE_SUCCESS = 'OKAY: TEST MESSAGE'24  TEST_SERIAL = 'TEST_SERIAL'25  TEST_VAR = 'VAR1'26  class TestError(sh.ErrorReturnCode):27    def __init__(self):28      pass29  def setUp(self):30    pass31  # Test FastbootDevice.ListDevices32  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)33  def testListDevicesOneDevice(self, mock_fastboot_commands):34    mock_fastboot_commands.return_value = self.TEST_SERIAL + '\tfastboot'35    device_serial_numbers = fastbootsh.FastbootDevice.ListDevices()36    mock_fastboot_commands.assert_called_once_with('devices')37    self.assertEqual(1, len(device_serial_numbers))38    self.assertEqual(self.TEST_SERIAL, device_serial_numbers[0])39  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)40  def testListDevicesTwoDevices(self, mock_fastboot_commands):41    mock_fastboot_commands.return_value = (self.TEST_SERIAL + '\tfastboot\n' +42                                           self.ATFA_TEST_SERIAL + '\tfastboot')43    device_serial_numbers = fastbootsh.FastbootDevice.ListDevices()44    mock_fastboot_commands.assert_called_once_with('devices')45    self.assertEqual(2, len(device_serial_numbers))46    self.assertEqual(self.TEST_SERIAL, device_serial_numbers[0])47    self.assertEqual(self.ATFA_TEST_SERIAL, device_serial_numbers[1])48  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)49  def testListDevicesMultiDevices(self, mock_fastboot_commands):50    one_device = self.TEST_SERIAL + '\tfastboot'51    result = one_device52    for _ in range(0, 9):53      result += '\n' + one_device54    mock_fastboot_commands.return_value = result55    device_serial_numbers = fastbootsh.FastbootDevice.ListDevices()56    mock_fastboot_commands.assert_called_once_with('devices')57    self.assertEqual(10, len(device_serial_numbers))58  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)59  def testListDevicesNone(self, mock_fastboot_commands):60    mock_fastboot_commands.return_value = ''61    device_serial_numbers = fastbootsh.FastbootDevice.ListDevices()62    mock_fastboot_commands.assert_called_once_with('devices')63    self.assertEqual(0, len(device_serial_numbers))64  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)65  def testListDevicesFailure(self, mock_fastboot_commands):66    mock_error = self.TestError()67    mock_error.stderr = self.TEST_MESSAGE_FAILURE68    mock_fastboot_commands.side_effect = mock_error69    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:70      fastbootsh.FastbootDevice.ListDevices()71    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))72  # Test FastbootDevice.Oem73  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)74  def testOem(self, mock_fastboot_commands):75    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS76    command = 'TEST COMMAND'77    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)78    message = device.Oem(command, False)79    mock_fastboot_commands.assert_called_once_with('-s',80                                                   self.TEST_SERIAL,81                                                   'oem',82                                                   command,83                                                   _err_to_out=False)84    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)85  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)86  def testOemErrToOut(self, mock_fastboot_commands):87    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS88    command = 'TEST COMMAND'89    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)90    message = device.Oem(command, True)91    mock_fastboot_commands.assert_called_once_with('-s',92                                                   self.TEST_SERIAL,93                                                   'oem',94                                                   command,95                                                   _err_to_out=True)96    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)97  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)98  def testOemFailure(self, mock_fastboot_commands):99    mock_error = self.TestError()100    mock_error.stderr = self.TEST_MESSAGE_FAILURE101    mock_fastboot_commands.side_effect = mock_error102    command = 'TEST COMMAND'103    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)104    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:105      device.Oem(command, False)106    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))107  # Test FastbootDevice.Upload108  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)109  def testUpload(self, mock_fastboot_commands):110    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS111    command = 'TEST COMMAND'112    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)113    message = device.Upload(command)114    mock_fastboot_commands.assert_called_once_with('-s',115                                                   self.TEST_SERIAL,116                                                   'get_staged',117                                                   command)118    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)119  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)120  def testUploadFailure(self, mock_fastboot_commands):121    mock_error = self.TestError()122    mock_error.stderr = self.TEST_MESSAGE_FAILURE123    mock_fastboot_commands.side_effect = mock_error124    command = 'TEST COMMAND'125    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)126    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:127      device.Upload(command)128    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))129  # Test FastbootDevice.Download130  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)131  def testDownload(self, mock_fastboot_commands):132    mock_fastboot_commands.return_value = self.TEST_MESSAGE_SUCCESS133    command = 'TEST COMMAND'134    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)135    message = device.Download(command)136    mock_fastboot_commands.assert_called_once_with('-s',137                                                   self.TEST_SERIAL,138                                                   'stage',139                                                   command)140    self.assertEqual(self.TEST_MESSAGE_SUCCESS, message)141  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)142  def testDownloadFailure(self, mock_fastboot_commands):143    mock_error = self.TestError()144    mock_error.stderr = self.TEST_MESSAGE_FAILURE145    mock_fastboot_commands.side_effect = mock_error146    command = 'TEST COMMAND'147    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)148    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:149      device.Download(command)150    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))151  # Test FastbootDevice.GetVar152  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)153  def testGetVar(self, mock_fastboot_commands):154    mock_fastboot_commands.return_value = self.TEST_VAR + ': ' + 'abcd'155    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)156    message = device.GetVar(self.TEST_VAR)157    mock_fastboot_commands.assert_called_once_with('-s',158                                                   self.TEST_SERIAL,159                                                   'getvar',160                                                   self.TEST_VAR,161                                                   _err_to_out=True)162    self.assertEqual('abcd', message)163  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)164  def testGetVarFailure(self, mock_fastboot_commands):165    mock_error = self.TestError()166    mock_error.stdout = self.TEST_MESSAGE_FAILURE167    mock_fastboot_commands.side_effect = mock_error168    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)169    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:170      device.GetVar(self.TEST_VAR)171    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))172  # Test FastbootDevice.Reboot173  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)174  def testReboot(self, mock_fastboot_commands):175    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)176    device.Reboot()177    mock_fastboot_commands.assert_called_once_with('-s',178                                                   self.TEST_SERIAL,179                                                   'reboot-bootloader')180  @patch('fastbootsh.FastbootDevice.fastboot_command', create=True)181  def testRebootFailure(self, mock_fastboot_commands):182    mock_error = self.TestError()183    mock_error.stderr = self.TEST_MESSAGE_FAILURE184    mock_fastboot_commands.side_effect = mock_error185    device = fastbootsh.FastbootDevice(self.TEST_SERIAL)186    with self.assertRaises(fastboot_exceptions.FastbootFailure) as e:187      device.Reboot()188    self.assertEqual(self.TEST_MESSAGE_FAILURE, str(e.exception))189if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
