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

Test refactoring #223

Merged
merged 9 commits into from
Aug 16, 2023
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
7 changes: 5 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
Expand All @@ -22,9 +22,12 @@ jobs:
- name: Install tox
run: pip install tox

- name: Run tests
- name: Run asyncio tests
run: tox -e py

- name: Run twisted tests
run: tox -e py-twisted

- name: Upload coverage report
run: |
if [ "${{ runner.os }}" = "Linux" ]; then
Expand Down
13 changes: 9 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
.mypy_cache/
*.egg-info/
.tox/
.coverage
.coverage.*
htmlcov/
coverage.xml
build/
dist/
examples/*.png
pip-wheel-metadata/

# coverage
.coverage
.coverage.*
htmlcov/
coverage.xml
coverage-*.xml
coverage-asyncio/
coverage-twisted/
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand All @@ -33,7 +32,7 @@
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Topic :: Software Development :: Libraries :: Python Modules",
],
python_requires=">=3.7",
python_requires=">=3.8",
install_requires=[
"scrapy>=2.0,!=2.4.0",
"playwright>=1.15",
Expand Down
31 changes: 0 additions & 31 deletions tests/conftest.py

This file was deleted.

136 changes: 0 additions & 136 deletions tests/test_utils.py

This file was deleted.

Empty file added tests/tests_asyncio/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import platform
import tempfile
from pathlib import Path
from unittest import IsolatedAsyncioTestCase
from uuid import uuid4

import pytest
Expand Down Expand Up @@ -245,14 +246,14 @@ async def test_contexts_dynamic(self):
assert cookie["domain"] == "example.org"


class TestCaseMultipleContextsChromium(MixinTestCaseMultipleContexts):
class TestCaseMultipleContextsChromium(IsolatedAsyncioTestCase, MixinTestCaseMultipleContexts):
browser_type = "chromium"


class TestCaseMultipleContextsFirefox(MixinTestCaseMultipleContexts):
class TestCaseMultipleContextsFirefox(IsolatedAsyncioTestCase, MixinTestCaseMultipleContexts):
browser_type = "firefox"


@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
class TestCaseMultipleContextsWebkit(MixinTestCaseMultipleContexts):
class TestCaseMultipleContextsWebkit(IsolatedAsyncioTestCase, MixinTestCaseMultipleContexts):
browser_type = "webkit"
7 changes: 4 additions & 3 deletions tests/test_headers.py → tests/tests_asyncio/test_headers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import platform
from unittest import IsolatedAsyncioTestCase

import pytest
from scrapy import Spider, Request
Expand Down Expand Up @@ -91,14 +92,14 @@ async def important_headers(*_args, **_kwargs) -> dict:
assert "asdf" not in headers


class TestProcessHeadersChromium(MixinProcessHeadersTestCase):
class TestProcessHeadersChromium(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
browser_type = "chromium"


class TestProcessHeadersFirefox(MixinProcessHeadersTestCase):
class TestProcessHeadersFirefox(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
browser_type = "firefox"


@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
class TestProcessHeadersWebkit(MixinProcessHeadersTestCase):
class TestProcessHeadersWebkit(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
browser_type = "webkit"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import platform
import subprocess
from tempfile import NamedTemporaryFile
from unittest import IsolatedAsyncioTestCase

import pytest
from scrapy import Spider, Request
Expand All @@ -22,19 +23,25 @@ def get_mimetype(file):
).stdout.strip()


@pytest.mark.asyncio
async def test_page_methods():
screenshot = PageMethod("screenshot", "foo", 123, path="/tmp/file", type="png")
assert screenshot.method == "screenshot"
assert screenshot.args == ("foo", 123)
assert screenshot.kwargs == {"path": "/tmp/file", "type": "png"}
assert screenshot.result is None
assert str(screenshot) == "<PageMethod for method 'screenshot'>"
class TestPageMethods(IsolatedAsyncioTestCase):
@pytest.mark.asyncio
async def test_page_methods(self):
screenshot = PageMethod("screenshot", "foo", 123, path="/tmp/file", type="png")
assert screenshot.method == "screenshot"
assert screenshot.args == ("foo", 123)
assert screenshot.kwargs == {"path": "/tmp/file", "type": "png"}
assert screenshot.result is None
assert str(screenshot) == "<PageMethod for method 'screenshot'>"


class MixinPageMethodTestCase:
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
caplog.set_level(logging.DEBUG)
self._caplog = caplog

@pytest.mark.asyncio
async def test_page_non_page_method(self, caplog):
async def test_page_non_page_method(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
req = Request(
Expand All @@ -56,10 +63,10 @@ async def test_page_non_page_method(self, caplog):
"scrapy-playwright",
logging.WARNING,
f"Ignoring {repr(obj)}: expected PageMethod, got {repr(type(obj))}",
) in caplog.record_tuples
) in self._caplog.record_tuples

@pytest.mark.asyncio
async def test_page_mixed_page_methods(self, caplog):
async def test_page_mixed_page_methods(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
req = Request(
Expand All @@ -81,7 +88,7 @@ async def test_page_mixed_page_methods(self, caplog):
"scrapy-playwright",
logging.WARNING,
f"Ignoring {repr(does_not_exist)}: could not find method",
) in caplog.record_tuples
) in self._caplog.record_tuples
assert not req.meta["playwright_page_methods"]["is_closed"].result
assert req.meta["playwright_page_methods"]["title"].result == "Awesome site"

Expand Down Expand Up @@ -178,14 +185,14 @@ async def test_page_method_pdf(self):
assert get_mimetype(pdf_file) == "application/pdf"


class TestPageMethodChromium(MixinPageMethodTestCase):
class TestPageMethodChromium(IsolatedAsyncioTestCase, MixinPageMethodTestCase):
browser_type = "chromium"


class TestPageMethodFirefox(MixinPageMethodTestCase):
class TestPageMethodFirefox(IsolatedAsyncioTestCase, MixinPageMethodTestCase):
browser_type = "firefox"


@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
class TestPageMethodWebkit(MixinPageMethodTestCase):
class TestPageMethodWebkit(IsolatedAsyncioTestCase, MixinPageMethodTestCase):
browser_type = "webkit"
Loading
Loading