Best Python code snippet using Airtest
logcat_monitor_test.py
Source:logcat_monitor_test.py  
1#!/usr/bin/env python2# Copyright 2015 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5# pylint: disable=protected-access6import itertools7import threading8import unittest9import six10from devil import devil_env11from devil.android import logcat_monitor12from devil.android.sdk import adb_wrapper13with devil_env.SysPath(devil_env.PYMOCK_PATH):14  import mock  # pylint: disable=import-error15def _CreateTestLog(raw_logcat=None):16  test_adb = adb_wrapper.AdbWrapper('0123456789abcdef')17  test_adb.Logcat = mock.Mock(return_value=(l for l in raw_logcat))18  test_log = logcat_monitor.LogcatMonitor(test_adb, clear=False)19  return test_log20def zip_longest(expected, actual):21  # pylint: disable=no-member22  if six.PY2:23    return itertools.izip_longest(expected, actual)24  else:25    return itertools.zip_longest(expected, actual)26class LogcatMonitorTest(unittest.TestCase):27  _TEST_THREADTIME_LOGCAT_DATA = [28      '01-01 01:02:03.456  7890  0987 V LogcatMonitorTest: '29      'verbose logcat monitor test message 1',30      '01-01 01:02:03.457  8901  1098 D LogcatMonitorTest: '31      'debug logcat monitor test message 2',32      '01-01 01:02:03.458  9012  2109 I LogcatMonitorTest: '33      'info logcat monitor test message 3',34      '01-01 01:02:03.459  0123  3210 W LogcatMonitorTest: '35      'warning logcat monitor test message 4',36      '01-01 01:02:03.460  1234  4321 E LogcatMonitorTest: '37      'error logcat monitor test message 5',38      '01-01 01:02:03.461  2345  5432 F LogcatMonitorTest: '39      'fatal logcat monitor test message 6',40      '01-01 01:02:03.462  3456  6543 D LogcatMonitorTest: '41      'last line'42  ]43  def assertIterEqual(self, expected_iter, actual_iter):44    for expected, actual in zip_longest(expected_iter, actual_iter):45      self.assertIsNotNone(46          expected,47          msg='actual has unexpected elements starting with %s' % str(actual))48      self.assertIsNotNone(49          actual,50          msg='actual is missing elements starting with %s' % str(expected))51      self.assertEqual(actual.group('proc_id'), expected[0])52      self.assertEqual(actual.group('thread_id'), expected[1])53      self.assertEqual(actual.group('log_level'), expected[2])54      self.assertEqual(actual.group('component'), expected[3])55      self.assertEqual(actual.group('message'), expected[4])56    with self.assertRaises(StopIteration):57      next(actual_iter)58    with self.assertRaises(StopIteration):59      next(expected_iter)60  @mock.patch('time.sleep', mock.Mock())61  def testWaitFor_success(self):62    test_log = _CreateTestLog(63        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)64    test_log.Start()65    actual_match = test_log.WaitFor(r'.*(fatal|error) logcat monitor.*', None)66    self.assertTrue(actual_match)67    self.assertEqual(68        '01-01 01:02:03.460  1234  4321 E LogcatMonitorTest: '69        'error logcat monitor test message 5', actual_match.group(0))70    self.assertEqual('error', actual_match.group(1))71    test_log.Stop()72    test_log.Close()73  @mock.patch('time.sleep', mock.Mock())74  def testWaitFor_failure(self):75    test_log = _CreateTestLog(76        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)77    test_log.Start()78    actual_match = test_log.WaitFor(r'.*My Success Regex.*',79                                    r'.*(fatal|error) logcat monitor.*')80    self.assertIsNone(actual_match)81    test_log.Stop()82    test_log.Close()83  @mock.patch('time.sleep', mock.Mock())84  def testWaitFor_buffering(self):85    # Simulate an adb log stream which does not complete until the test tells it86    # to. This checks that the log matcher can receive individual lines from the87    # log reader thread even if adb is not producing enough output to fill an88    # entire file io buffer.89    finished_lock = threading.Lock()90    finished_lock.acquire()91    def LogGenerator():92      for line in type(self)._TEST_THREADTIME_LOGCAT_DATA:93        yield line94      finished_lock.acquire()95    test_adb = adb_wrapper.AdbWrapper('0123456789abcdef')96    test_adb.Logcat = mock.Mock(return_value=LogGenerator())97    test_log = logcat_monitor.LogcatMonitor(test_adb, clear=False)98    test_log.Start()99    actual_match = test_log.WaitFor(r'.*last line.*', None)100    finished_lock.release()101    self.assertTrue(actual_match)102    test_log.Stop()103    test_log.Close()104  @mock.patch('time.sleep', mock.Mock())105  def testFindAll_defaults(self):106    test_log = _CreateTestLog(107        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)108    test_log.Start()109    test_log.WaitFor(r'.*last line.*', None)110    test_log.Stop()111    expected_results = [('7890', '0987', 'V', 'LogcatMonitorTest',112                         'verbose logcat monitor test message 1'),113                        ('8901', '1098', 'D', 'LogcatMonitorTest',114                         'debug logcat monitor test message 2'),115                        ('9012', '2109', 'I', 'LogcatMonitorTest',116                         'info logcat monitor test message 3'),117                        ('0123', '3210', 'W', 'LogcatMonitorTest',118                         'warning logcat monitor test message 4'),119                        ('1234', '4321', 'E', 'LogcatMonitorTest',120                         'error logcat monitor test message 5'),121                        ('2345', '5432', 'F', 'LogcatMonitorTest',122                         'fatal logcat monitor test message 6')]123    actual_results = test_log.FindAll(r'\S* logcat monitor test message \d')124    self.assertIterEqual(iter(expected_results), actual_results)125    test_log.Close()126  @mock.patch('time.sleep', mock.Mock())127  def testFindAll_defaults_miss(self):128    test_log = _CreateTestLog(129        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)130    test_log.Start()131    test_log.WaitFor(r'.*last line.*', None)132    test_log.Stop()133    expected_results = []134    actual_results = test_log.FindAll(r'\S* nothing should match this \d')135    self.assertIterEqual(iter(expected_results), actual_results)136    test_log.Close()137  @mock.patch('time.sleep', mock.Mock())138  def testFindAll_filterProcId(self):139    test_log = _CreateTestLog(140        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)141    test_log.Start()142    test_log.WaitFor(r'.*last line.*', None)143    test_log.Stop()144    actual_results = test_log.FindAll(145        r'\S* logcat monitor test message \d', proc_id=1234)146    expected_results = [('1234', '4321', 'E', 'LogcatMonitorTest',147                         'error logcat monitor test message 5')]148    self.assertIterEqual(iter(expected_results), actual_results)149    test_log.Close()150  @mock.patch('time.sleep', mock.Mock())151  def testFindAll_filterThreadId(self):152    test_log = _CreateTestLog(153        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)154    test_log.Start()155    test_log.WaitFor(r'.*last line.*', None)156    test_log.Stop()157    actual_results = test_log.FindAll(158        r'\S* logcat monitor test message \d', thread_id=2109)159    expected_results = [('9012', '2109', 'I', 'LogcatMonitorTest',160                         'info logcat monitor test message 3')]161    self.assertIterEqual(iter(expected_results), actual_results)162    test_log.Close()163  @mock.patch('time.sleep', mock.Mock())164  def testFindAll_filterLogLevel(self):165    test_log = _CreateTestLog(166        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)167    test_log.Start()168    test_log.WaitFor(r'.*last line.*', None)169    test_log.Stop()170    actual_results = test_log.FindAll(171        r'\S* logcat monitor test message \d', log_level=r'[DW]')172    expected_results = [('8901', '1098', 'D', 'LogcatMonitorTest',173                         'debug logcat monitor test message 2'),174                        ('0123', '3210', 'W', 'LogcatMonitorTest',175                         'warning logcat monitor test message 4')]176    self.assertIterEqual(iter(expected_results), actual_results)177    test_log.Close()178  @mock.patch('time.sleep', mock.Mock())179  def testFindAll_filterComponent(self):180    test_log = _CreateTestLog(181        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)182    test_log.Start()183    test_log.WaitFor(r'.*last line.*', None)184    test_log.Stop()185    actual_results = test_log.FindAll(r'.*', component='LogcatMonitorTest')186    expected_results = [('7890', '0987', 'V', 'LogcatMonitorTest',187                         'verbose logcat monitor test message 1'),188                        ('8901', '1098', 'D', 'LogcatMonitorTest',189                         'debug logcat monitor test message 2'),190                        ('9012', '2109', 'I', 'LogcatMonitorTest',191                         'info logcat monitor test message 3'),192                        ('0123', '3210', 'W', 'LogcatMonitorTest',193                         'warning logcat monitor test message 4'),194                        ('1234', '4321', 'E', 'LogcatMonitorTest',195                         'error logcat monitor test message 5'),196                        ('2345', '5432', 'F', 'LogcatMonitorTest',197                         'fatal logcat monitor test message 6'),198                        ('3456', '6543', 'D', 'LogcatMonitorTest', 'last line')]199    self.assertIterEqual(iter(expected_results), actual_results)200    test_log.Close()201if __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!!
