Skip to content

Commit e0f3b61

Browse files
authored
Test refactoring (#223)
* Refactor tests, update test packages * Bump minimum Python version to 3.8 * Simplify tests for py38 * Adapt CI to run asyncio/twisted tests separately * pylint adjustments * Revert "Adapt CI to run asyncio/twisted tests separately" This reverts commit bb20997. * More CI changes * tox.ini * Separate coverage reports
1 parent 1c3f425 commit e0f3b61

14 files changed

+213
-237
lines changed

.github/workflows/tests.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
os: [ubuntu-latest, macos-latest]
12-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
12+
python-version: ["3.8", "3.9", "3.10", "3.11"]
1313

1414
steps:
1515
- uses: actions/checkout@v2
@@ -22,9 +22,12 @@ jobs:
2222
- name: Install tox
2323
run: pip install tox
2424

25-
- name: Run tests
25+
- name: Run asyncio tests
2626
run: tox -e py
2727

28+
- name: Run twisted tests
29+
run: tox -e py-twisted
30+
2831
- name: Upload coverage report
2932
run: |
3033
if [ "${{ runner.os }}" = "Linux" ]; then

.gitignore

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
.mypy_cache/
55
*.egg-info/
66
.tox/
7-
.coverage
8-
.coverage.*
9-
htmlcov/
10-
coverage.xml
117
build/
128
dist/
139
examples/*.png
1410
pip-wheel-metadata/
11+
12+
# coverage
13+
.coverage
14+
.coverage.*
15+
htmlcov/
16+
coverage.xml
17+
coverage-*.xml
18+
coverage-asyncio/
19+
coverage-twisted/

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"Development Status :: 3 - Alpha",
2323
"License :: OSI Approved :: BSD License",
2424
"Programming Language :: Python",
25-
"Programming Language :: Python :: 3.7",
2625
"Programming Language :: Python :: 3.8",
2726
"Programming Language :: Python :: 3.9",
2827
"Programming Language :: Python :: 3.10",
@@ -33,7 +32,7 @@
3332
"Topic :: Software Development :: Libraries :: Application Frameworks",
3433
"Topic :: Software Development :: Libraries :: Python Modules",
3534
],
36-
python_requires=">=3.7",
35+
python_requires=">=3.8",
3736
install_requires=[
3837
"scrapy>=2.0,!=2.4.0",
3938
"playwright>=1.15",

tests/conftest.py

-31
This file was deleted.

tests/test_utils.py

-136
This file was deleted.

tests/tests_asyncio/__init__.py

Whitespace-only changes.

tests/test_browser_contexts.py renamed to tests/tests_asyncio/test_browser_contexts.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import platform
33
import tempfile
44
from pathlib import Path
5+
from unittest import IsolatedAsyncioTestCase
56
from uuid import uuid4
67

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

247248

248-
class TestCaseMultipleContextsChromium(MixinTestCaseMultipleContexts):
249+
class TestCaseMultipleContextsChromium(IsolatedAsyncioTestCase, MixinTestCaseMultipleContexts):
249250
browser_type = "chromium"
250251

251252

252-
class TestCaseMultipleContextsFirefox(MixinTestCaseMultipleContexts):
253+
class TestCaseMultipleContextsFirefox(IsolatedAsyncioTestCase, MixinTestCaseMultipleContexts):
253254
browser_type = "firefox"
254255

255256

256257
@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
257-
class TestCaseMultipleContextsWebkit(MixinTestCaseMultipleContexts):
258+
class TestCaseMultipleContextsWebkit(IsolatedAsyncioTestCase, MixinTestCaseMultipleContexts):
258259
browser_type = "webkit"

tests/test_headers.py renamed to tests/tests_asyncio/test_headers.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import platform
3+
from unittest import IsolatedAsyncioTestCase
34

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

9394

94-
class TestProcessHeadersChromium(MixinProcessHeadersTestCase):
95+
class TestProcessHeadersChromium(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
9596
browser_type = "chromium"
9697

9798

98-
class TestProcessHeadersFirefox(MixinProcessHeadersTestCase):
99+
class TestProcessHeadersFirefox(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
99100
browser_type = "firefox"
100101

101102

102103
@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
103-
class TestProcessHeadersWebkit(MixinProcessHeadersTestCase):
104+
class TestProcessHeadersWebkit(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
104105
browser_type = "webkit"

tests/test_page_methods.py renamed to tests/tests_asyncio/test_page_methods.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import platform
33
import subprocess
44
from tempfile import NamedTemporaryFile
5+
from unittest import IsolatedAsyncioTestCase
56

67
import pytest
78
from scrapy import Spider, Request
@@ -22,19 +23,25 @@ def get_mimetype(file):
2223
).stdout.strip()
2324

2425

25-
@pytest.mark.asyncio
26-
async def test_page_methods():
27-
screenshot = PageMethod("screenshot", "foo", 123, path="/tmp/file", type="png")
28-
assert screenshot.method == "screenshot"
29-
assert screenshot.args == ("foo", 123)
30-
assert screenshot.kwargs == {"path": "/tmp/file", "type": "png"}
31-
assert screenshot.result is None
32-
assert str(screenshot) == "<PageMethod for method 'screenshot'>"
26+
class TestPageMethods(IsolatedAsyncioTestCase):
27+
@pytest.mark.asyncio
28+
async def test_page_methods(self):
29+
screenshot = PageMethod("screenshot", "foo", 123, path="/tmp/file", type="png")
30+
assert screenshot.method == "screenshot"
31+
assert screenshot.args == ("foo", 123)
32+
assert screenshot.kwargs == {"path": "/tmp/file", "type": "png"}
33+
assert screenshot.result is None
34+
assert str(screenshot) == "<PageMethod for method 'screenshot'>"
3335

3436

3537
class MixinPageMethodTestCase:
38+
@pytest.fixture(autouse=True)
39+
def inject_fixtures(self, caplog):
40+
caplog.set_level(logging.DEBUG)
41+
self._caplog = caplog
42+
3643
@pytest.mark.asyncio
37-
async def test_page_non_page_method(self, caplog):
44+
async def test_page_non_page_method(self):
3845
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
3946
with StaticMockServer() as server:
4047
req = Request(
@@ -56,10 +63,10 @@ async def test_page_non_page_method(self, caplog):
5663
"scrapy-playwright",
5764
logging.WARNING,
5865
f"Ignoring {repr(obj)}: expected PageMethod, got {repr(type(obj))}",
59-
) in caplog.record_tuples
66+
) in self._caplog.record_tuples
6067

6168
@pytest.mark.asyncio
62-
async def test_page_mixed_page_methods(self, caplog):
69+
async def test_page_mixed_page_methods(self):
6370
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
6471
with StaticMockServer() as server:
6572
req = Request(
@@ -81,7 +88,7 @@ async def test_page_mixed_page_methods(self, caplog):
8188
"scrapy-playwright",
8289
logging.WARNING,
8390
f"Ignoring {repr(does_not_exist)}: could not find method",
84-
) in caplog.record_tuples
91+
) in self._caplog.record_tuples
8592
assert not req.meta["playwright_page_methods"]["is_closed"].result
8693
assert req.meta["playwright_page_methods"]["title"].result == "Awesome site"
8794

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

180187

181-
class TestPageMethodChromium(MixinPageMethodTestCase):
188+
class TestPageMethodChromium(IsolatedAsyncioTestCase, MixinPageMethodTestCase):
182189
browser_type = "chromium"
183190

184191

185-
class TestPageMethodFirefox(MixinPageMethodTestCase):
192+
class TestPageMethodFirefox(IsolatedAsyncioTestCase, MixinPageMethodTestCase):
186193
browser_type = "firefox"
187194

188195

189196
@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
190-
class TestPageMethodWebkit(MixinPageMethodTestCase):
197+
class TestPageMethodWebkit(IsolatedAsyncioTestCase, MixinPageMethodTestCase):
191198
browser_type = "webkit"

0 commit comments

Comments
 (0)