本文整理汇总了Python中testfixtures.popen.MockPopen.set_default方法的典型用法代码示例。如果您正苦于以下问题:Python MockPopen.set_default方法的具体用法?Python MockPopen.set_default怎么用?Python MockPopen.set_default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类testfixtures.popen.MockPopen
的用法示例。
在下文中一共展示了MockPopen.set_default方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_callable_default_behaviour
# 需要导入模块: from testfixtures.popen import MockPopen [as 别名]
# 或者: from testfixtures.popen.MockPopen import set_default [as 别名]
def test_callable_default_behaviour(self):
def some_callable(command, stdin):
return PopenBehaviour(BytesLiteral(command), BytesLiteral(stdin), 1, 345, 0)
Popen = MockPopen()
Popen.set_default(behaviour=some_callable)
process = Popen('a command', stdin='some stdin', stdout=PIPE, stderr=PIPE)
compare(process.pid, 345)
out, err = process.communicate()
compare(out, b'a command')
compare(err, b'some stdin')
compare(process.returncode, 1)
示例2: test_default_command_max_args
# 需要导入模块: from testfixtures.popen import MockPopen [as 别名]
# 或者: from testfixtures.popen.MockPopen import set_default [as 别名]
def test_default_command_max_args(self):
Popen = MockPopen()
Popen.set_default(b"out", b"err", 1, 345)
process = Popen("a command", stdout=PIPE, stderr=PIPE)
compare(process.pid, 345)
compare(None, process.returncode)
out, err = process.communicate()
# test the rest
compare(out, b"out")
compare(err, b"err")
compare(process.returncode, 1)
# test call list
compare(
[call.Popen("a command", stderr=-1, stdout=-1), call.Popen_instance.communicate()], Popen.mock.method_calls
)
示例3: test_default_command_min_args
# 需要导入模块: from testfixtures.popen import MockPopen [as 别名]
# 或者: from testfixtures.popen.MockPopen import set_default [as 别名]
def test_default_command_min_args(self):
# setup
Popen = MockPopen()
Popen.set_default()
# usage
process = Popen("a command", stdout=PIPE, stderr=PIPE)
# process started, no return code
compare(process.pid, 1234)
compare(None, process.returncode)
out, err = process.communicate()
# test the rest
compare(out, b"")
compare(err, b"")
compare(process.returncode, 0)
# test call list
compare(
[call.Popen("a command", stderr=-1, stdout=-1), call.Popen_instance.communicate()], Popen.mock.method_calls
)
示例4: TestMyFunc
# 需要导入模块: from testfixtures.popen import MockPopen [as 别名]
# 或者: from testfixtures.popen.MockPopen import set_default [as 别名]
#.........这里部分代码省略.........
compare(my_func(), b"o")
# testing calls were in the right order and with the correct parameters:
compare(
[call.Popen("svn ls -R foo", shell=True, stderr=PIPE, stdout=PIPE), call.Popen_instance.communicate()],
Popen.mock.method_calls,
)
def test_example_bad_returncode(self):
# set up
Popen.set_command("svn ls -R foo", stdout=b"o", stderr=b"e", returncode=1)
# testing of error
with ShouldRaise(RuntimeError("something bad happened")):
my_func()
def test_communicate_with_input(self):
# setup
Popen = MockPopen()
Popen.set_command("a command")
# usage
process = Popen("a command", stdout=PIPE, stderr=PIPE, shell=True)
out, err = process.communicate("foo")
# test call list
compare(
[call.Popen("a command", shell=True, stderr=-1, stdout=-1), call.Popen_instance.communicate("foo")],
Popen.mock.method_calls,
)
def test_read_from_stdout_and_stderr(self):
# setup
Popen = MockPopen()
Popen.set_command("a command", stdout=b"foo", stderr=b"bar")
# usage
process = Popen("a command", stdout=PIPE, stderr=PIPE, shell=True)
compare(process.stdout.read(), b"foo")
compare(process.stderr.read(), b"bar")
# test call list
compare([call.Popen("a command", shell=True, stderr=PIPE, stdout=PIPE)], Popen.mock.method_calls)
def test_wait_and_return_code(self):
# setup
Popen = MockPopen()
Popen.set_command("a command", returncode=3)
# usage
process = Popen("a command")
compare(process.returncode, None)
# result checking
compare(process.wait(), 3)
compare(process.returncode, 3)
# test call list
compare([call.Popen("a command"), call.Popen_instance.wait()], Popen.mock.method_calls)
def test_send_signal(self):
# setup
Popen = MockPopen()
Popen.set_command("a command")
# usage
process = Popen("a command", stdout=PIPE, stderr=PIPE, shell=True)
process.send_signal(0)
# result checking
compare(
[call.Popen("a command", shell=True, stderr=-1, stdout=-1), call.Popen_instance.send_signal(0)],
Popen.mock.method_calls,
)
def test_poll_until_result(self):
# setup
Popen = MockPopen()
Popen.set_command("a command", returncode=3, poll_count=2)
# example usage
process = Popen("a command")
while process.poll() is None:
# you'd probably have a sleep here, or go off and
# do some other work.
pass
# result checking
compare(process.returncode, 3)
compare(
[
call.Popen("a command"),
call.Popen_instance.poll(),
call.Popen_instance.poll(),
call.Popen_instance.poll(),
],
Popen.mock.method_calls,
)
def test_default_behaviour(self):
# set up
self.Popen.set_default(stdout=b"o", stderr=b"e")
# testing of results
compare(my_func(), b"o")
# testing calls were in the right order and with the correct parameters:
compare(
[call.Popen("svn ls -R foo", shell=True, stderr=PIPE, stdout=PIPE), call.Popen_instance.communicate()],
Popen.mock.method_calls,
)
示例5: TestMyFunc
# 需要导入模块: from testfixtures.popen import MockPopen [as 别名]
# 或者: from testfixtures.popen.MockPopen import set_default [as 别名]
#.........这里部分代码省略.........
# usage
process = Popen('a command')
compare(process.returncode, None)
# result checking
compare(process.wait(), 3)
compare(process.returncode, 3)
# test call list
compare([
call.Popen('a command'),
call.Popen_instance.wait(),
], Popen.mock.method_calls)
def test_send_signal(self):
# setup
Popen = MockPopen()
Popen.set_command('a command')
# usage
process = Popen('a command', stdout=PIPE, stderr=PIPE, shell=True)
process.send_signal(0)
# result checking
compare([
call.Popen('a command', shell=True, stderr=-1, stdout=-1),
call.Popen_instance.send_signal(0),
], Popen.mock.method_calls)
def test_poll_until_result(self):
# setup
Popen = MockPopen()
Popen.set_command('a command', returncode=3, poll_count=2)
# example usage
process = Popen('a command')
while process.poll() is None:
# you'd probably have a sleep here, or go off and
# do some other work.
pass
# result checking
compare(process.returncode, 3)
compare([
call.Popen('a command'),
call.Popen_instance.poll(),
call.Popen_instance.poll(),
call.Popen_instance.poll(),
], Popen.mock.method_calls)
def test_default_behaviour(self):
# set up
self.Popen.set_default(stdout=b'o', stderr=b'e')
# testing of results
compare(my_func(), b'o')
# testing calls were in the right order and with the correct parameters:
compare([
call.Popen('svn ls -R foo',
shell=True, stderr=PIPE, stdout=PIPE),
call.Popen_instance.communicate()
], Popen.mock.method_calls)
def test_callable(self):
# set up
def command_callable(command, stdin):
return PopenBehaviour(stdout=b'stdout')
self.Popen.set_default(behaviour=command_callable)
# testing of results
compare(my_func(), b'stdout')
# testing calls were in the right order and with the correct parameters:
compare([
call.Popen('svn ls -R foo',
shell=True, stderr=PIPE, stdout=PIPE),
call.Popen_instance.communicate()
], Popen.mock.method_calls)
def test_multiple_responses(self):
# set up
behaviours = [
PopenBehaviour(stderr=b'e', returncode=1),
PopenBehaviour(stdout=b'o'),
]
def behaviour(command, stdin):
return behaviours.pop(0)
self.Popen.set_command('svn ls -R foo', behaviour=behaviour)
# testing of error:
with ShouldRaise(RuntimeError('something bad happened')):
my_func()
# testing of second call:
compare(my_func(), b'o')
def test_count_down(self):
# set up
self.Popen.set_command('svn ls -R foo', behaviour=CustomBehaviour())
# testing of error:
with ShouldRaise(RuntimeError('something bad happened')):
my_func()
# testing of second call:
compare(my_func(), b'o')