summaryrefslogtreecommitdiff
path: root/httpcache/conftest.py
blob: 2fe30f8adb5d9b21e09ddcff0b1ca905760c81d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import py.test
import subprocess
import requests
import time


class TestServer(object):

    def start(self):
        base = os.path.abspath(os.path.dirname(__file__))
        server_file = os.path.join(base, 'httpcache', 'tests', 'server.py')
        cmd = ['python', server_file]

        kw = {}
        if not os.environ.get('TEST_SERVER_OUTPUT'):
            kw = {'stdout': subprocess.PIPE,
                  'stderr': subprocess.STDOUT}
        self.proc = subprocess.Popen(cmd, **kw)
        url = 'http://localhost:8080'
        up = None
        while not up:
            try:
                up = requests.get(url)
            except requests.ConnectionError:
                time.sleep(1)

    def stop(self):
        self.proc.terminate()


def pytest_namespace():
    return dict(server=TestServer())


def pytest_configure(config):
    py.test.server.start()


def pytest_unconfigure(config):
    py.test.server.stop()