Best Python code snippet using Testify_python
test_utlcli.py
Source:test_utlcli.py  
...33        self.assertEqual(result.exit_code, 0)34    def test_utl_cli_pwd_env(self):35        opts = '-U foo -w'36        runner = CliRunner(env={'USER': 'foo', 'PASSWORD': 'yada'})37        def assert_call(ctx):38            self.assertIsNotNone(ctx.obj)39            self.assertIsInstance(ctx.obj, ExecContext)40            self.assertIn('username', ctx.obj)41            self.assertIn('password', ctx.obj)42        with self.set_assert_case(assert_call):43            result = runner.invoke(cmd_sink, opts)44        self.assertEqual(0, result.exit_code)45    def test_utl_cli_pwd_input(self):46        opts = '-U foo'47        runner = CliRunner()48        def assert_call(ctx):49            self.assertIsNotNone(ctx.obj)50            self.assertIsInstance(ctx.obj, ExecContext)51            self.assertIn('username', ctx.obj)52            self.assertIn('password', ctx.obj)53        with self.set_assert_case(assert_call):54            result = runner.invoke(cmd_sink, opts, input='yada')55        self.assertEqual(0, result.exit_code)56    def test_no_password(self):57        """Testing --no-password option without58        any valid password source (env/file)"""59        runner = CliRunner()60        result = runner.invoke(cmd_sink, args='-U foo -w')61        self.assertEqual(result.exit_code, 2)62        self.assertIn('no password file could be found',63                      result.output.lower())64    def test_no_password_env_provided(self):65        """Testing --no-password option with an ENV defined password"""66        runner = CliRunner()67        def assert_call(ctx):68            self.assertEqual(ctx.password, 'baz')69            self.assertEqual(ctx.username, 'foo')70        result = runner.invoke(cmd_sink, args='-U foo -w',71                               env={'USER': 'foo',72                                    'PASSWORD': 'baz'})73        self.assertEqual(result.exit_code, 0)74        self.assertEqual(result.output.lower(), '')75    def test_no_password_file_provided(self):76        """Testing --no-password option with a file defined password"""77        runner = CliRunner()78        def assert_call(ctx):79            self.assertEqual(ctx.password, 'baz')80            self.assertEqual(ctx.username, 'foo')81        with runner.isolated_filesystem():82            with open('.passwords', 'w') as f:83                f.write('foo:baz')84            result = runner.invoke(cmd_sink, args='-U foo -w',85                                   env={'USER': 'foo',86                                        'IDPASSFILE': '.passwords'})87        self.assertEqual(result.exit_code, 0)88        self.assertEqual(result.output.lower(), '')89    def test_input_password(self):90        runner = CliRunner()91        def assert_call(ctx):92            self.assertEqual(ctx.password, 'baz')93            self.assertEqual(ctx.username, 'foo')94        with self.subTest('Test with user provided through --username'):95            result = runner.invoke(cmd_sink, args='-U foo', input='baz')96            self.assertEqual(result.exit_code, 0)97            self.assertIn('password for', result.output.lower())98        with self.subTest('Test with user provided through ENV'):99            result = runner.invoke(cmd_sink, args='',100                                   input='baz', env={'USER': 'foo'})101            self.assertEqual(result.exit_code, 0)102            self.assertIn('password for', result.output.lower())103        with self.subTest('Test precedence of --username of ENV.user'):104            result = runner.invoke(cmd_sink, args='-U foo', input='baz',105                                   env={'USER': 'spam'})...test_tango_device_component_manager.py
Source:test_tango_device_component_manager.py  
...49        component_state_callback=comm_call_group["comp_state"],50    )51    assert tc_manager.state == "disconnected"52    tc_manager.start_communicating()53    comm_call_group.assert_call(54        "comm_state", CommunicationStatus.NOT_ESTABLISHED55    )56    comm_call_group.assert_call(57        "comp_state", connection_state="setting_up_device_proxy"58    )59    comm_call_group.assert_call(60        "comp_state", connection_state="setting_up_monitoring"61    )62    comm_call_group.assert_call("comp_state", connection_state="monitoring")63    comm_call_group.assert_call("comm_state", CommunicationStatus.ESTABLISHED)64    tc_manager.abort_tasks()65@pytest.mark.unit66@mock.patch("ska_mid_dish_manager.component_managers.tango_device_cm.tango")67def test_unhappy_path(patched_tango, caplog):68    """Tango device is unreachable and can't communicate with component manager69    Similar to `test_component_manager_continues_reconnecting_...` except70    Tango layer is mocked here. Checks are made on the mock for expected71    calls and logs when communication is attempted by component manager72    on mocked device73    """74    # pylint: disable=no-member75    patched_tango.DevFailed = tango.DevFailed76    caplog.set_level(logging.DEBUG)77    log_call_group = MockCallableGroup("log_message", timeout=5)78    # Set up mocks79    mock_device_proxy = mock.MagicMock(name="DP")80    mock_device_proxy.ping.side_effect = tango.DevFailed("FAIL")81    patched_tango.DeviceProxy.return_value = mock_device_proxy82    tc_manager = TangoDeviceComponentManager(83        "a/b/c",84        LOGGER,85        communication_state_callback=None,86        component_state_callback=None,87    )88    with mock.patch.object(89        tc_manager.logger, "info", log_call_group["log_message"]90    ):91        assert tc_manager.state == "disconnected"  # pylint: disable=no-member92        tc_manager.start_communicating()93        log_call_group.assert_call(94            "log_message",95            "Connection retry count [%s] for device [%s]",96            4,97            "a/b/c",98            lookahead=10,99        )100        assert tc_manager.state == "setting_up_device_proxy"101        tc_manager.abort_tasks()102@pytest.mark.unit103@mock.patch("ska_mid_dish_manager.component_managers.tango_device_cm.tango")104def test_device_goes_away(patched_tango, caplog):105    """Start up the component_manager.106    Signal a lost connection via an event107    Check for reconnect108    """109    caplog.set_level(logging.DEBUG)110    call_group = MockCallableGroup("comm_state", "comp_state", timeout=5)111    # Set up mocks112    patched_dp = mock.MagicMock()113    patched_dp.command_inout = mock.MagicMock()114    patched_tango.DeviceProxy = mock.MagicMock(return_value=patched_dp)115    tc_manager = TangoDeviceComponentManager(116        "a/b/c",117        LOGGER,118        communication_state_callback=call_group["comm_state"],119        component_state_callback=call_group["comp_state"],120    )121    assert tc_manager.state == "disconnected"  # pylint:disable=no-member122    tc_manager.start_communicating()123    call_group.assert_call("comm_state", CommunicationStatus.NOT_ESTABLISHED)124    call_group.assert_call(125        "comp_state", connection_state="setting_up_device_proxy"126    )127    call_group.assert_call(128        "comp_state", connection_state="setting_up_monitoring"129    )130    call_group.assert_call("comp_state", connection_state="monitoring")131    call_group.assert_call("comm_state", CommunicationStatus.ESTABLISHED)132    # Set up mock error event133    mock_error = mock.MagicMock()134    mock_error.err = True135    # Trigger the failure136    tc_manager._events_queue.put(mock_error)137    # Make sure we lost connection138    call_group.assert_call("comm_state", CommunicationStatus.NOT_ESTABLISHED)139    # Make sure we get it back140    call_group.assert_call("comp_state", connection_state="reconnecting")141    call_group.assert_call(142        "comp_state", connection_state="setting_up_device_proxy"143    )144    call_group.assert_call(145        "comp_state", connection_state="setting_up_monitoring"146    )147    call_group.assert_call("comp_state", connection_state="monitoring")148    call_group.assert_call("comm_state", CommunicationStatus.ESTABLISHED)...test_configurable.py
Source:test_configurable.py  
...56class TestConfigurable(unittest.TestCase):7    def test_fct_generic(self):8        @dct_arg9        def assert_call(*args, e1, e2=2, **kwargs):10            self.assertEqual(0, args[0])11            self.assertEqual(1, e1)12            self.assertEqual(2, e2)13            self.assertEqual(3, kwargs['e3'])1415        assert_call({}, 0, e1=1, e2=2, e3=3)16        assert_call({'e1': 1, 'e2': 2, 'e3': 3}, 0, e3=3)17        assert_call({'e1': 1}, 0, e3=3)18        assert_call({'e1': 1, 'e3': 0}, 0, e3=3)1920    def test_fct_no_pos(self):21        @dct_arg22        def assert_call(e0, e1=1, *, e2=2, e3):23            self.assertEqual(0, e0)24            self.assertEqual(1, e1)25            self.assertEqual(2, e2)26            self.assertEqual(3, e3)2728        assert_call({}, 0, 1, e2=2, e3=3)29        assert_call({}, 0, 1, e3=3)30        assert_call({'e1': 1, 'e0': 0, 'e3': 3})31        assert_call({'e1': 1, 'e0': 2, 'e3': 3}, e0=0)32        self.assertRaises(TypeError, assert_call, {}, 0, e0=0)3334    def test_replace_default(self):35        @dct_arg36        def assert_call(e0=3):37            self.assertEqual(0, e0)3839        assert_call({}, 0)40        assert_call({}, e0=0)41        assert_call({'e0': 1}, e0=0)42        assert_call({'e0': 0})4344    def test_path(self):45        @dct_arg(path='child/subchild')46        def assert_call(e0, e1):47            self.assertEqual(0, e0)48            self.assertEqual(1, e1)4950        assert_call({'child': {'subchild': {'e1': 1, 'e0': 0}}})51        assert_call({'child': {'subchild': {'e1': 1}}}, e0=0)52        assert_call(e0=0, e1=1)5354    def tests_fetch_conf(self):55        @dct_arg(fetch_args={'config': ""}, name='configuration')56        def assert_call(config, e0):57            self.assertEqual({'e0': 0}, config)58            self.assertEqual(0, e0)5960        assert_call(configuration={'e0': 0})6162    def test_double_conf(self):63        @dct_arg(name='conf_0')64        @dct_arg(name='conf_1')65        def assert_call(e_0, e_1):66            self.assertEqual(0, e_0)67            self.assertEqual(1, e_1)6869        assert_call(conf_1={'e_1': 1}, conf_0={'e_0': 0})70        assert_call(conf_1={'e_1': 1}, e_0=0)71        assert_call(e_1=1, e_0=0)727374if __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!!
