From a0a436fbb4a927bddff1bc68c376d3cb638c0c43 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Fri, 1 Oct 2021 19:13:55 +0200 Subject: [PATCH] Add offline mode to skip tests that require an internet connection Closes https://github.com/webrecorder/warcio/issues/133. --- pytest.ini | 4 ++++ test/conftest.py | 16 ++++++++++++++++ test/test_capture_http.py | 4 +++- test/test_capture_http_proxy.py | 5 ++++- 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 pytest.ini create mode 100644 test/conftest.py diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..a8afa633 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +addopts = --strict-markers +markers = + online: marks tests as requiring an internet connection diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 00000000..6134a4e0 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,16 @@ +import pytest + + +def pytest_addoption(parser): + parser.addoption( + "--offline", action="store_true", help="Run tests in offline mode" + ) + +@pytest.fixture +def offline(request): + return request.config.getoption("--offline") + +@pytest.fixture(autouse=True) +def skip_if_offline(request, offline): + if offline and request.node.get_closest_marker('online'): + pytest.skip('requires internet connection') diff --git a/test/test_capture_http.py b/test/test_capture_http.py index 41274d87..8aabbdd9 100644 --- a/test/test_capture_http.py +++ b/test/test_capture_http.py @@ -5,7 +5,7 @@ # must be imported before 'requests' from warcio.capture_http import capture_http -from pytest import raises +from pytest import raises, mark import requests import json @@ -147,6 +147,7 @@ def nop_filter(request, response, recorder): data = request.content_stream().read().decode('utf-8') assert data == 'somedatatopost' + @mark.online() def test_post_chunked(self): warc_writer = BufferWARCWriter(gzip=False) @@ -268,6 +269,7 @@ def test_warc_1_1(self): os.remove(full_path) + @mark.online() def test_remote(self): with capture_http(warc_version='1.1', gzip=True) as writer: requests.get('http://example.com/') diff --git a/test/test_capture_http_proxy.py b/test/test_capture_http_proxy.py index fba301d8..b894f513 100644 --- a/test/test_capture_http_proxy.py +++ b/test/test_capture_http_proxy.py @@ -7,7 +7,7 @@ import requests from warcio.archiveiterator import ArchiveIterator -from pytest import raises +from pytest import raises, mark # ================================================================== @@ -44,6 +44,7 @@ def run(): thread.start() time.sleep(0.1) + @mark.online() def test_capture_http_proxy(self): with capture_http() as warc_writer: res = requests.get("http://example.com/test", proxies=self.proxies, verify=False) @@ -63,6 +64,7 @@ def test_capture_http_proxy(self): with raises(StopIteration): assert next(ai) + @mark.online() def test_capture_https_proxy(self): with capture_http() as warc_writer: res = requests.get("https://example.com/test", proxies=self.proxies, verify=False) @@ -109,6 +111,7 @@ def test_capture_https_proxy(self): with raises(StopIteration): assert next(ai) + @mark.online() def test_capture_https_proxy_same_session(self): sesh = requests.session() with capture_http() as warc_writer: