Skip to content

Commit 72ff51c

Browse files
committed
Fixed a bunch of unit test best practices warnings from ruff PT ruleset
1 parent bab4e67 commit 72ff51c

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

plugins/ext_test/examples/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#
22
# coding=utf-8
33
# import cmd2
4+
import cmd2_ext_test
5+
46
import cmd2
57
import cmd2.py_bridge
68

7-
import cmd2_ext_test
8-
99

1010
class Example(cmd2.Cmd):
1111
"""An class to show how to use a plugin"""

plugins/ext_test/tests/test_ext_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#
22
# coding=utf-8
33

4+
import cmd2_ext_test
45
import pytest
6+
57
from cmd2 import (
68
CommandResult,
79
cmd2,
810
)
911

10-
import cmd2_ext_test
11-
1212
######
1313
#
1414
# define a class which implements a simple cmd2 application

tests/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
mock,
1717
)
1818

19-
from pytest import (
20-
fixture,
21-
)
19+
import pytest
2220

2321
import cmd2
2422
from cmd2.rl_utils import (
@@ -151,7 +149,7 @@ def run_cmd(app, cmd):
151149
return normalize(out), normalize(err)
152150

153151

154-
@fixture
152+
@pytest.fixture
155153
def base_app():
156154
return cmd2.Cmd(include_py=True, include_ipy=True)
157155

tests/test_ansi.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,12 @@ def test_clear_screen():
179179
assert ansi.clear_screen(clear_type) == f"{ansi.CSI}{clear_type}J"
180180

181181
clear_type = -1
182-
with pytest.raises(ValueError):
182+
expected_err = "clear_type must in an integer from 0 to 3"
183+
with pytest.raises(ValueError, match=expected_err):
183184
ansi.clear_screen(clear_type)
184185

185186
clear_type = 4
186-
with pytest.raises(ValueError):
187+
with pytest.raises(ValueError, match=expected_err):
187188
ansi.clear_screen(clear_type)
188189

189190

@@ -192,11 +193,12 @@ def test_clear_line():
192193
assert ansi.clear_line(clear_type) == f"{ansi.CSI}{clear_type}K"
193194

194195
clear_type = -1
195-
with pytest.raises(ValueError):
196+
expected_err = "clear_type must in an integer from 0 to 2"
197+
with pytest.raises(ValueError, match=expected_err):
196198
ansi.clear_line(clear_type)
197199

198200
clear_type = 3
199-
with pytest.raises(ValueError):
201+
with pytest.raises(ValueError, match=expected_err):
200202
ansi.clear_line(clear_type)
201203

202204

@@ -247,9 +249,10 @@ def test_rgb_bounds(r, g, b, valid):
247249
ansi.RgbFg(r, g, b)
248250
ansi.RgbBg(r, g, b)
249251
else:
250-
with pytest.raises(ValueError):
252+
expected_err = "RGB values must be integers in the range of 0 to 255"
253+
with pytest.raises(ValueError, match=expected_err):
251254
ansi.RgbFg(r, g, b)
252-
with pytest.raises(ValueError):
255+
with pytest.raises(ValueError, match=expected_err):
253256
ansi.RgbBg(r, g, b)
254257

255258

tests/test_argparse_custom.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,26 @@ def fake_func():
5858
)
5959
def test_apcustom_choices_callable_count(kwargs, is_valid):
6060
parser = Cmd2ArgumentParser()
61-
try:
61+
if is_valid:
6262
parser.add_argument('name', **kwargs)
63-
assert is_valid
64-
except ValueError as ex:
65-
assert not is_valid
66-
assert 'Only one of the following parameters' in str(ex)
63+
else:
64+
expected_err = 'Only one of the following parameters'
65+
with pytest.raises(ValueError, match=expected_err):
66+
parser.add_argument('name', **kwargs)
6767

6868

6969
@pytest.mark.parametrize('kwargs', [({'choices_provider': fake_func}), ({'completer': fake_func})])
7070
def test_apcustom_no_choices_callables_alongside_choices(kwargs):
71+
parser = Cmd2ArgumentParser()
7172
with pytest.raises(TypeError) as excinfo:
72-
parser = Cmd2ArgumentParser()
7373
parser.add_argument('name', choices=['my', 'choices', 'list'], **kwargs)
7474
assert 'None of the following parameters can be used alongside a choices parameter' in str(excinfo.value)
7575

7676

7777
@pytest.mark.parametrize('kwargs', [({'choices_provider': fake_func}), ({'completer': fake_func})])
7878
def test_apcustom_no_choices_callables_when_nargs_is_0(kwargs):
79+
parser = Cmd2ArgumentParser()
7980
with pytest.raises(TypeError) as excinfo:
80-
parser = Cmd2ArgumentParser()
8181
parser.add_argument('--name', action='store_true', **kwargs)
8282
assert 'None of the following parameters can be used on an action that takes no arguments' in str(excinfo.value)
8383

@@ -126,24 +126,24 @@ def test_apcustom_nargs_range_validation(cust_app):
126126
],
127127
)
128128
def test_apcustom_narg_invalid_tuples(nargs_tuple):
129-
with pytest.raises(ValueError) as excinfo:
130-
parser = Cmd2ArgumentParser()
129+
parser = Cmd2ArgumentParser()
130+
expected_err = 'Ranged values for nargs must be a tuple of 1 or 2 integers'
131+
with pytest.raises(ValueError, match=expected_err):
131132
parser.add_argument('invalid_tuple', nargs=nargs_tuple)
132-
assert 'Ranged values for nargs must be a tuple of 1 or 2 integers' in str(excinfo.value)
133133

134134

135135
def test_apcustom_narg_tuple_order():
136-
with pytest.raises(ValueError) as excinfo:
137-
parser = Cmd2ArgumentParser()
136+
parser = Cmd2ArgumentParser()
137+
expected_err = 'Invalid nargs range. The first value must be less than the second'
138+
with pytest.raises(ValueError, match=expected_err):
138139
parser.add_argument('invalid_tuple', nargs=(2, 1))
139-
assert 'Invalid nargs range. The first value must be less than the second' in str(excinfo.value)
140140

141141

142142
def test_apcustom_narg_tuple_negative():
143-
with pytest.raises(ValueError) as excinfo:
144-
parser = Cmd2ArgumentParser()
143+
parser = Cmd2ArgumentParser()
144+
expected_err = 'Negative numbers are invalid for nargs range'
145+
with pytest.raises(ValueError, match=expected_err):
145146
parser.add_argument('invalid_tuple', nargs=(-1, 1))
146-
assert 'Negative numbers are invalid for nargs range' in str(excinfo.value)
147147

148148

149149
def test_apcustom_narg_tuple_zero_base():

tests/test_cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ def test_base_shortcuts(base_app):
146146

147147

148148
def test_command_starts_with_shortcut():
149-
with pytest.raises(ValueError) as excinfo:
149+
expected_err = "Invalid command name 'help'"
150+
with pytest.raises(ValueError, match=expected_err):
150151
cmd2.Cmd(shortcuts={'help': 'fake'})
151-
assert "Invalid command name 'help'" in str(excinfo.value)
152152

153153

154154
def test_base_set(base_app):

0 commit comments

Comments
 (0)