Skip to content

Commit 91c8968

Browse files
committed
Clean up pytest style a bit.
1 parent c1df53c commit 91c8968

File tree

10 files changed

+113
-139
lines changed

10 files changed

+113
-139
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ extend-ignore = E203,E501
33
extend-select = B950
44
max-complexity = 13
55
max-line-length = 88
6+
pytest-parametrize-names-type = list
7+
pytest-parametrize-values-type = list

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def lint_flake8(session: nox.Session) -> None:
298298
Lint code with flake8.
299299
300300
"""
301-
session.install("flake8", "flake8-bugbear")
301+
session.install("flake8", "flake8-bugbear", "flake8-pytest-style")
302302
session.run(f"python{session.python}", "-Im", "flake8", "--version")
303303
session.run(
304304
f"python{session.python}",

tests/async_client/test_api.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async def test_verify_key_invalid_explicit(
7979

8080
@pytest.mark.akismet_fixed_response(response_text="invalid")
8181
@pytest.mark.parametrize(
82-
"method_name,pass_args",
82+
["method_name", "pass_args"],
8383
[
8484
("comment_check", True),
8585
("submit_ham", True),
@@ -100,12 +100,10 @@ async def test_request_with_invalid_key(
100100
an invalid API key/URL.
101101
102102
"""
103+
method = getattr(akismet_async_client_fixed_response, method_name)
104+
args = akismet_common_kwargs if pass_args else {}
103105
with pytest.raises(akismet.APIKeyError):
104-
method = getattr(akismet_async_client_fixed_response, method_name)
105-
if pass_args:
106-
await method(**akismet_common_kwargs)
107-
else:
108-
await method()
106+
await method(**args)
109107

110108

111109
@pytest.mark.parametrize(

tests/async_client/test_errors.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
pytestmark = [pytest.mark.anyio, pytest.mark.async_client, pytest.mark.constructors]
1414

1515
check_all_methods = pytest.mark.parametrize(
16-
"method_name,pass_args",
16+
["method_name", "pass_args"],
1717
[
1818
("comment_check", True),
1919
("key_sites", False),
@@ -37,16 +37,18 @@
3737
@pytest.mark.parametrize(
3838
"status_code",
3939
[
40-
pytest.param(code, marks=pytest.mark.akismet_fixed_response(status_code=code))
40+
pytest.param(
41+
code.value, marks=pytest.mark.akismet_fixed_response(status_code=code)
42+
)
4143
for code in HTTPStatus
4244
if 400 <= code < 600
4345
],
44-
ids=[code for code in HTTPStatus if 400 <= code < 600],
46+
ids=[code.value for code in HTTPStatus if 400 <= code < 600],
4547
)
4648
async def test_error_status(
4749
akismet_async_client_fixed_response: akismet.AsyncClient,
4850
akismet_common_kwargs: dict,
49-
status_code: HTTPStatus,
51+
status_code: int,
5052
method_name: str,
5153
pass_args: bool,
5254
):
@@ -55,15 +57,13 @@ async def test_error_status(
5557
code indicating an error.
5658
5759
"""
60+
method = getattr(akismet_async_client_fixed_response, method_name)
61+
args = akismet_common_kwargs if pass_args else {}
5862
with pytest.raises(
5963
akismet.RequestError,
60-
match=f"Akismet responded with error status: {status_code.value}",
64+
match=f"Akismet responded with error status: {status_code}",
6165
):
62-
method = getattr(akismet_async_client_fixed_response, method_name)
63-
if pass_args:
64-
await method(**akismet_common_kwargs)
65-
else:
66-
await method()
66+
await method(**args)
6767

6868

6969
@check_all_methods
@@ -78,12 +78,10 @@ async def test_error_timeout(
7878
RequestError is raised when the request to Akismet times out.
7979
8080
"""
81+
method = getattr(akismet_async_client_exception, method_name)
82+
args = akismet_common_kwargs if pass_args else {}
8183
with pytest.raises(akismet.RequestError, match="Akismet timed out."):
82-
method = getattr(akismet_async_client_exception, method_name)
83-
if pass_args:
84-
await method(**akismet_common_kwargs)
85-
else:
86-
await method()
84+
await method(**args)
8785

8886

8987
@check_all_methods
@@ -98,12 +96,10 @@ async def test_error_other_httpx(
9896
RequestError is raised when a generic httpx request error occurs.
9997
10098
"""
99+
method = getattr(akismet_async_client_exception, method_name)
100+
args = akismet_common_kwargs if pass_args else {}
101101
with pytest.raises(akismet.RequestError, match="Error making request to Akismet."):
102-
method = getattr(akismet_async_client_exception, method_name)
103-
if pass_args:
104-
await method(**akismet_common_kwargs)
105-
else:
106-
await method()
102+
await method(**args)
107103

108104

109105
@check_all_methods
@@ -119,12 +115,10 @@ async def test_error_other(
119115
request.
120116
121117
"""
118+
method = getattr(akismet_async_client_exception, method_name)
119+
args = akismet_common_kwargs if pass_args else {}
122120
with pytest.raises(akismet.RequestError, match="Error making request to Akismet."):
123-
method = getattr(akismet_async_client_exception, method_name)
124-
if pass_args:
125-
await method(**akismet_common_kwargs)
126-
else:
127-
await method()
121+
await method(**args)
128122

129123

130124
@pytest.mark.parametrize("method_name", ["comment_check", "submit_ham", "submit_spam"])
@@ -145,7 +139,7 @@ async def test_unknown_argument(
145139

146140

147141
@pytest.mark.parametrize(
148-
"method_name,pass_args",
142+
["method_name", "pass_args"],
149143
[
150144
("comment_check", True),
151145
("submit_ham", True),
@@ -166,9 +160,7 @@ async def test_protocol_error(
166160
response.
167161
168162
"""
163+
method = getattr(akismet_async_client_fixed_response, method_name)
164+
args = akismet_common_kwargs if pass_args else {}
169165
with pytest.raises(akismet.ProtocolError):
170-
method = getattr(akismet_async_client_fixed_response, method_name)
171-
if pass_args:
172-
await method(**akismet_common_kwargs)
173-
else:
174-
await method()
166+
await method(**args)

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def akismet_sync_client(
259259
@pytest.fixture
260260
def akismet_async_client(
261261
akismet_async_class: type[akismet.AsyncClient],
262-
) -> akismet.SyncClient:
262+
) -> akismet.AsyncClient:
263263
"""
264264
Return an (async) Akismet test client.
265265

tests/end_to_end/test_async_client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def test_verify_key_invalid(akismet_bad_config: akismet.Config):
5151

5252

5353
@pytest.mark.parametrize(
54-
"method_name,pass_args",
54+
["method_name", "pass_args"],
5555
[
5656
("comment_check", True),
5757
("submit_ham", True),
@@ -73,12 +73,10 @@ async def test_request_with_invalid_key(
7373
7474
"""
7575
client = akismet.AsyncClient(config=akismet_bad_config)
76+
method = getattr(client, method_name)
77+
args = akismet_end_to_end_kwargs if pass_args else {}
7678
with pytest.raises(akismet.APIKeyError):
77-
method = getattr(client, method_name)
78-
if pass_args:
79-
await method(**akismet_end_to_end_kwargs)
80-
else:
81-
await method()
79+
await method(**args)
8280

8381

8482
async def test_comment_check_spam(

tests/end_to_end/test_sync_client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_verify_key_invalid(akismet_bad_config: akismet.Config):
5151

5252

5353
@pytest.mark.parametrize(
54-
"method_name,pass_args",
54+
["method_name", "pass_args"],
5555
[
5656
("comment_check", True),
5757
("submit_ham", True),
@@ -73,12 +73,10 @@ def test_request_with_invalid_key(
7373
7474
"""
7575
client = akismet.SyncClient(config=akismet_bad_config)
76+
method = getattr(client, method_name)
77+
args = akismet_end_to_end_kwargs if pass_args else {}
7678
with pytest.raises(akismet.APIKeyError):
77-
method = getattr(client, method_name)
78-
if pass_args:
79-
method(**akismet_end_to_end_kwargs)
80-
else:
81-
method()
79+
method(**args)
8280

8381

8482
def test_comment_check_spam(akismet_end_to_end_kwargs: dict, akismet_spam_author: str):

tests/sync_client/test_api.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_verify_key_invalid_explicit(
7979

8080
@pytest.mark.akismet_fixed_response(response_text="invalid")
8181
@pytest.mark.parametrize(
82-
"method_name,pass_args",
82+
["method_name", "pass_args"],
8383
[
8484
("comment_check", True),
8585
("submit_ham", True),
@@ -100,12 +100,10 @@ def test_request_with_invalid_key(
100100
an invalid API key/URL.
101101
102102
"""
103+
method = getattr(akismet_sync_client_fixed_response, method_name)
104+
args = akismet_common_kwargs if pass_args else {}
103105
with pytest.raises(akismet.APIKeyError):
104-
method = getattr(akismet_sync_client_fixed_response, method_name)
105-
if pass_args:
106-
method(**akismet_common_kwargs)
107-
else:
108-
method()
106+
method(**args)
109107

110108

111109
@pytest.mark.parametrize(

tests/sync_client/test_errors.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
pytestmark = [pytest.mark.errors, pytest.mark.sync_client]
1414

1515
check_all_methods = pytest.mark.parametrize(
16-
"method_name,pass_args",
16+
["method_name", "pass_args"],
1717
[
1818
("comment_check", True),
1919
("key_sites", False),
@@ -37,16 +37,18 @@
3737
@pytest.mark.parametrize(
3838
"status_code",
3939
[
40-
pytest.param(code, marks=pytest.mark.akismet_fixed_response(status_code=code))
40+
pytest.param(
41+
code.value, marks=pytest.mark.akismet_fixed_response(status_code=code)
42+
)
4143
for code in HTTPStatus
4244
if 400 <= code < 600
4345
],
44-
ids=[code for code in HTTPStatus if 400 <= code < 600],
46+
ids=[code.value for code in HTTPStatus if 400 <= code < 600],
4547
)
4648
def test_error_status(
4749
akismet_sync_client_fixed_response: akismet.SyncClient,
4850
akismet_common_kwargs: dict,
49-
status_code: HTTPStatus,
51+
status_code: int,
5052
method_name: str,
5153
pass_args: bool,
5254
):
@@ -55,15 +57,13 @@ def test_error_status(
5557
code indicating an error.
5658
5759
"""
60+
method = getattr(akismet_sync_client_fixed_response, method_name)
61+
args = akismet_common_kwargs if pass_args else {}
5862
with pytest.raises(
5963
akismet.RequestError,
60-
match=f"Akismet responded with error status: {status_code.value}",
64+
match=f"Akismet responded with error status: {status_code}",
6165
):
62-
method = getattr(akismet_sync_client_fixed_response, method_name)
63-
if pass_args:
64-
method(**akismet_common_kwargs)
65-
else:
66-
method()
66+
method(**args)
6767

6868

6969
@check_all_methods
@@ -78,12 +78,10 @@ def test_error_timeout(
7878
RequestError is raised when the request to Akismet times out.
7979
8080
"""
81+
method = getattr(akismet_sync_client_exception, method_name)
82+
args = akismet_common_kwargs if pass_args else {}
8183
with pytest.raises(akismet.RequestError, match="Akismet timed out."):
82-
method = getattr(akismet_sync_client_exception, method_name)
83-
if pass_args:
84-
method(**akismet_common_kwargs)
85-
else:
86-
method()
84+
method(**args)
8785

8886

8987
@check_all_methods
@@ -98,12 +96,10 @@ def test_error_other_httpx(
9896
RequestError is raised when a generic httpx request error occurs.
9997
10098
"""
99+
method = getattr(akismet_sync_client_exception, method_name)
100+
args = akismet_common_kwargs if pass_args else {}
101101
with pytest.raises(akismet.RequestError, match="Error making request to Akismet."):
102-
method = getattr(akismet_sync_client_exception, method_name)
103-
if pass_args:
104-
method(**akismet_common_kwargs)
105-
else:
106-
method()
102+
method(**args)
107103

108104

109105
@check_all_methods
@@ -119,12 +115,10 @@ def test_error_other(
119115
request.
120116
121117
"""
118+
method = getattr(akismet_sync_client_exception, method_name)
119+
args = akismet_common_kwargs if pass_args else {}
122120
with pytest.raises(akismet.RequestError, match="Error making request to Akismet."):
123-
method = getattr(akismet_sync_client_exception, method_name)
124-
if pass_args:
125-
method(**akismet_common_kwargs)
126-
else:
127-
method()
121+
method(**args)
128122

129123

130124
@pytest.mark.parametrize("method_name", ["comment_check", "submit_ham", "submit_spam"])
@@ -145,7 +139,7 @@ def test_unknown_argument(
145139

146140

147141
@pytest.mark.parametrize(
148-
"method_name,pass_args",
142+
["method_name", "pass_args"],
149143
[
150144
("comment_check", True),
151145
("submit_ham", True),
@@ -166,9 +160,7 @@ def test_protocol_error(
166160
response.
167161
168162
"""
163+
method = getattr(akismet_sync_client_fixed_response, method_name)
164+
args = akismet_common_kwargs if pass_args else {}
169165
with pytest.raises(akismet.ProtocolError):
170-
method = getattr(akismet_sync_client_fixed_response, method_name)
171-
if pass_args:
172-
method(**akismet_common_kwargs)
173-
else:
174-
method()
166+
method(**args)

0 commit comments

Comments
 (0)