Skip to content

Instantly share code, notes, and snippets.

@jbking
Created March 9, 2011 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbking/862204 to your computer and use it in GitHub Desktop.
Save jbking/862204 to your computer and use it in GitHub Desktop.
wait external process started before testing and kill before exit in teardown in testing cycle.
import os
import time
import socket
import xmlrpclib
import subprocess
here = os.path.dirname(os.path.abspath(__file__))
target = os.path.join(here, 'server.py')
gateway = None
def setup():
global gateway
gateway = subprocess.Popen([target])
while True:
try:
sock = socket.create_connection(('localhost', 9000))
sock.close()
break
except:
time.sleep(0.1)
def test_add():
proxy = xmlrpclib.ServerProxy('http://localhost:9000')
assert proxy.add(5, 7) == 12
def teardown():
global gateway
gateway.terminate()
gateway.wait()
if __name__ == '__main__':
setup()
try:
test_add()
except:
pass
teardown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment