Index: src/gnuboat/deviceservice_impl/tests/__init__.py
===================================================================
--- src/gnuboat/deviceservice_impl/tests/__init__.py	(revision 30949f8491b5e971402c1886ab70676a350e35ae)
+++ src/gnuboat/deviceservice_impl/tests/__init__.py	(revision cd3ea37b12451090ce061ea78a7c2b770eea2db0)
@@ -31,7 +31,12 @@
 import subprocess
 import socket
+from urllib.parse import urlparse
+from werkzeug.utils import import_string
 
 from gnuboat.common.datahandler import (
     rrdhandler, loghandler, logdataset,
+)
+from gnuboat.common.webservice.Config import (
+    config_get
 )
 
@@ -74,5 +79,5 @@
     _create_maps(testcase, application)
 
-    _mapservice_start(testcase)
+    _mapservice_start(testcase, application)
 
     return application
@@ -214,13 +219,39 @@
 
 
-def _mapservice_start(testcase):
+def _mapservice_start(testcase, application):
     if 'TestMapserviceController' not in testcase.id():
         testcase.mapservice_process = None
     else:
+        print('MAPSERVICE Starting')
+        mapservice_url = config_get(application.config, 'MAPSERVICE', None)
+        if mapservice_url is None:
+            raise LookupError('MAPSERVICE not configured')
+        fragments = urlparse(mapservice_url)
+        if fragments.hostname is None:
+            raise LookupError('MAPSERVICE host not found')
+        if fragments.port is None:
+            raise LookupError('MAPSERVICE port not found')
+
+        mapservice_config = 'Testing'
+        mapservice_config_object = import_string(
+            'gnuboat.mapservice_impl.Config.{}Config'.format(mapservice_config)
+        )
+        database_uri = config_get(mapservice_config_object,
+                                  'SQLALCHEMY_DATABASE_URI', None)
+        if database_uri is None:
+            raise LookupError(
+                'SQLALCHEMY_DATABASE_URI not in MAPSERVICE Testing config')
+        database_uri = database_uri.replace('sqlite:///', '')
+        if os.path.isfile(database_uri):
+            os.remove(database_uri)
+
         mapservice_env = dict(os.environ)
-        mapservice_env['FLASK_ENV'] = 'Testing'
+        mapservice_env['FLASK_ENV'] = mapservice_config
+        # the timeout is needed, because of the spatiallite table creation,
+        # which is pretty slow
         testcase.mapservice_process = subprocess.Popen(
             [
-                'gnuboat-dev-mapservice', '--bind=localhost:8080',
+                'gnuboat-dev-mapservice',
+                '--bind={}:{}'.format(fragments.hostname, fragments.port),
                 '--log-level=debug'
             ],
@@ -229,5 +260,23 @@
         )
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        # we need to give mapservice a chance to start before calling
+        # connect_ex
+        time.sleep(2)
         s.settimeout(10)
-        if s.connect_ex(('localhost', int(8090))) == 0:
+        if s.connect_ex((fragments.hostname, int(fragments.port))) == 0:
+            print('MAPSERVICE running at {}:{}'.format(
+                fragments.hostname, fragments.port))
             s.shutdown(socket.SHUT_RDWR)
+        else:
+            print('MAPSERVICE Failed to start at {}:{}'.format(
+                fragments.hostname, fragments.port))
+            testcase.mapservice_process.terminate()
+            (stdout_data, stderr_data) = \
+                testcase.mapservice_process.communicate(timeout=10)
+            if stdout_data:
+                stdout_data = stdout_data.decode('utf-8')
+                print('MAPSERVICE STDOUT: {}'.format(stdout_data))
+            if stderr_data:
+                stderr_data = stderr_data.decode('utf-8')
+                print('MAPSERVICE STDERR: {}'.format(stderr_data))
+            testcase.mapservice_process = None
Index: src/gnuboat/mapservice_impl/__init__.py
===================================================================
--- src/gnuboat/mapservice_impl/__init__.py	(revision 30949f8491b5e971402c1886ab70676a350e35ae)
+++ src/gnuboat/mapservice_impl/__init__.py	(revision cd3ea37b12451090ce061ea78a7c2b770eea2db0)
@@ -59,17 +59,4 @@
 
 
-@application.before_first_request
-def create_database():
-    @event.listens_for(db.engine, "connect")
-    def load_spatialite_extension(dbapi_conn, connection_record):
-        load_spatialite(dbapi_conn, connection_record)
-
-    if flask.current_app.config['CREATE_TEST_DATABASE'] is True:
-        from gnuboat.mapservice_impl.demo import DbSetup as demoDbSetup
-        demoDbSetup.setup()
-    else:
-        db_setup()
-
-
 def setup_app(app_name, config, template_folder=None):
     application.json_encoder = encoder.JSONEncoder
@@ -91,2 +78,12 @@
     mail.init_app(application)
     celery.config_from_object(config)
+    with application.app_context():
+        @event.listens_for(db.engine, "connect")
+        def load_spatialite_extension(dbapi_conn, connection_record):
+            load_spatialite(dbapi_conn, connection_record)
+
+        if flask.current_app.config['CREATE_TEST_DATABASE'] is True:
+            from gnuboat.mapservice_impl.demo import DbSetup as demoDbSetup
+            demoDbSetup.setup()
+        else:
+            db_setup()
