Best Python code snippet using autotest_python
subcommand_unittest.py
Source:subcommand_unittest.py
...56 cmd = subcommand.subcommand(func, (2, 3), subdir='dir')57 check_attributes(cmd, func, (2, 3), subdir='/foo/dir',58 debug='/foo/dir/debug')59 self.god.check_playback()60 def _setup_fork_start_parent(self):61 self.god.stub_function(subcommand.os, 'fork')62 subcommand.os.fork.expect_call().and_return(1000)63 func = self.god.create_mock_function('func')64 cmd = _create_subcommand(func, [])65 cmd.fork_start()66 return cmd67 def test_fork_start_parent(self):68 cmd = self._setup_fork_start_parent()69 self.assertEquals(cmd.pid, 1000)70 self.god.check_playback()71 def _setup_fork_start_child(self):72 self.god.stub_function(subcommand.os, 'pipe')73 self.god.stub_function(subcommand.os, 'fork')74 self.god.stub_function(subcommand.os, 'close')75 self.god.stub_function(subcommand.os, 'write')76 self.god.stub_function(subcommand.cPickle, 'dumps')77 self.god.stub_function(subcommand.os, '_exit')78 def test_fork_start_child(self):79 self._setup_fork_start_child()80 func = self.god.create_mock_function('func')81 fork_hook = self.god.create_mock_function('fork_hook')82 join_hook = self.god.create_mock_function('join_hook')83 subcommand.subcommand.register_fork_hook(fork_hook)84 subcommand.subcommand.register_join_hook(join_hook)85 cmd = _create_subcommand(func, (1, 2))86 subcommand.os.pipe.expect_call().and_return((10, 20))87 subcommand.os.fork.expect_call().and_return(0)88 subcommand.os.close.expect_call(10)89 fork_hook.expect_call(cmd)90 func.expect_call(1, 2).and_return(True)91 subcommand.cPickle.dumps.expect_call(True,92 subcommand.cPickle.HIGHEST_PROTOCOL).and_return('True')93 subcommand.os.write.expect_call(20, 'True')94 subcommand.os.close.expect_call(20)95 join_hook.expect_call(cmd)96 subcommand.os._exit.expect_call(0)97 cmd.fork_start()98 self.god.check_playback()99 def test_fork_start_child_error(self):100 self._setup_fork_start_child()101 self.god.stub_function(subcommand.logging, 'exception')102 func = self.god.create_mock_function('func')103 cmd = _create_subcommand(func, (1, 2))104 error = Exception('some error')105 subcommand.os.pipe.expect_call().and_return((10, 20))106 subcommand.os.fork.expect_call().and_return(0)107 subcommand.os.close.expect_call(10)108 func.expect_call(1, 2).and_raises(error)109 subcommand.logging.exception.expect_call('function failed')110 subcommand.cPickle.dumps.expect_call(error,111 subcommand.cPickle.HIGHEST_PROTOCOL).and_return('error')112 subcommand.os.write.expect_call(20, 'error')113 subcommand.os.close.expect_call(20)114 subcommand.os._exit.expect_call(1)115 cmd.fork_start()116 self.god.check_playback()117 def _setup_poll(self):118 cmd = self._setup_fork_start_parent()119 self.god.stub_function(subcommand.os, 'waitpid')120 return cmd121 def test_poll_running(self):122 cmd = self._setup_poll()123 (subcommand.os.waitpid.expect_call(1000, subcommand.os.WNOHANG)124 .and_raises(subcommand.os.error('waitpid')))125 self.assertEquals(cmd.poll(), None)126 self.god.check_playback()127 def test_poll_finished_success(self):128 cmd = self._setup_poll()129 (subcommand.os.waitpid.expect_call(1000, subcommand.os.WNOHANG)130 .and_return((1000, 0)))131 self.assertEquals(cmd.poll(), 0)132 self.god.check_playback()133 def test_poll_finished_failure(self):134 cmd = self._setup_poll()135 self.god.stub_function(cmd, '_handle_exitstatus')136 (subcommand.os.waitpid.expect_call(1000, subcommand.os.WNOHANG)137 .and_return((1000, 10)))138 cmd._handle_exitstatus.expect_call(10).and_raises(Exception('fail'))139 self.assertRaises(Exception, cmd.poll)140 self.god.check_playback()141 def test_wait_success(self):142 cmd = self._setup_poll()143 (subcommand.os.waitpid.expect_call(1000, 0)144 .and_return((1000, 0)))145 self.assertEquals(cmd.wait(), 0)146 self.god.check_playback()147 def test_wait_failure(self):148 cmd = self._setup_poll()149 self.god.stub_function(cmd, '_handle_exitstatus')150 (subcommand.os.waitpid.expect_call(1000, 0)151 .and_return((1000, 10)))152 cmd._handle_exitstatus.expect_call(10).and_raises(Exception('fail'))153 self.assertRaises(Exception, cmd.wait)154 self.god.check_playback()155 def _setup_fork_waitfor(self):156 cmd = self._setup_fork_start_parent()157 self.god.stub_function(cmd, 'wait')158 self.god.stub_function(cmd, 'poll')159 self.god.stub_function(subcommand.time, 'time')160 self.god.stub_function(subcommand.time, 'sleep')161 self.god.stub_function(subcommand.utils, 'nuke_pid')162 return cmd163 def test_fork_waitfor_no_timeout(self):164 cmd = self._setup_fork_waitfor()165 cmd.wait.expect_call().and_return(0)166 self.assertEquals(cmd.fork_waitfor(), 0)167 self.god.check_playback()168 def test_fork_waitfor_success(self):169 cmd = self._setup_fork_waitfor()170 self.god.stub_function(cmd, 'wait')...
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!!