Skip to content

Commit bb44e2b

Browse files
authored
Merge pull request #8512 from elpaso/docker-testing-env-tests
Docker testing env tests
2 parents d1d3a51 + 9c6e24f commit bb44e2b

File tree

5 files changed

+127
-6
lines changed

5 files changed

+127
-6
lines changed

.ci/travis/linux/docker_test.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
###########################################################################
3+
# docker_test.sh
4+
#
5+
# Run a particular test on docker testing env and return its exit code
6+
#
7+
# Arguments:
8+
#
9+
# $1: test name in dotted notation
10+
#
11+
# ---------------------
12+
# Date : November 2018
13+
# Copyright : (C) 2018 by Alessandro Pasotti
14+
# Email : elpaso at itopen dot it
15+
###########################################################################
16+
# #
17+
# This program is free software; you can redistribute it and/or modify #
18+
# it under the terms of the GNU General Public License as published by #
19+
# the Free Software Foundation; either version 2 of the License, or #
20+
# (at your option) any later version. #
21+
# #
22+
###########################################################################
23+
24+
TEST_NAME=$1
25+
26+
docker exec -it qgis-testing-environment sh -c "cd /tests_directory && qgis_testrunner.sh ${TEST_NAME}" &>/dev/null
27+
28+
echo $?

.ci/travis/linux/script.sh

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ if [[ ${DOCKER_BUILD_QGIS_IMAGE} =~ true ]]; then
3434
docker push "qgis/qgis:${DOCKER_TAG}"
3535
popd
3636
else
37-
# running tests
37+
# running QGIS tests
3838
docker-compose -f ${TRAVIS_BUILD_DIR}/.docker/docker-compose.travis.yml run --rm qgis-deps
39+
40+
# running tests for the python test runner
41+
docker run -d --name qgis-testing-environment -v ${TRAVIS_BUILD_DIR}/tests/src/python:/tests_directory -e DISPLAY=:99 "qgis/qgis:${DOCKER_TAG}"
42+
sleep 10 # Wait for xvfb to finish starting
43+
# Temporary workaround until docker images are built
44+
docker cp ${TRAVIS_BUILD_DIR}/.docker/qgis_resources/test_runner/qgis_testrunner.sh qgis-testing-environment:/usr/bin/qgis_testrunner.sh
45+
# Run tests in the docker
46+
# Passing cases:
47+
TEST_SCRIPT_PATH=${TRAVIS_BUILD_DIR}/.ci/travis/linux/docker_test.sh
48+
[[ $(${TEST_SCRIPT_PATH} test_testrunner.run_passing) -eq '0' ]]
49+
[[ $(${TEST_SCRIPT_PATH} test_testrunner.run_skipped_and_passing) -eq '0' ]]
50+
# Failing cases:
51+
[[ $(${TEST_SCRIPT_PATH} test_testrunner) -eq '1' ]]
52+
[[ $(${TEST_SCRIPT_PATH} test_testrunner.run_all) -eq '1' ]]
53+
[[ $(${TEST_SCRIPT_PATH} test_testrunner.run_failing) -eq '1' ]]
3954
fi

.docker/qgis_resources/test_runner/qgis_testrunner.sh

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
#!/bin/bash
2-
# Run a test inside QGIS
2+
# Run a python test inside QGIS
3+
# Note: the test module and function are specified in dotted notation
4+
# for example, to run the test function run_all (which is the default anyway)
5+
# $ qgis_testrunner.sh tests_folder.test_module.run_all
6+
# tests_folder must be in PYTHONPATH (plugins main folders are automatically added to path)
7+
38
### Turn on debug mode ###
49
#set -x
510

611
TEST_NAME=$1
712

8-
cd /tests_directory || exit
913
echo "Running test $1 ..."
1014
OUTPUT=$(QGIS_TEST_MODULE=${TEST_NAME} unbuffer qgis --version-migration --nologo --code /usr/bin/qgis_testrunner.py "$TEST_NAME" 2>/dev/null | tee /dev/tty)
1115
EXIT_CODE="$?"
1216
if [ -z "$OUTPUT" ]; then
1317
echo "ERROR: no output from the test runner! (exit code: ${EXIT_CODE})"
1418
exit 1
1519
fi
16-
echo "$OUTPUT" | grep -q FAILED
20+
echo "$OUTPUT" | grep -q 'FAILED'
1721
IS_FAILED="$?"
18-
echo "$OUTPUT" | grep OK | grep -q 'Ran'
22+
echo "$OUTPUT" | grep -q 'OK' && echo "$OUTPUT" | grep -q 'Ran'
1923
IS_PASSED="$?"
2024
echo "$OUTPUT" | grep "QGIS died on signal"
2125
IS_DEAD="$?"
22-
echo "Finished running test $1."
26+
echo "Finished running test $1 (codes: IS_DEAD=$IS_DEAD IS_FAILED=$IS_FAILED IS_PASSED=$IS_PASSED)."
2327
if [ "$IS_PASSED" -eq "0" ] && [ "$IS_FAILED" -eq "1" ] && [ "$IS_DEAD" -eq "1" ]; then
2428
exit 0;
2529
fi

python/testing/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,11 @@ def start_app(cleanup=True):
394394
except NameError:
395395
myGuiFlag = True # All test will run qgis in gui mode
396396

397+
try:
398+
sys.argv
399+
except:
400+
sys.argv = ['']
401+
397402
# In python3 we need to convert to a bytes object (or should
398403
# QgsApplication accept a QString instead of const char* ?)
399404
try:

tests/src/python/test_testrunner.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
# -*- coding: utf-8 -*-
3+
"""QGIS Unit tests for the docker python test runner
4+
5+
.. note:: This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 2 of the License, or
8+
(at your option) any later version.
9+
"""
10+
__author__ = 'Alessandro Pasotti'
11+
__date__ = '19.11.2018'
12+
__copyright__ = 'Copyright 2018, The QGIS Project'
13+
# This will get replaced with a git SHA1 when you do a git archive
14+
__revision__ = '$Format:%H$'
15+
16+
import qgis # NOQA
17+
import sys
18+
19+
from qgis.testing import unittest
20+
from console import console
21+
from qgis.core import Qgis
22+
23+
24+
class TestTestRunner(unittest.TestCase):
25+
26+
def test_fails(self):
27+
self.assertTrue(False)
28+
29+
def test_passes(self):
30+
self.assertTrue(Qgis.QGIS_VERSION_INT > 0)
31+
32+
@unittest.skip('Skipped!')
33+
def test_skipped(self):
34+
self.assertTrue(False)
35+
36+
37+
def _make_runner(tests=[]):
38+
suite = unittest.TestSuite()
39+
for t in tests:
40+
suite.addTest(TestTestRunner(t))
41+
runner = unittest.TextTestRunner(verbosity=2)
42+
return runner.run(suite)
43+
44+
45+
# Test functions to be called by the runner
46+
47+
def run_all():
48+
"""Default function that is called by the runner if nothing else is specified"""
49+
return _make_runner(['test_fails', 'test_skipped', 'test_passes'])
50+
51+
52+
def run_failing():
53+
"""Run failing test only"""
54+
return _make_runner(['test_fails'])
55+
56+
57+
def run_passing():
58+
"""Run passing test only"""
59+
return _make_runner(['test_passes'])
60+
61+
62+
def run_skipped():
63+
"""Run skipped test only"""
64+
return _make_runner(['test_skipped'])
65+
66+
67+
def run_skipped_and_passing():
68+
"""Run skipped and passing test only"""
69+
return _make_runner(['test_skipped', 'test_passes'])

0 commit comments

Comments
 (0)