Skip to content

Commit e8e66d0

Browse files
committed
Fix the python test runner and add a test for it
1 parent ba81a2e commit e8e66d0

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

.ci/travis/linux/script.sh

+14-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ if [[ ${DOCKER_BUILD_QGIS_IMAGE} =~ true ]]; then
3434
docker push "qgis/qgis:${DOCKER_TAG}"
3535
popd
3636
else
37-
# running tests
38-
docker-compose -f ${TRAVIS_BUILD_DIR}/.docker/docker-compose.travis.yml run --rm qgis-deps
37+
# running QGIS tests
38+
#docker-compose -f ${TRAVIS_BUILD_DIR}/.docker/docker-compose.travis.yml run --rm qgis-deps
39+
# running tests for the python test runner
40+
docker run -d --name qgis-testing-environment -v ${TRAVIS_BUILD_DIR}:/tests/src/python -e DISPLAY=:99 "qgis/qgis:${DOCKER_TAG}"
41+
docker exec -it qgis-testing-environment sh -c "qgis_testrunner.sh test_testrunner"
42+
[ $? -ne 0 ] # expected failure
43+
docker exec -it qgis-testing-environment sh -c "qgis_testrunner.sh test_testrunner.run_all"
44+
[ $? -ne 0 ] # expected failure
45+
docker exec -it qgis-testing-environment sh -c "qgis_testrunner.sh test_testrunner.run_failing"
46+
[ $? -ne 0 ] # expected failure
47+
docker exec -it qgis-testing-environment sh -c "qgis_testrunner.sh test_testrunner.run_passing"
48+
[ $? -eq 0 ] # expected pass
49+
docker exec -it qgis-testing-environment sh -c "qgis_testrunner.sh test_testrunner.run_skipped_and_passing"
50+
[ $? -eq 0 ] # expected pass
3951
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

tests/src/python/test_testrunner.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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, start_app
20+
from console import console
21+
from qgis.core import Qgis
22+
from qgis.PyQt.QtCore import QCoreApplication
23+
24+
start_app()
25+
26+
27+
class TestTestRunner(unittest.TestCase):
28+
29+
def test_fails(self):
30+
self.assertTrue(False)
31+
32+
def test_passes(self):
33+
self.assertTrue(Qgis.QGIS_VERSION_INT > 0)
34+
35+
@unittest.skip('Skipped!')
36+
def test_skipped(self):
37+
self.assertTrue(False)
38+
39+
40+
def _make_runner(tests=[]):
41+
suite = unittest.TestSuite()
42+
for t in tests:
43+
suite.addTest(TestTestRunner(t))
44+
runner = unittest.TextTestRunner(verbosity=2)
45+
return runner.run(suite)
46+
47+
48+
# Test functions to be called by the runner
49+
50+
def run_all():
51+
"""Default function that is called by the runner if nothing else is specified"""
52+
return _make_runner(['test_fails', 'test_skipped', 'test_passes'])
53+
54+
55+
def run_failing():
56+
"""Run failing test only"""
57+
return _make_runner(['test_fails'])
58+
59+
60+
def run_passes():
61+
"""Run passing test only"""
62+
return _make_runner(['test_passes'])
63+
64+
65+
def run_skipped():
66+
"""Run skipped test only"""
67+
return _make_runner(['test_skipped'])
68+
69+
70+
def run_skipped_and_passing():
71+
"""Run skipped and passing test only"""
72+
return _make_runner(['test_skipped', 'test_passes'])

0 commit comments

Comments
 (0)