Best Python code snippet using avocado_python
test_utils_cpu.py
Source:test_utils_cpu.py  
...9        file_mock.__enter__ = unittest.mock.Mock(10            return_value=io.BytesIO(content))11        file_mock.__exit__ = unittest.mock.Mock()12        return file_mock13    def _get_data_mock(self, data_name):14        with open(self.get_data(data_name), 'rb') as data_file:15            content = data_file.read()16        return self._get_file_mock(content)17    def test_s390x_cpu_online(self):18        with unittest.mock.patch('avocado.utils.cpu.platform.machine',19                                 return_value='s390x'):20            with unittest.mock.patch(21                    'builtins.open',22                    return_value=self._get_data_mock('s390x')):23                self.assertEqual(len(cpu.online_list()), 2)24            with unittest.mock.patch(25                    'builtins.open',26                    return_value=self._get_data_mock('s390x_2')):27                self.assertEqual(len(cpu.online_list()), 4)28    def test_x86_64_cpu_online(self):29        with unittest.mock.patch('avocado.utils.cpu.platform.machine',30                                 return_value='x86_64'):31            with unittest.mock.patch('builtins.open',32                                     return_value=self._get_data_mock('x86_64')):33                self.assertEqual(len(cpu.online_list()), 8)34    def test_cpu_arch_i386(self):35        with unittest.mock.patch('builtins.open',36                                 return_value=self._get_data_mock('i386')):37            self.assertEqual(cpu.get_arch(), "i386")38    def test_cpu_arch_x86_64(self):39        with unittest.mock.patch('builtins.open',40                                 return_value=self._get_data_mock('x86_64')):41            self.assertEqual(cpu.get_arch(), "x86_64")42    def test_cpu_has_flags(self):43        with unittest.mock.patch('builtins.open',44                                 return_value=self._get_data_mock('x86_64')):45            self.assertTrue(cpu.cpu_has_flags('flexpriority'))46            self.assertTrue(cpu.cpu_has_flags(['sse4_2', 'xsaveopt']))47            self.assertFalse(cpu.cpu_has_flags('THIS_WILL_NEVER_BE_A_FLAG_NAME'))48            self.assertFalse(cpu.cpu_has_flags(['THIS_WILL_NEVER_BE_A_FLAG_NAME',49                                                'NEITHER_WILL_THIS_WILL_EVER_BE']))50    def test_cpu_arch_power8(self):51        with unittest.mock.patch('builtins.open',52                                 return_value=self._get_data_mock('power8')):53            self.assertEqual(cpu.get_arch(), "powerpc")54    def test_cpu_arch_power9(self):55        with unittest.mock.patch('builtins.open',56                                 return_value=self._get_data_mock('power9')):57            self.assertEqual(cpu.get_arch(), "powerpc")58    def test_cpu_arch_s390(self):59        with unittest.mock.patch(60                'builtins.open',61                return_value=self._get_data_mock('s390x')):62            self.assertEqual(cpu.get_arch(), "s390")63    def test_cpu_arch_arm_v7(self):64        with unittest.mock.patch('builtins.open',65                                 return_value=self._get_data_mock('armv7')):66            self.assertEqual(cpu.get_arch(), "arm")67    def test_cpu_arch_arm_v8(self):68        with unittest.mock.patch('builtins.open',69                                 return_value=self._get_data_mock('armv8')):70            self.assertEqual(cpu.get_arch(), "aarch64")71    def test_cpu_arch_risc_v(self):72        with unittest.mock.patch('builtins.open',73                                 return_value=self._get_data_mock('risc_v')):74            self.assertEqual(cpu.get_arch(), "riscv")75    def test_cpu_vendor_intel(self):76        with unittest.mock.patch('builtins.open',77                                 return_value=self._get_data_mock('x86_64')):78            self.assertEqual(cpu.get_vendor(), "intel")79    def test_cpu_vendor_power8(self):80        with unittest.mock.patch('builtins.open',81                                 return_value=self._get_data_mock('power8')):82            self.assertEqual(cpu.get_vendor(), "ibm")83    def test_cpu_vendor_power9(self):84        with unittest.mock.patch('builtins.open',85                                 return_value=self._get_data_mock('power9')):86            self.assertEqual(cpu.get_vendor(), "ibm")87    def test_s390x_get_version(self):88        with unittest.mock.patch('avocado.utils.cpu.platform.machine',89                                 return_value='s390x'):90            with unittest.mock.patch(91                    'builtins.open',92                    return_value=self._get_data_mock('s390x')):93                self.assertEqual(cpu.get_version(), "2827")94    def test_intel_get_version(self):95        with unittest.mock.patch('avocado.utils.cpu.platform.machine',96                                 return_value='x86_64'):97            with unittest.mock.patch('builtins.open',98                                     return_value=self._get_data_mock('x86_64')):99                self.assertEqual(cpu.get_version(), "i7-4710MQ")100    def test_power8_get_version(self):101        with unittest.mock.patch('avocado.utils.cpu.platform.machine',102                                 return_value='powerpc'):103            with unittest.mock.patch('builtins.open',104                                     return_value=self._get_data_mock('power8')):105                self.assertEqual(cpu.get_version(), "2.1")106    def test_power9_get_version(self):107        with unittest.mock.patch('avocado.utils.cpu.platform.machine',108                                 return_value='powerpc'):109            with unittest.mock.patch('builtins.open',110                                     return_value=self._get_data_mock('power9')):111                self.assertEqual(cpu.get_version(), "1.0")112    def test_s390x_get_family(self):113        with unittest.mock.patch('avocado.utils.cpu.get_arch',114                                 return_value='s390'):115            with unittest.mock.patch('avocado.utils.cpu.get_version',116                                     return_value='8561'):117                self.assertEqual(cpu.get_family(), "z15")118    def test_intel_get_family(self):119        with unittest.mock.patch('avocado.utils.cpu.get_arch',120                                 return_value='x86_64'):121            with unittest.mock.patch('avocado.utils.cpu.get_vendor',122                                     return_value='intel'):123                with unittest.mock.patch(124                        'builtins.open',125                        return_value=self._get_file_mock(b'broadwell')):126                    self.assertEqual(cpu.get_family(), "broadwell")127    def test_power8_get_family(self):128        with unittest.mock.patch('avocado.utils.cpu.get_arch',129                                 return_value='powerpc'):130            with unittest.mock.patch('builtins.open',131                                     return_value=self._get_data_mock('power8')):132                self.assertEqual(cpu.get_family(), "power8")133    def test_power9_get_family(self):134        with unittest.mock.patch('avocado.utils.cpu.get_arch', return_value='powerpc'):135            with unittest.mock.patch('builtins.open',136                                     return_value=self._get_data_mock('power9')):137                self.assertEqual(cpu.get_family(), "power9")138    def test_get_idle_state_off(self):139        retval = {0: {0: False}}140        with unittest.mock.patch('avocado.utils.cpu.online_list',141                                 return_value=[0]):142            with unittest.mock.patch('glob.glob',143                                     return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state1']):144                with unittest.mock.patch('builtins.open',145                                         return_value=io.BytesIO(b'0')):146                    self.assertEqual(cpu.get_idle_state(), retval)147    def test_get_idle_state_on(self):148        retval = {0: {0: True}}149        with unittest.mock.patch('avocado.utils.cpu.online_list',150                                 return_value=[0]):...test_cpu.py
Source:test_cpu.py  
...8        file_mock = unittest.mock.Mock()9        file_mock.__enter__ = unittest.mock.Mock(return_value=io.BytesIO(content))10        file_mock.__exit__ = unittest.mock.Mock()11        return file_mock12    def _get_data_mock(self, data_name):13        with open(self.get_data(data_name), "rb") as data_file:14            content = data_file.read()15        return self._get_file_mock(content)16    def test_s390x_cpu_online(self):17        with unittest.mock.patch(18            "avocado.utils.cpu.platform.machine", return_value="s390x"19        ):20            with unittest.mock.patch(21                "builtins.open", return_value=self._get_data_mock("s390x")22            ):23                self.assertEqual(len(cpu.online_list()), 2)24            with unittest.mock.patch(25                "builtins.open", return_value=self._get_data_mock("s390x_2")26            ):27                self.assertEqual(len(cpu.online_list()), 4)28    def test_x86_64_cpu_online(self):29        with unittest.mock.patch(30            "avocado.utils.cpu.platform.machine", return_value="x86_64"31        ):32            with unittest.mock.patch(33                "builtins.open", return_value=self._get_data_mock("x86_64")34            ):35                self.assertEqual(len(cpu.online_list()), 8)36    def test_cpu_arch_i386(self):37        with unittest.mock.patch(38            "builtins.open", return_value=self._get_data_mock("i386")39        ):40            self.assertEqual(cpu.get_arch(), "i386")41    def test_cpu_arch_x86_64(self):42        with unittest.mock.patch(43            "builtins.open", return_value=self._get_data_mock("x86_64")44        ):45            self.assertEqual(cpu.get_arch(), "x86_64")46    def test_cpu_has_flags(self):47        with unittest.mock.patch(48            "builtins.open", return_value=self._get_data_mock("x86_64")49        ):50            self.assertTrue(cpu.cpu_has_flags("flexpriority"))51            self.assertTrue(cpu.cpu_has_flags(["sse4_2", "xsaveopt"]))52            self.assertFalse(cpu.cpu_has_flags("THIS_WILL_NEVER_BE_A_FLAG_NAME"))53            self.assertFalse(54                cpu.cpu_has_flags(55                    ["THIS_WILL_NEVER_BE_A_FLAG_NAME", "NEITHER_WILL_THIS_WILL_EVER_BE"]56                )57            )58    def test_cpu_arch_power8(self):59        with unittest.mock.patch(60            "builtins.open", return_value=self._get_data_mock("power8")61        ):62            self.assertEqual(cpu.get_arch(), "powerpc")63    def test_cpu_arch_power9(self):64        with unittest.mock.patch(65            "builtins.open", return_value=self._get_data_mock("power9")66        ):67            self.assertEqual(cpu.get_arch(), "powerpc")68    def test_cpu_arch_s390(self):69        with unittest.mock.patch(70            "builtins.open", return_value=self._get_data_mock("s390x")71        ):72            self.assertEqual(cpu.get_arch(), "s390")73    def test_cpu_arch_arm_v7(self):74        with unittest.mock.patch(75            "builtins.open", return_value=self._get_data_mock("armv7")76        ):77            self.assertEqual(cpu.get_arch(), "arm")78    def test_cpu_arch_arm_v8(self):79        with unittest.mock.patch(80            "builtins.open", return_value=self._get_data_mock("armv8")81        ):82            self.assertEqual(cpu.get_arch(), "aarch64")83    def test_cpu_arch_risc_v(self):84        with unittest.mock.patch(85            "builtins.open", return_value=self._get_data_mock("risc_v")86        ):87            self.assertEqual(cpu.get_arch(), "riscv")88    def test_cpu_vendor_intel(self):89        with unittest.mock.patch(90            "builtins.open", return_value=self._get_data_mock("x86_64")91        ):92            self.assertEqual(cpu.get_vendor(), "intel")93    def test_cpu_vendor_power8(self):94        with unittest.mock.patch(95            "builtins.open", return_value=self._get_data_mock("power8")96        ):97            self.assertEqual(cpu.get_vendor(), "ibm")98    def test_cpu_vendor_power9(self):99        with unittest.mock.patch(100            "builtins.open", return_value=self._get_data_mock("power9")101        ):102            self.assertEqual(cpu.get_vendor(), "ibm")103    def test_s390x_get_version(self):104        with unittest.mock.patch(105            "avocado.utils.cpu.platform.machine", return_value="s390x"106        ):107            with unittest.mock.patch(108                "builtins.open", return_value=self._get_data_mock("s390x")109            ):110                self.assertEqual(cpu.get_version(), "2827")111    def test_intel_get_version(self):112        with unittest.mock.patch(113            "avocado.utils.cpu.platform.machine", return_value="x86_64"114        ):115            with unittest.mock.patch(116                "builtins.open", return_value=self._get_data_mock("x86_64")117            ):118                self.assertEqual(cpu.get_version(), "i7-4710MQ")119    def test_power8_get_version(self):120        with unittest.mock.patch(121            "avocado.utils.cpu.platform.machine", return_value="powerpc"122        ):123            with unittest.mock.patch(124                "builtins.open", return_value=self._get_data_mock("power8")125            ):126                self.assertEqual(cpu.get_version(), "2.1")127    def test_power9_get_version(self):128        with unittest.mock.patch(129            "avocado.utils.cpu.platform.machine", return_value="powerpc"130        ):131            with unittest.mock.patch(132                "builtins.open", return_value=self._get_data_mock("power9")133            ):134                self.assertEqual(cpu.get_version(), "1.0")135    def test_s390x_get_family(self):136        with unittest.mock.patch("avocado.utils.cpu.get_arch", return_value="s390"):137            with unittest.mock.patch(138                "avocado.utils.cpu.get_version", return_value="8561"139            ):140                self.assertEqual(cpu.get_family(), "z15")141    def test_intel_get_family(self):142        with unittest.mock.patch("avocado.utils.cpu.get_arch", return_value="x86_64"):143            with unittest.mock.patch(144                "avocado.utils.cpu.get_vendor", return_value="intel"145            ):146                with unittest.mock.patch(147                    "builtins.open", return_value=self._get_file_mock(b"broadwell")148                ):149                    self.assertEqual(cpu.get_family(), "broadwell")150    def test_power8_get_family(self):151        with unittest.mock.patch("avocado.utils.cpu.get_arch", return_value="powerpc"):152            with unittest.mock.patch(153                "builtins.open", return_value=self._get_data_mock("power8")154            ):155                self.assertEqual(cpu.get_family(), "power8")156    def test_power9_get_family(self):157        with unittest.mock.patch("avocado.utils.cpu.get_arch", return_value="powerpc"):158            with unittest.mock.patch(159                "builtins.open", return_value=self._get_data_mock("power9")160            ):161                self.assertEqual(cpu.get_family(), "power9")162    def test_get_idle_state_off(self):163        retval = {0: {0: False}}164        with unittest.mock.patch("avocado.utils.cpu.online_list", return_value=[0]):165            with unittest.mock.patch(166                "glob.glob",167                return_value=["/sys/devices/system/cpu/cpu0/cpuidle/state1"],168            ):169                with unittest.mock.patch(170                    "builtins.open", return_value=io.BytesIO(b"0")171                ):172                    self.assertEqual(cpu.get_idle_state(), retval)173    def test_get_idle_state_on(self):...test_get_avg.py
Source:test_get_avg.py  
1import unittest2from unittest.mock import MagicMock, patch3from sesja_samodzielna.zadanie_4.rozwiazanie.get_avg import get_avg4class TestGetAvg(unittest.TestCase):5    def test_for_integer_average(self):6        _get_data_mock = MagicMock(return_value="123")7        with patch('sesja_samodzielna.zadanie_4.rozwiazanie.get_avg._get_data', _get_data_mock):8            how_much = 39            expected = 210            self.assertEqual(get_avg(how_much), expected)11            _get_data_mock.assert_called_once_with(how_much)12    def test_for_non_integer_average(self):13        _get_data_mock = MagicMock(return_value="12")14        with patch('sesja_samodzielna.zadanie_4.rozwiazanie.get_avg._get_data', _get_data_mock):15            how_much = 216            expected = 1.517            self.assertEqual(get_avg(how_much), expected)...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!!
