diff --git a/.gitattributes b/.gitattributes
index 0069c24195de1795f2ee87379676beabff2ff434..bc9111d65dee417fbfa1da33f83d805be741ece7 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -4284,6 +4284,7 @@ SAS/LSMR/test/t_lsmrapp_specification_django.sh -text
 SAS/LSMR/test/t_lsmrapp_specification_functional.py -text
 SAS/LSMR/test/t_lsmrapp_specification_functional.run -text
 SAS/LSMR/test/t_lsmrapp_specification_functional.sh -text
+SAS/LSMR/test/test_funcs.sh -text
 SAS/MoM/CMakeLists.txt -text
 SAS/MoM/MoMQueryService/CMakeLists.txt -text
 SAS/MoM/MoMQueryService/MoMQueryServiceClient/CMakeLists.txt -text
diff --git a/SAS/LSMR/test/CMakeLists.txt b/SAS/LSMR/test/CMakeLists.txt
index 42d75396943652ddefdc3838475ec4932fc8c9e7..6cb354797849e3a10a416f3b7e67d63db09085c0 100644
--- a/SAS/LSMR/test/CMakeLists.txt
+++ b/SAS/LSMR/test/CMakeLists.txt
@@ -15,6 +15,9 @@ lofar_add_test(t_lsmrapp_specification_functional)
 lofar_add_test(t_lsmrapp_scheduling_django)
 lofar_add_test(t_lsmrapp_scheduling_functional)
 
