From 1b783c9bdeea6ea893a4842368d7cc8d09f875f1 Mon Sep 17 00:00:00 2001 From: Malte Franken Date: Tue, 9 Jan 2024 20:35:27 +1100 Subject: [PATCH] Migrate to pytest (#42) * add test requirements * add pytest configuration * add requirements * refactoring ci process * fix typo * use async pytest * add pytest libraries * add pytest libraries * remove unnecessary file * remove event_loop fixture * migrated to different http mock library --- .github/workflows/ci.yaml | 23 +++++--- pyproject.toml | 5 ++ requirements-test.txt | 6 +++ requirements.txt | 3 ++ tests/conftest.py | 10 ++++ tests/test_feed.py | 105 +++++++++++++++++-------------------- tests/test_feed_manager.py | 68 +++++++++++------------- tox.ini | 40 -------------- 8 files changed, 118 insertions(+), 142 deletions(-) create mode 100644 requirements-test.txt create mode 100644 requirements.txt create mode 100644 tests/conftest.py delete mode 100644 tox.ini diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index df3e225..312489e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,7 +12,7 @@ jobs: name: Python ${{ matrix.python-version }} runs-on: ubuntu-latest env: - USING_COVERAGE: '3.8' + USING_COVERAGE: '3.10' strategy: fail-fast: true matrix: @@ -28,13 +28,22 @@ jobs: python -VV python -m site python -m pip install --upgrade pip setuptools wheel - python -m pip install --upgrade coverage[toml] virtualenv tox tox-gh-actions - - name: "Run tox targets for ${{ matrix.python-version }}" - run: "python -m tox" - - name: "Combine coverage" - if: "contains(env.USING_COVERAGE, matrix.python-version)" + python -m pip install -r requirements.txt + python -m pip install -r requirements-test.txt + - name: "Run tests for ${{ matrix.python-version }}" run: | - set -xe + pip install pytest-cov + pip install coverage + pytest \ + -qq \ + --timeout=9 \ + --durations=10 \ + -n auto \ + --cov aio_geojson_client \ + --cov-report xml \ + -o console_output_style=count \ + -p no:sugar \ + tests python -m coverage xml - name: "Upload coverage to Codecov" if: "contains(env.USING_COVERAGE, matrix.python-version)" diff --git a/pyproject.toml b/pyproject.toml index 4cea53a..9f01eba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,3 +4,8 @@ target-version = ['py38', 'py39', 'py310', 'py311'] [tool.isort] profile = "black" src_paths = ["aio_geojson_client", "tests"] + +[tool.pytest.ini_options] +testpaths = [ + "tests", +] diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..925d447 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,6 @@ +pytest-asyncio +pytest-timeout +pytest-xdist +pytest-cov +mock +aioresponses diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..060d88f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +aiohttp>=3.7.4,<4 +geojson>=2.4.0 +haversine>=1.0.1 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..6cf4a6a --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,10 @@ +"""Configuration for tests.""" +import pytest +from aioresponses import aioresponses + + +@pytest.fixture +def mock_aioresponse(): + """Return aioresponse fixture.""" + with aioresponses() as m: + yield m diff --git a/tests/test_feed.py b/tests/test_feed.py index 62a65ba..88fdfd3 100644 --- a/tests/test_feed.py +++ b/tests/test_feed.py @@ -1,5 +1,6 @@ """Test for the generic geojson feed.""" import asyncio +from http import HTTPStatus from unittest.mock import MagicMock import aiohttp @@ -15,18 +16,16 @@ @pytest.mark.asyncio -async def test_update_ok(aresponses, event_loop): +async def test_update_ok(mock_aioresponse): """Test updating feed is ok.""" home_coordinates = (-31.0, 151.0) - aresponses.add( - "test.url", - "/testpath", - "get", - aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200), + mock_aioresponse.get( + "http://test.url/testpath", + status=HTTPStatus.OK, + body=load_fixture("generic_feed_1.json"), ) - async with aiohttp.ClientSession(loop=event_loop) as websession: - + async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession: feed = MockGeoJsonFeed(websession, home_coordinates, "http://test.url/testpath") assert ( repr(feed) == "