Skip to content

Commit d4ae239

Browse files
committed
Refactor test files to replace any import pytest with from pytest import ... for improved consistency
1 parent 4987831 commit d4ae239

6 files changed

Lines changed: 44 additions & 44 deletions

File tree

src/tests/test_bools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from unittest import TestCase
1515

1616
# ## Python Third Party Imports ----
17-
import pytest
1817
from parameterized import parameterized
18+
from pytest import raises
1919

2020
# ## Local First Party Imports ----
2121
from tests.setup import name_func_nested_list
@@ -44,5 +44,5 @@ def test_strtobool(self, _expected: bool, _input: str) -> None:
4444

4545
def test_strtobool_raises(self) -> None:
4646
_input = 5
47-
with pytest.raises(ValueError):
47+
with raises(ValueError):
4848
_output: bool = strtobool(_input)

src/tests/test_checkers.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from unittest import TestCase
1616

1717
# ## Python Third Party Imports ----
18-
import pytest
1918
from parameterized import parameterized
19+
from pytest import raises
2020

2121
# ## Local First Party Imports ----
2222
from tests.setup import (
@@ -137,7 +137,7 @@ def test_is_value_of_type(
137137
assert_type(_val, _typ)
138138
assert_value_of_type(_val, _typ)
139139
else:
140-
with pytest.raises(TypeError):
140+
with raises(TypeError):
141141
assert_type(_val, _typ)
142142
assert_value_of_type(_val, _typ)
143143

@@ -187,7 +187,7 @@ def test_is_all_values_of_type(
187187
assert_all_values_of_type(_vals, _typ)
188188
assert_all_type(_vals, _typ)
189189
else:
190-
with pytest.raises(TypeError):
190+
with raises(TypeError):
191191
assert_all_values_of_type(_vals, _typ)
192192
assert_all_type(_vals, _typ)
193193

@@ -237,7 +237,7 @@ def test_is_any_values_of_type(
237237
assert_any_values_of_type(_vals, _typ)
238238
assert_any_type(_vals, _typ)
239239
else:
240-
with pytest.raises(TypeError):
240+
with raises(TypeError):
241241
assert_any_values_of_type(_vals, _typ)
242242
assert_any_type(_vals, _typ)
243243

@@ -295,27 +295,27 @@ def test_is_any_values_in_iterable(self, _nam: str, _vals: Any) -> None:
295295
assert_any_in(_val, _vals)
296296

297297
def test_raises_assert_value_of_type(self) -> None:
298-
with pytest.raises(TypeError):
298+
with raises(TypeError):
299299
assert_value_of_type(5, str)
300300
assert_value_of_type("5", int)
301301

302302
def test_raises_all_values_of_type(self) -> None:
303-
with pytest.raises(TypeError):
303+
with raises(TypeError):
304304
assert_all_values_of_type((1, 2, 3, 4, 5), str)
305305
assert_all_values_of_type(("1", "2", "3", "4", "5"), int)
306306

307307
def test_raises_assert_value_in_iterable(self) -> None:
308-
with pytest.raises(LookupError):
308+
with raises(LookupError):
309309
assert_value_in_iterable("a", (1, 2, 3))
310310
assert_value_in_iterable(1, ("a", "b", "c"))
311311

312312
def test_raises_assert_any_values_in_iterable(self) -> None:
313-
with pytest.raises(LookupError):
313+
with raises(LookupError):
314314
assert_any_values_in_iterable(("a", "b"), (1, 2, 3))
315315
assert_any_values_in_iterable((1, 2), ("a", "b", "c"))
316316

317317
def test_raises_assert_all_values_in_iterable(self) -> None:
318-
with pytest.raises(LookupError):
318+
with raises(LookupError):
319319
assert_all_values_in_iterable(("a", "d"), ("a", "b", "c"))
320320

321321

@@ -449,9 +449,9 @@ def test_assert_is_valid(self, _val: Any, _op: str, _targ: Any) -> None:
449449
name_func=name_func_flat_list,
450450
)
451451
def test_assert_not_is_valid(self, _val: Any, _op: str, _targ: Any) -> None:
452-
with pytest.raises(ValueError):
452+
with raises(ValueError):
453453
assert_is_valid(_val, _op, _targ)
454454

455455
def test_assert_is_valid_unknown_operator(self) -> None:
456-
with pytest.raises(ValueError):
456+
with raises(ValueError):
457457
assert_is_valid(5, "unknown", 10)

src/tests/test_defaults.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from unittest import TestCase
1515

1616
# ## Python Third Party Imports ----
17-
import pytest
1817
from parameterized import parameterized
18+
from pytest import raises
1919

2020
# ## Local First Party Imports ----
2121
from tests.setup import name_func_nested_list
@@ -75,9 +75,9 @@ def test_defaults_cast_strings(self, cast_type, cast_value) -> None:
7575
assert _output == _expected
7676

7777
def test_defaults_invalid_cast(self) -> None:
78-
with pytest.raises(AttributeError):
78+
with raises(AttributeError):
7979
defaults(value="next", cast="bad_type")
8080

8181
def test_defaults_all_blank(self) -> None:
82-
with pytest.raises(AttributeError):
82+
with raises(AttributeError):
8383
defaults(value=None, default=None)

src/tests/test_dictionaries.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from unittest import TestCase
1616

1717
# ## Python Third Party Imports ----
18-
import pytest
18+
from pytest import raises
1919

2020
# ## Local First Party Imports ----
2121
from toolbox_python.checkers import assert_type
@@ -113,7 +113,7 @@ def test_reverse_dict_iterables_with_duplicates(self) -> None:
113113
r"Full `output_dict` so far:\n"
114114
r"{'1': 'a', '2': 'a', '3': 'a', '4': 'b'}"
115115
)
116-
with pytest.raises(KeyError) as execinfo:
116+
with raises(KeyError) as execinfo:
117117
_output = dict_reverse_keys_and_values(_input)
118118
assert str(execinfo.value) == f'"{_expected}"'
119119

@@ -166,7 +166,7 @@ def test_dict_init(self) -> None:
166166
def test_dict_values(self) -> None:
167167
assert self.dot_dict.version == 1.0
168168
assert self.dot_dict.title == "Sample Dictionary"
169-
with pytest.raises(AttributeError):
169+
with raises(AttributeError):
170170
assert self.dot_dict.missing == "Missing Key"
171171

172172
def test_dict_nested_values(self) -> None:
@@ -204,9 +204,9 @@ def test_dict_add_new_key(self) -> None:
204204
self.dot_dict["key_two"] = "New Value"
205205
assert self.dot_dict["key_two"] == "New Value"
206206
del self.dot_dict["key_two"]
207-
with pytest.raises(KeyError):
207+
with raises(KeyError):
208208
del self.dot_dict["missing_key"]
209-
with pytest.raises(AttributeError):
209+
with raises(AttributeError):
210210
del self.dot_dict.missing_key
211211

212212
def test_convert_to_dict(self) -> None:
@@ -215,7 +215,7 @@ def test_convert_to_dict(self) -> None:
215215
assert dict_from_dotdict == self.test_dict
216216
assert dict_from_dotdict["user"]["name"] == "John"
217217
assert dict_from_dotdict["list_objects"][0]["name"] == "Item 1"
218-
with pytest.raises(AttributeError):
218+
with raises(AttributeError):
219219
dict_from_dotdict.version
220220
dict_from_dotdict.title
221221

src/tests/test_output.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
from unittest import TestCase
1919

2020
# ## Python Third Party Imports ----
21-
import pytest
2221
import requests
2322
from parameterized import parameterized
23+
from pytest import CaptureFixture, LogCaptureFixture, fixture, raises
2424
from typeguard import TypeCheckError
2525

2626
# ## Local First Party Imports ----
@@ -42,12 +42,10 @@ def setUpClass(cls) -> None:
4242
cls.message = "Message for:"
4343
cls.log: logging.Logger = logging.getLogger(__name__)
4444

45-
@pytest.fixture(autouse=True)
46-
def _pass_fixtures(
47-
self, capsys: pytest.CaptureFixture, caplog: pytest.LogCaptureFixture
48-
) -> None:
49-
self.capsys: pytest.CaptureFixture = capsys
50-
self.caplog: pytest.LogCaptureFixture = caplog
45+
@fixture(autouse=True)
46+
def _pass_fixtures(self, capsys: CaptureFixture, caplog: LogCaptureFixture) -> None:
47+
self.capsys: CaptureFixture = capsys
48+
self.caplog: LogCaptureFixture = caplog
5149
self.caplog.set_level("notset".upper())
5250

5351
def test_print_output(self) -> None:
@@ -114,14 +112,14 @@ def test_log_critical_output(self) -> None:
114112
self.caplog.clear()
115113

116114
def test_invalid_input(self) -> None:
117-
with pytest.raises(TypeCheckError):
115+
with raises(TypeCheckError):
118116
print_or_log_output(
119117
message=f"{self.message} none",
120118
print_or_log="invalid",
121119
)
122120

123121
def test_raises_type_error(self) -> None:
124-
with pytest.raises(TypeError):
122+
with raises(TypeError):
125123
print_or_log_output(
126124
message=f"{self.message} error",
127125
print_or_log="log",
@@ -130,7 +128,7 @@ def test_raises_type_error(self) -> None:
130128
)
131129

132130
def test_raises_value_error(self) -> None:
133-
with pytest.raises(ValueError):
131+
with raises(ValueError):
134132
print_or_log_output(
135133
message=f"{self.message} error",
136134
print_or_log="log",
@@ -139,7 +137,7 @@ def test_raises_value_error(self) -> None:
139137
)
140138

141139
def test_invalid_input_01(self) -> None:
142-
with pytest.raises(TypeCheckError):
140+
with raises(TypeCheckError):
143141
print_or_log_output(
144142
message=1,
145143
print_or_log="log",
@@ -148,7 +146,7 @@ def test_invalid_input_01(self) -> None:
148146
)
149147

150148
def test_invalid_input_02(self) -> None:
151-
with pytest.raises(TypeCheckError):
149+
with raises(TypeCheckError):
152150
print_or_log_output(
153151
message=self.message,
154152
print_or_log="invalid",
@@ -157,7 +155,7 @@ def test_invalid_input_02(self) -> None:
157155
)
158156

159157
def test_invalid_input_03(self) -> None:
160-
with pytest.raises(TypeCheckError):
158+
with raises(TypeCheckError):
161159
print_or_log_output(
162160
message=self.message,
163161
print_or_log="log",
@@ -166,7 +164,7 @@ def test_invalid_input_03(self) -> None:
166164
)
167165

168166
def test_invalid_input_04(self) -> None:
169-
with pytest.raises(TypeCheckError):
167+
with raises(TypeCheckError):
170168
print_or_log_output(
171169
message=self.message,
172170
print_or_log="log",
@@ -180,9 +178,9 @@ class TestListColumnsOutput(TestCase):
180178
def setUp(self) -> None:
181179
pass
182180

183-
@pytest.fixture(autouse=True)
184-
def _pass_fixtures(self, capsys: pytest.CaptureFixture) -> None:
185-
self.capsys: pytest.CaptureFixture = capsys
181+
@fixture(autouse=True)
182+
def _pass_fixtures(self, capsys: CaptureFixture) -> None:
183+
self.capsys: CaptureFixture = capsys
186184

187185
@staticmethod
188186
@lru_cache

src/tests/test_retry.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from unittest import TestCase
1616

1717
# ## Python Third Party Imports ----
18-
import pytest
18+
from pytest import CaptureFixture, LogCaptureFixture, fixture
1919

2020
# ## Local First Party Imports ----
2121
from toolbox_python.retry import retry
@@ -53,10 +53,12 @@ class TestRetryDecorator(TestCase):
5353
def setUp(self) -> None:
5454
pass
5555

56-
@pytest.fixture(autouse=True)
57-
def _pass_fixtures(self, capsys, caplog) -> None:
58-
self.capsys: pytest.CaptureFixture = capsys
59-
self.caplog: pytest.LogCaptureFixture = caplog
56+
@fixture(autouse=True)
57+
def _pass_fixtures(
58+
self, capsys: CaptureFixture[str], caplog: LogCaptureFixture
59+
) -> None:
60+
self.capsys: CaptureFixture = capsys
61+
self.caplog: LogCaptureFixture = caplog
6062
self.caplog.set_level("notset".upper())
6163

6264
@staticmethod

0 commit comments

Comments
 (0)