I am working with the following bit of Python code. The code is fairly simple, pexpect sends echo testcase
to the terminal, then monitors the terminal for the subsequent appearance of testcase
, then verifies that it saw the echo by setting bool_value=True
(assuming it sees the expected output).
#!/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()
What I expect to see is that after the first test case, bool_value would print True
. In this case it does, but only after adding the odd hack of setting bool_value = not testcase.expect('testcase')
(setting it to the inverse of what expect() returns).
In the second test case, testcase.expect()
does not see the expected value of someothervalue
, so I would expect it to return false
. Instead, it returns an exception:
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
I'm really not sure how to correct this behavior based on what the documentation is telling me. Adding testcase.expect(pexpect.EOF())
per this previous implementation returns the same error.
How do I correctly implement this function?