我放棄了試圖使用java工具進行測試,並使用python pexpect庫去執行控制檯應用程序。這些測試整合到Maven構建,但所需的* nix主機來運行它們:
import unittest
import pexpect
import os
import signal
import subprocess
import urllib
import urllib2
import json
from wiremock import WiremockClient
class TestInteractive(unittest.TestCase):
cli_cmd = "java -jar " + os.environ["CLI_JAR"]
# ... code omitted for brevity
def test_interactive_mode_username_and_password_sent_to_server(self):
child = pexpect.spawn(TestInteractive.cli_cmd, timeout=10)
child.expect ('Username: ')
child.sendline ('1234')
child.expect ('Password: ')
child.sendline ('abcd')
child.expect ('Successfully authenticated')
child.expect ('stratos> ')
child.sendline ('exit')
child.expect (pexpect.EOF)
# CLI sends GET request to mock server url /stratos/admin/coookie
self.assertEqual(self.wiremock.get_cookie_auth_header(), "1234:abcd")
# ... code omitted for brevity
if __name__ == '__main__':
try:
unittest.main()
# handle CTRL-C
except KeyboardInterrupt:
# shut down wiremock
TestInteractive.wiremock.stop()
exit(1)
完整的CLI測試套件,我工作的項目可以發現here。
我有同樣的問題。陛下你取得了一些進展? – Alex
我已經添加了一個答案,顯示我所用的解決方案。 –