+# copy helper script to bin dir
+configure_file(test_funcs.sh ${CMAKE_CURRENT_BINARY_DIR}/test_funcs.sh COPYONLY)
+
 # Copy testrunner module so Python won't complain about incorrect import location:
 set(_py_files
     __init__.py
diff --git a/SAS/LSMR/test/t_lsmrapp_scheduling_django.run b/SAS/LSMR/test/t_lsmrapp_scheduling_django.run
index 36e80e4f6f9565f5ca16e5cc5773910fadadcd7c..a2afc82514039c337593dad4a229febea6aa853b 100755
--- a/SAS/LSMR/test/t_lsmrapp_scheduling_django.run
+++ b/SAS/LSMR/test/t_lsmrapp_scheduling_django.run
@@ -1,61 +1,9 @@
 #!/bin/bash
-set +x
-
-# get free port for postgres (default port or first subsequent free)
-function get_free_port {
-  comm -23 <(seq $1 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | head -n 1
-}
-PORT=$(get_free_port 7654)
-
-# write test credentials to file
-mkdir -p ~/.lofar/dbcredentials
-
-DJANGO_TEST_DATABASE_NAME="lsmr_test_`uuidgen | sed 's/-/_/g'`"
-CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_DATABASE_NAME.ini"
-
-echo "creating test credentials file: $CREDENTIALS_PATH"
-echo "[database:$DJANGO_TEST_DATABASE_NAME]
-host=localhost
-type=postgres
-database=$DJANGO_TEST_DATABASE_NAME
-port=$PORT
-user=lsrm
-password=lsrm
-" > "$CREDENTIALS_PATH"
-
-
-
-# setup propagation of signals to child processes
-teardown() {
-    echo "tearing down test environment"
-
-    echo "removing test credentials file: $CREDENTIALS_PATH"
-    rm $CREDENTIALS_PATH
-}
-
-## Trap upon signals and upon normal exit.
-trap 'STATUS=$?; teardown; exit $STATUS' SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM
-
-
-export DJANGO_TEST_PORT=$DJANGO_TEST_PORT
-export LSMR_DBCREDENTIALS=$DJANGO_TEST_DATABASE_NAME
-
-# Run test
-$LOFARROOT/lib*/python*/site-packages/lofar/sas/lsmr/manage.py test --pattern="t_lsmrapp_scheduling_django.py" --testrunner=postgres_testrunner.PostgresqlTestRunner &
-TEST_PID=$!
-
-wait "$TEST_PID"
-# wait again (to get the status code of the test)
-wait "$TEST_PID"
-TEST_EXIT_CODE=$?
-
-teardown
-
-exit $TEST_EXIT_CODE
-
-
 
+. test_funcs.sh
 
+setup
+run_test "$LOFARROOT/lib*/python*/site-packages/lofar/sas/lsmr/manage.py test --pattern=\"t_lsmrapp_scheduling_django.py\" --testrunner=postgres_testrunner.PostgresqlTestRunner"
 
 
 
diff --git a/SAS/LSMR/test/t_lsmrapp_scheduling_functional.run b/SAS/LSMR/test/t_lsmrapp_scheduling_functional.run
index fe3d426967aec923b5336d75db9e416262112516..ca5934d5d7c57731cdce9571ab58eb6907150daa 100755
--- a/SAS/LSMR/test/t_lsmrapp_scheduling_functional.run
+++ b/SAS/LSMR/test/t_lsmrapp_scheduling_functional.run
@@ -1,80 +1,6 @@
 #!/bin/bash
-set +x
 
-# get free port for postgres (default port or first subsequent free)
-function get_free_port {
-  comm -23 <(seq $1 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | head -n 1
-}
-PORT=$(get_free_port 7654)
+. test_funcs.sh
 
-# write test credentials to file
-mkdir -p ~/.lofar/dbcredentials
-
-DJANGO_TEST_DATABASE_NAME="lsmr_test_`uuidgen | sed 's/-/_/g'`"
-CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_DATABASE_NAME.ini"
-
-echo "creating test credentials file: $CREDENTIALS_PATH"
-echo "[database:$DJANGO_TEST_DATABASE_NAME]
-host=localhost
-type=postgres
-database=$DJANGO_TEST_DATABASE_NAME
-port=$PORT
-user=lsmr
-password=lsmr
-" > "$CREDENTIALS_PATH"
-
-#keep track of helper application pids to kill in teardown
-PIDS=
-
-# setup propagation of signals to child processes
-teardown() {
-    echo "tearing down test environment"
-    for PID in $PIDS
-    do
-        # get the full command
-        CMD="`ps --pid $PID h -o command`"
-        echo "killing helper application: $CMD"
-
-        # and kill it
-        kill -TERM $PID
-    done
-
-    echo "removing test credentials file: $CREDENTIALS_PATH"
-    rm $CREDENTIALS_PATH
-}
-
-## Trap upon signals and upon normal exit.
-trap 'STATUS=$?; teardown; exit $STATUS' SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM
-
-
-
-
-# fire up a postgres test database
-lsmr_testdatabase -C $DJANGO_TEST_DATABASE_NAME &
-PIDS="$! $PIDS"
-sleep 5
-echo "Started Django test database server\n\n"
-
-# Run Django test instance
-export LSMR_RAISE_ON_SIGNALS="True"
-DJANGO_TEST_PORT=8777
-lsmr -p $DJANGO_TEST_PORT -C $DJANGO_TEST_DATABASE_NAME &
-PIDS="$! $PIDS"
-sleep 5
-echo "Started lsmr django server\n\n"
-
-export DJANGO_TEST_PORT=$DJANGO_TEST_PORT
-export LSMR_DBCREDENTIALS=$DJANGO_TEST_DATABASE_NAME
-
-# Run test
-./t_lsmrapp_scheduling_functional.py &
-TEST_PID=$!
-
-wait "$TEST_PID"
-# wait again (to get the status code of the test)
-wait "$TEST_PID"
-TEST_EXIT_CODE=$?
-
-teardown
-
-exit $TEST_EXIT_CODE
+setup_with_test_lsmr
+run_test ./t_lsmrapp_scheduling_functional.py
diff --git a/SAS/LSMR/test/t_lsmrapp_specification_django.run b/SAS/LSMR/test/t_lsmrapp_specification_django.run
index 443778b7168a8ae0dcb1282cf6317fa0509f79e2..c597fa96d9f21a7179ee7aff9057ba53bba7d55f 100755
--- a/SAS/LSMR/test/t_lsmrapp_specification_django.run
+++ b/SAS/LSMR/test/t_lsmrapp_specification_django.run
@@ -1,56 +1,10 @@
 #!/bin/bash
-set +x
 
-# get free port for postgres (default port or first subsequent free)
-function get_free_port {
-  comm -23 <(seq $1 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | head -n 1
-}
-PORT=$(get_free_port 7654)
+. test_funcs.sh
 
-# write test credentials to file
-mkdir -p ~/.lofar/dbcredentials
+setup
+run_test "$LOFARROOT/lib*/python*/site-packages/lofar/sas/lsmr/manage.py test --pattern=\"t_lsmrapp_specification_django.py\" --testrunner=postgres_testrunner.PostgresqlTestRunner"
 
-DJANGO_TEST_DATABASE_NAME="lsmr_test_`uuidgen | sed 's/-/_/g'`"
-CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_DATABASE_NAME.ini"
-
-echo "creating test credentials file: $CREDENTIALS_PATH"
-echo "[database:$DJANGO_TEST_DATABASE_NAME]
-host=localhost
-type=postgres
-database=$DJANGO_TEST_DATABASE_NAME
-port=$PORT
-user=lsrm
-password=lsrm
-" > "$CREDENTIALS_PATH"
-
-
-# setup propagation of signals to child processes
-teardown() {
-    echo "tearing down test environment"
-
-    echo "removing test credentials file: $CREDENTIALS_PATH"
-    rm $CREDENTIALS_PATH
-}
-
-## Trap upon signals and upon normal exit.
-trap 'STATUS=$?; teardown; exit $STATUS' SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM
-
-
-export DJANGO_TEST_PORT=$DJANGO_TEST_PORT
-export LSMR_DBCREDENTIALS=$DJANGO_TEST_DATABASE_NAME
-
-# Run test
-$LOFARROOT/lib*/python*/site-packages/lofar/sas/lsmr/manage.py test --pattern="t_lsmrapp_specification_django.py" --testrunner=postgres_testrunner.PostgresqlTestRunner &
-TEST_PID=$!
-
-wait "$TEST_PID"
-# wait again (to get the status code of the test)
-wait "$TEST_PID"
-TEST_EXIT_CODE=$?
-
-teardown
-
-exit $TEST_EXIT_CODE
 
 
 
diff --git a/SAS/LSMR/test/t_lsmrapp_specification_functional.run b/SAS/LSMR/test/t_lsmrapp_specification_functional.run
index 9983333a5e1313cbaa8de6810b14bef1477f5711..0f6ed6e24062438bf3f8094c80e9eb4d42c71a3d 100755
--- a/SAS/LSMR/test/t_lsmrapp_specification_functional.run
+++ b/SAS/LSMR/test/t_lsmrapp_specification_functional.run
@@ -1,80 +1,10 @@
 #!/bin/bash
-set +x
 
-# get free port for postgres (default port or first subsequent free)
-function get_free_port {
-  comm -23 <(seq $1 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | head -n 1
-}
-PORT=$(get_free_port 7654)
+. test_funcs.sh
 
-# write test credentials to file
-mkdir -p ~/.lofar/dbcredentials
+setup_with_test_lsmr
+run_test ./t_lsmrapp_specification_functional.py
 
-DJANGO_TEST_DATABASE_NAME="lsmr_test_`uuidgen | sed 's/-/_/g'`"
-CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_DATABASE_NAME.ini"
 
-echo "creating test credentials file: $CREDENTIALS_PATH"
-echo "[database:$DJANGO_TEST_DATABASE_NAME]
-host=localhost
-type=postgres
-database=$DJANGO_TEST_DATABASE_NAME
-port=$PORT
-user=lsmr
-password=lsmr
-" > "$CREDENTIALS_PATH"
 
-#keep track of helper application pids to kill in teardown
-PIDS=
 
-# setup propagation of signals to child processes
-teardown() {
-    echo "tearing down test environment"
-    for PID in $PIDS
-    do
-        # get the full command
-        CMD="`ps --pid $PID h -o command`"
-        echo "killing helper application: $CMD"
-
-        # and kill it
-        kill -TERM $PID
-    done
-
-    echo "removing test credentials file: $CREDENTIALS_PATH"
-    rm $CREDENTIALS_PATH
-}
-
-## Trap upon signals and upon normal exit.
-trap 'STATUS=$?; teardown; exit $STATUS' SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM
-
-
-
-
-# fire up a postgres test database
-lsmr_testdatabase -C $DJANGO_TEST_DATABASE_NAME &
-PIDS="$! $PIDS"
-sleep 5
-echo "Started Django test database server\n\n"
-
-# Run Django test instance
-export LSMR_RAISE_ON_SIGNALS="True"
-DJANGO_TEST_PORT=8777
-lsmr -p $DJANGO_TEST_PORT -C $DJANGO_TEST_DATABASE_NAME &
-PIDS="$! $PIDS"
-sleep 5
-echo "Started lsmr django server\n\n"
-
-export DJANGO_TEST_PORT=$DJANGO_TEST_PORT
-export LSMR_DBCREDENTIALS=$DJANGO_TEST_DATABASE_NAME
-
-# Run test
-./t_lsmrapp_specification_functional.py &
-TEST_PID=$!
-
-wait "$TEST_PID"
-# wait again (to get the status code of the test)
-wait "$TEST_PID"
-TEST_EXIT_CODE=$?
-
-teardown
-
-exit $TEST_EXIT_CODE
diff --git a/SAS/LSMR/test/test_funcs.sh b/SAS/LSMR/test/test_funcs.sh
new file mode 100755
index 0000000000000000000000000000000000000000..a00a8dc350a25824c5994e096db26a48dfa37c30
--- /dev/null
+++ b/SAS/LSMR/test/test_funcs.sh
@@ -0,0 +1,89 @@
+#!/bin/bash
+set +x
+
+# get free port for postgres (default port or first subsequent free)
+function get_free_port {
+  comm -23 <(seq $1 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | head -n 1
+}
+
+function setup {
+    # write test credentials to file
+    mkdir -p ~/.lofar/dbcredentials
+
+    DB_PORT=$(get_free_port 7654)
+
+    DJANGO_TEST_DATABASE_NAME="lsmr_test_`uuidgen | sed 's/-/_/g'`"
+    CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_DATABASE_NAME.ini"
+
+    echo "creating test credentials file: $CREDENTIALS_PATH"
+    echo "[database:$DJANGO_TEST_DATABASE_NAME]
+    host=localhost
+    type=postgres
+    database=$DJANGO_TEST_DATABASE_NAME
+    port=$DB_PORT
+    user=lsmr
+    password=lsmr
+    " > "$CREDENTIALS_PATH"
+
+    #keep track of helper application pids to kill in teardown
+    PIDS=
+
+    DJANGO_TEST_PORT=$(get_free_port 8765)
+
+    export DJANGO_TEST_PORT=$DJANGO_TEST_PORT
+    export LSMR_DBCREDENTIALS=$DJANGO_TEST_DATABASE_NAME
+
+    ## Trap upon signals and upon normal exit.
+    trap 'STATUS=$?; teardown; exit $STATUS' SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM
+}
+
+function teardown {
+    echo "tearing down test environment"
+    for PID in $PIDS
+    do
+        # get the full command
+        CMD="`ps --pid $PID h -o command`"
+        echo "killing helper application: pid=$PID cmd='$CMD'"
+
+        # and kill it
+        kill -TERM $PID
+    done
+
+    echo "removing test credentials file: $CREDENTIALS_PATH"
+    rm $CREDENTIALS_PATH
+}
+
+
+function setup_with_test_lsmr {
+    setup
+
+    # fire up a postgres test database
+    lsmr_testdatabase -C $DJANGO_TEST_DATABASE_NAME &
+    PIDS="$! $PIDS"
+    sleep 5
+    echo "Started Django test database server\n\n"
+
+    # Run Django test instance
+    export LSMR_RAISE_ON_SIGNALS="True"
+    lsmr -p $DJANGO_TEST_PORT -C $DJANGO_TEST_DATABASE_NAME &
+    PIDS="$! $PIDS"
+    sleep 5
+    echo "Started lsmr django server\n\n"
+}
+
+function run_test {
+    # Run test
+    echo "starting test: $1"
+    $1 &
+    TEST_PID=$!
+
+    wait "$TEST_PID"
+    # wait again (to get the status code of the test)
+    wait "$TEST_PID"
+    TEST_EXIT_CODE=$?
+
+    teardown
+
+    exit $TEST_EXIT_CODE
+}
+