Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to unittest #90

Merged
merged 11 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .coveragerc

This file was deleted.

50 changes: 31 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
name: Test
name: Test plugin

on:
pull_request:
branches: [main]
workflow_dispatch:
branches:
- main

env:
# plugin name/directory where the code for the plugin is stored
PLUGIN_NAME: gtfsgo
# python notation to test running inside plugin
TESTS_RUN_FUNCTION: gtfsgo.test_suite.test_package
# Docker settings
DOCKER_IMAGE: qgis/qgis

jobs:
Test:
Test-plugin-gtfsgo:
runs-on: ubuntu-latest

strategy:
matrix:
qgis_image_tag: [release-3_28]
env:
OS: ubuntu
QGIS_IMAGE_TAG: ${{ matrix.qgis_image_tag }}
steps:
- uses: actions/checkout@v3
docker_tags: [release-3_28, release-3_34]

- name: Pull QGIS
run: docker pull qgis/qgis:${{ matrix.qgis_image_tag }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Export requirements.txt
run: |
pip3 install poetry
poetry export --with dev -f requirements.txt -o requirements.txt

- name: Run tests
run: docker run --rm --net=host --volume .:/app -w=/app -e QGIS_PLUGIN_IN_CI=1 qgis/qgis:${{ matrix.qgis_image_tag }} sh -c "pip3 install -r /app/requirements.txt && xvfb-run -s '+extension GLX -screen 0 1024x768x24' pytest -v --cov --cov-report=xml --cov-report=term"
- name: Docker pull and create qgis-testing-environment
run: |
docker pull "$DOCKER_IMAGE":${{ matrix.docker_tags }}
docker run -d --name qgis-testing-environment -v .:/tests_directory/gtfsgo -e DISPLAY=:99 "$DOCKER_IMAGE":${{ matrix.docker_tags }}

- name: Docker set up QGIS
run: |
docker exec qgis-testing-environment sh -c "qgis_setup.sh $PLUGIN_NAME"
docker exec qgis-testing-environment sh -c "rm -f /root/.local/share/QGIS/QGIS3/profiles/default/python/plugins/$PLUGIN_NAME"
docker exec qgis-testing-environment sh -c "ln -s /tests_directory/$PLUGIN_NAME /root/.local/share/QGIS/QGIS3/profiles/default/python/plugins/$PLUGIN_NAME"
docker exec qgis-testing-environment sh -c "pip3 install -r /tests_directory/$PLUGIN_NAME/requirements.txt"

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: MIERUNE/GTFS-GO
- name: Docker run plugin tests
run: |
docker exec qgis-testing-environment sh -c "qgis_testrunner.sh $TESTS_RUN_FUNCTION"
264 changes: 1 addition & 263 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ python = "^3.9"


[tool.poetry.group.dev.dependencies]
pytest = "^7.2.1"
ruff = "^0.4.3"
pytest-qt = "^4.4.0"
pytest-cov = "^5.0.0"
pandas = "^2.2.2"
pytest-mock = "^3.14.0"
pytest-qgis = "^2.0.0"


[build-system]
Expand All @@ -25,5 +20,5 @@ build-backend = "poetry.core.masonry.api"

[tool.ruff]
select = ["F", "E", "W", "B", "C90", "N", "I"]
ignore = ["N999", "N802", "F401", "E501"]
ignore = ["N999", "N802", "F401", "E501", "N806", "C901", "N803", "N815", "B018"]
target-version = "py39"
85 changes: 85 additions & 0 deletions test_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Test Suite.
"""

import os
import sys
import tempfile
import unittest

from osgeo import gdal
from qgis.core import Qgis

try:
from pip import main as pipmain
except ImportError:
from pip._internal import main as pipmain

try:
import coverage
except ImportError:
pipmain(["install", "coverage"])
import coverage

__author__ = "Alessandro Pasotti"
__revision__ = "$Format:%H$"
__date__ = "30/04/2018"
__copyright__ = "Copyright 2018, North Road"


def _run_tests(test_suite, package_name, with_coverage=False):
"""Core function to test a test suite."""
count = test_suite.countTestCases()
print("########")
print("%s tests has been discovered in %s" % (count, package_name))
print("Python GDAL : %s" % gdal.VersionInfo("VERSION_NUM"))
print("QGIS version : {}".format(Qgis.version()))
print("########")
if with_coverage:
cov = coverage.Coverage(
source=["/processing_r"],
omit=["*/test/*"],
)
cov.start()

unittest.TextTestRunner(verbosity=3, stream=sys.stdout).run(test_suite)

if with_coverage:
cov.stop()
cov.save()

with tempfile.NamedTemporaryFile(delete=False) as report:
cov.report(file=report)
# Produce HTML reports in the `htmlcov` folder and open index.html
# cov.html_report()
report.close()

with open(report.name, "r", encoding="utf8") as fin:
print(fin.read())


def test_package(package="gtfsgo"):
"""Test package.
This function is called by travis without arguments.

:param package: The package to test.
:type package: str
"""
test_loader = unittest.defaultTestLoader
try:
test_suite = test_loader.discover(package)
except ImportError:
test_suite = unittest.TestSuite()
_run_tests(test_suite, package)


def test_environment():
"""Test package with an environment variable."""
package = os.environ.get("TESTING_PACKAGE", "gtfsgo")
test_loader = unittest.defaultTestLoader
test_suite = test_loader.discover(package)
_run_tests(test_suite, package)


if __name__ == "__main__":
test_package()
22 changes: 0 additions & 22 deletions tests/conftest.py

This file was deleted.

Loading
Loading