다음 파이썬 코드로 작업하고 있습니다. 코드는 매우 간단하며 pexpect는 터미널에 echo testcase
을 전송 한 다음 터미널에서 후속 모양 인 testcase
을 모니터링 한 다음 bool_value=True
(기대되는 출력이 있다고 가정)을 설정하여 에코를 보았는지 확인합니다. 내가 볼 것으로 예상 무엇Python pexpect : 'string'을 찾을 수 없을 때 var.expect ('string')가 예상 값을 반환하지 않습니다.
#!/usr/bin/python
import pexpect
print('Testing case where pexpect finds the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = not testcase.expect('testcase')
print bool_value
testcase.close()
print('Testing case where pexpect does not find the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = not testcase.expect('someothervalue')
print bool_value
testcase.close()
는 첫 번째 테스트 케이스 후, bool_value가 True
를 인쇄 할 것입니다. 이 경우에는 수행하지만, 단지 bool_value = not testcase.expect('testcase')
을 설정하는 홀수 해킹을 추가 한 후에야 (expect()가 반환하는 것과 반대의 값으로 설정).
두 번째 테스트 케이스에서 은 someothervalue
의 예상 값을 볼 수 없으므로 false
을 반환 할 것으로 예상됩니다. 대신 예외를 반환합니다.
Traceback (most recent call last):
File "./echotest.py", line 12, in <module>
bool_value = not testcase.expect('someothervalue')
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1521, in expect_loop
raise EOF(str(err) + '\n' + str(self))
pexpect.EOF: End Of File (EOF). Exception style platform.
<pexpect.spawn object at 0x7f7667a9db10>
version: 3.1
command: /bin/echo
args: ['/bin/echo', 'testcase']
searcher: <pexpect.searcher_re object at 0x7f7667a9d790>
buffer (last 100 chars): ''
before (last 100 chars): 'testcase\r\n'
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 0
flag_eof: True
pid: 4619
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
설명서가 무엇을 말하는지에 따라이 동작을 수정하는 방법을 잘 모르겠습니다. this previous implementation에 testcase.expect(pexpect.EOF())
을 추가하면 같은 오류가 반환됩니다.
이 기능을 올바르게 구현하려면 어떻게해야합니까?
아마도 'echo'가 기본적으로 개행 문자를 추가했기 때문일 수 있습니까? 'echo -n'을 사용하거나'expect ('testcase \ n')'를 사용하십시오. – Bakuriu
나는'echo -n'에 제안 된 변경을했다. throw 된 예외의 유일한 변경 사항은'before (last 100 chars) : 'testcase \ r \ n'''가'before (last 100chars) :'testcase''가 된 것입니다. 예외는'expect ('testcase \ n')'또는'expect ('testcase \ r \ n')'또는 새로운'expect ('\ r \ n')'행을 코드에 추가함으로써 변경되지 않습니다. –