示例#1
0
    def _run_test_against_dev_server(self, request_logic, assert_logic=None):
        '''This method provides a template for writing integration tests that requires a development server being active.
        It accepts a request logic (code that actually do the http request) and an assert logic for making sure
        code is correct.'''

        assert_logic = assert_logic or self._check_server_started

        server = DevServer()

        def start_server():
            server.start()

        def stop_server():
            try:
                while not server.started:
                    time.sleep(0.01)

                request_logic(server)

                self.assertTrue(server.started)
            finally:
                server.stop()

        thread_start = threading.Thread(target=start_server)
        thread_start.start()

        thread_stop = threading.Thread(target=stop_server)
        thread_stop.start()

        thread_stop.join()

        self._check_server_started(server)
        assert_logic(server)
示例#2
0
 def init(self):
     self._settings_facade = Mock()
     self._settings_facade.get = self._get_server_key
     self._settings_facade_cls = Mock(return_value=self._settings_facade)
     self._server = DevServer(self._settings_facade_cls)
示例#3
0
class DevServerTests(FantasticoUnitTestsCase):
    '''This class provides the test cases for making sure dev server class works.'''
    def init(self):
        self._settings_facade = Mock()
        self._settings_facade.get = self._get_server_key
        self._settings_facade_cls = Mock(return_value=self._settings_facade)
        self._server = DevServer(self._settings_facade_cls)

    def _get_server_key(self, key):
        if key == "dev_server_port":
            return 12000

        if key == "dev_server_host":
            return "localhost"

    def test_start_ok(self):
        '''This test case ensures development server starts ok scenario.'''

        make_server = Mock()
        app = Mock()

        self._server.start(make_server, app)

        self.assertEqual("localhost", self._server.hostname)
        self.assertEqual(12000, self._server.port)
        self.assertTrue(self._server.started)

    def test_start_error(self):
        '''This test case ensures development server does not start if an exception is raised by make_server or 
        app parameters.'''

        inputs = [(Mock(side_effect=Exception("Error make_server")), Mock(),
                   "Error make_server"),
                  (Mock(), Mock(side_effect=Exception("Error app")),
                   "Error app")]

        for make_server, app, expected_msg in inputs:
            with self.assertRaises(Exception) as cm:
                self._server.start(make_server, app)

            self.assertTrue(str(cm.exception).find(expected_msg) > -1)

    def test_stop_server_notstarted(self):
        '''This test case makes sure stop method does not crash when dev server was not started.'''

        self._server.stop()

        self.assertFalse(self._server.started)

    def test_stop_server_whenstarted(self):
        '''This test case makes sure stop method correctly shutdowns a running dev server.'''

        self._shutdown_invoked = False

        def fake_shutdown():
            self._shutdown_invoked = True

        http_server = Mock()
        http_server.shutdown = fake_shutdown
        make_server = Mock(return_value=http_server)
        app = Mock()

        self._server.start(make_server, app)

        self._server.stop()

        self.assertTrue(self._shutdown_invoked)
        self.assertFalse(self._server.started)