From c8ec3b72cacd1d9d2821bd62127922b01b70c0e2 Mon Sep 17 00:00:00 2001 From: Volker Braun Date: Mon, 4 Mar 2024 13:49:47 +0100 Subject: [PATCH] Prevent PytestCollectionWarning for TestRequest, TestResponse Prevents mildly annoying warnings in pytest of the form: PytestCollectionWarning: cannot collect test class 'TestResponse' because it has a __init__ constructor (from: test/test_foo.py) class TestResponse(webob.Response): This has already been fixed for TestApp in 03395151089dd58170f9542c99dbee6f20eeb090, but TestRequest and TestResponse were missed. But pytest will try to collect all classes whose name starts with "Test". A quick grep confirms that these three are the only webtest classes starting with "Test". --- tests/test_app.py | 6 ++++++ tests/test_response.py | 3 +++ webtest/app.py | 4 ++++ webtest/response.py | 3 +++ 4 files changed, 16 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index 9636b75..6ab109a 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -698,3 +698,9 @@ def test_xhr_param_change_headers(self): resp.charset = 'ascii' self.assertIn('HTTP_X_REQUESTED_WITH: XMLHttpRequest', resp.text) + + +class TestRequest(unittest.TestCase): + + def test_pytest_collection_disabled(self): + self.assertFalse(webtest.TestRequest.__test__) diff --git a/tests/test_response.py b/tests/test_response.py index aa1cb1d..5504173 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -492,3 +492,6 @@ def test_maybe_follow_twice(self): def test_maybe_follow_infinite(self): app = self.get_redirects_app(100000) self.assertRaises(AssertionError, app.get('/').maybe_follow) + + def test_pytest_collection_disabled(self): + self.assertFalse(webtest.TestResponse.__test__) diff --git a/webtest/app.py b/webtest/app.py index 9e3155e..9304288 100644 --- a/webtest/app.py +++ b/webtest/app.py @@ -76,6 +76,10 @@ def set_ok_domain(self, cookie, request): class TestRequest(webob.BaseRequest): """A subclass of webob.Request""" + + # Tell pytest not to collect this class as tests + __test__ = False + ResponseClass = TestResponse diff --git a/webtest/response.py b/webtest/response.py index fea35aa..c69cad7 100644 --- a/webtest/response.py +++ b/webtest/response.py @@ -22,6 +22,9 @@ class TestResponse(webob.Response): _forms_indexed = None parser_features = 'html.parser' + # Tell pytest not to collect this class as tests + __test__ = False + @property def forms(self): """