Hi all!

I'm working in a little Python exercise with testing since the beginning. So 
far I'm with my first end to end test (not even finished yet) trying to:

1) Launch a development web server linked to a demo app that just returns 
'Hello World!'

2) Make a GET request successfully

I can't understand very well what's happening. It seems that the main thread 
gets blocked listening to the web server. My intent was to spawn another 
process for the server independent of the test. Obviously I'm doing something 
wrong. I've made several guesses commenting pieces of code (tearDown method for 
example) but I didn't manage to solve the problem(s).

I'm running the test through PyCharm. Python version = 3.4.3



Here it's the code:


test_forecast_end_to_end.py
===========================

import unittest
from tests.end_to_end.forecastserver import ForecastServer
import src.forecast.webservice as webservice
import requests


class TestForecastEndToEnd(unittest.TestCase):

    def setUp(self):
        # Start the forecast server
        self.server = ForecastServer()
        self.server.start(webservice.app)

    def tearDown(self):
        # Stop the forecast server
        self.server.stop()

    def test_webservice_receives_a_request(self):
        response = requests.get('http://localhost:8000')
        print(response.text)


if __name__ == '__main__':
    unittest.main()



forecastserver.py
=================

from wsgiref.simple_server import make_server
import multiprocessing


class ForecastServer:

    def __init__(self):
        self._httpd_process = None

    def start(self, webservice):
        if not self._httpd_process:
            httpd = make_server('localhost', 8000, webservice)
            self._httpd_process = multiprocessing.Process(
                target=httpd.serve_forever)
            self._httpd_process.start()

    def stop(self):
        if self._httpd_process:
            self._httpd_process.join()
            self._httpd_process.terminate()

        del self._httpd_process



webservice.py
=============

def app(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)

    return ['Hello World!']
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to