Best Python code snippet using uiautomator
test_app.py
Source:test_app.py  
...97class TestAPIEdge(XXXTestBase):98    def assert_edge_clean(self, edge):99        assert_equal(edge._call_semaphore.counter,100                     edge.max_concurrent_calls)101    def test_normal_call(self):102        edge = APIEdge(MockApp(), self.get_settings())103        api = edge.app.api104        call = Call("foo")105        assert_equal(edge.get_call_handler(call), (api, "foo"))106        assert_equal(edge.get_call_handler_method(call, api, "foo"), api.foo)107        edge.execute(call)108        assert_equal(api.foo.call_count, 1)109        self.assert_edge_clean(edge)110    def test_debug_call(self):111        edge = APIEdge(MockApp(), self.get_settings())112        debug_api = edge.app.debug_api113        call = Call("debug.foo")114        edge.execute(call)115        assert_equal(debug_api.foo.call_count, 1)116        assert_equal(edge._call_semaphore, None)117    def test_timeout(self):118        edge = APIEdge(MockApp(), self.get_settings())119        edge.call_timeout = 0.0120        edge.app.api.foo = lambda: gevent.sleep(0.1)121        try:122            edge.execute(Call("foo"))123            raise AssertionError("timeout not raised!")124        except gevent.Timeout:125            pass126        self.assert_edge_clean(edge)127    def test_no_timeout_decorator(self):128        app = MockApp()129        edge = APIEdge(app, self.get_settings())130        app.api.foo = edge.no_timeout(Mock())131        edge.execute(Call("foo"))132        assert_equal(app.api.foo.call_count, 1)133        self.assert_edge_clean(edge)134    def test_semaphore(self):135        edge = APIEdge(MockApp(), self.get_settings())136        api = edge.app.api137        edge.max_concurrent_calls = 1138        in_first_method = Event()139        finish_first_method = Event()140        def first_method():141            in_first_method.set()142            finish_first_method.wait()143        api.first_method = first_method144        in_second_method = Event()145        def second_method():146            in_second_method.set()147        api.second_method = second_method148        gevent.spawn(edge.execute, Call("first_method"))149        in_first_method.wait()150        gevent.spawn(edge.execute, Call("second_method"))151        gevent.sleep(0)152        assert_logged("too many concurrent callers")153        assert not in_second_method.is_set()154        finish_first_method.set()155        in_second_method.wait()156        self.assert_edge_clean(edge)157class TestDebugAPI(XXXTestBase):158    def test_normal_call(self):159        app = DirtApp("test_normal_call", self.get_settings(), [])160        edge = APIEdge(app, app.settings)161        call = Call("debug.status", (), {}, {})162        result = edge.execute(call)163        assert_contains(result, "uptime")164    def test_error_call(self):165        app = DirtApp("test_normal_call", self.get_settings(), [])166        edge = APIEdge(app, app.settings)167        call = Call("debug.ping", (), {"raise_error": True}, {})168        try:169            edge.execute(call)170            raise AssertionError("exception not raised")171        except Exception as e:172            if not str(e).startswith("pong:"):...test.py
Source:test.py  
...17        self.assertEqual(expected, actual)18@patch("os.chdir")19@patch("os.system")20class TestAddFile(unittest.TestCase):21    def test_normal_call(self, system_mock, chdir_mock):22        expected_directory = "/home/someuser/somedir"23        expected_cmd = "tar -r --exclude='path' --file=arch f1"24        backup.addFile(expected_directory, "arch", "f1", "--exclude='path'")25        system_mock.assert_called_once_with(expected_cmd)26        chdir_mock.assert_called_once_with(expected_directory)27    def test_no_exclude(self, system_mock, chdir_mock):28        expected_directory = "/home/someuser/somedir"29        expected_cmd = "tar -r --file=arch f1"30        backup.addFile(expected_directory, "arch", "f1", "")31        system_mock.assert_called_once_with(expected_cmd)32        chdir_mock.assert_called_once_with(expected_directory)33@patch("os.path.exists")34@patch("backup.addFile")35@patch("backup.getFilesToCompress")36class TestCompress(unittest.TestCase):37    def test_normal_call(self, getFilesToCompress_mock, addFile_mock, path_exists_mock):38        bu_files = "bu_files"39        target_file_name = "target"40        to_compress = [("/somedir", "comp")]41        to_exclude = ["excl"]42        getFilesToCompress_mock.return_value = [to_compress, to_exclude]43        to_exclude_str = backup.makeExcludeDir(to_exclude)44        path_exists_mock.return_value = True45        backup.Compress(bu_files, target_file_name)46        addFile_mock.assert_called_once_with(47            to_compress[0][0], target_file_name, to_compress[0][1], to_exclude_str48        )49@patch("os.getcwd")50@patch("datetime.date")51class TestGetDefaultFileName(unittest.TestCase):52    def test_normal_call(self, date_mock, getcwd_mock):53        dir = "/some/dir"54        day = datetime.date(1991, 3, 26)55        date_str = str(day.year) + "_" + str(day.month) + "_" + str(day.day)56        date_mock.today.return_value = day57        getcwd_mock.return_value = dir58        expected = "{}/backup_{}.tar".format(dir, date_str)59        actual = backup.getDefaultFileName()60        self.assertEqual(expected, actual)61if __name__ == "__main__":...rack.py
Source:rack.py  
...11        h1 = Class5Server("hostname1.lindenlab.com")12        h2 = Class5Server("hostname2.lindenlab.com")13        rack.insert(h1, 1)14        rack.insert(h2, 2)15    def test_normal_call(self):16        response = self.do_api_call("c2-02-00")17        self.assert_response_code(response, 200)18        self.assertEqual(response.data, [{'serial_number': None, 19                                          'hostname': 'hostname1.lindenlab.com', 20                                          'type': 'server'}, 21                                         {'serial_number': None, 22                                          'hostname': 'hostname2.lindenlab.com', 23                                          'type': 'server'}])24    def test_bad_call(self):25        response = self.do_api_call("huh?")26        self.assert_response_code(response, 404)27        28        response = self.do_api_call("huh?", 2)29        self.assert_response_code(response, 400)30class TestGetServerHostnamesInRack(JinxTestCase):31    api_call_path = "/jinx/2.0/get_server_hostnames_in_rack"32    33    def data(self):34        # Populate Clusto                                                                                                                                            35        ServerClass("Class 5")36        h1 = Class5Server("hostname1.lindenlab.com")37        h2 = Class5Server("hostname2.lindenlab.com")38        rack1 = LindenRack("c3-03-100")39        rack2 = LindenRack("c3-03-200")40        ServerClass("Class 7")41        chassis1 = Class7Chassis()42        chassis2 = Class7Chassis()43        class7_1 = Class7Server("hostname3.lindenlab.com")44        class7_2 = Class7Server("hostname4.lindenlab.com")45        46        chassis1.insert(class7_1)47        chassis2.insert(class7_2)48        rack2.insert(h1, 1)49        rack1.insert(chassis1, [1, 2])50        rack1.insert(chassis2, [3, 4])51        rack1.insert(h2, 5)52    def test_normal_call(self):53        response = self.do_api_call("c3-03-100")54        self.assert_response_code(response, 200)55        self.assertEqual(sorted(response.data), ['hostname2.lindenlab.com', 'hostname3.lindenlab.com', 'hostname4.lindenlab.com'])56        response = self.do_api_call("c3-03-200")57        self.assert_response_code(response, 200)...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!!
