Skip to content

Commit c16a477

Browse files
committed
Use f-strings
1 parent fe69874 commit c16a477

2 files changed

Lines changed: 38 additions & 67 deletions

File tree

pytest_rerunfailures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def show_rerun(terminalreporter, lines):
303303
if rerun:
304304
for rep in rerun:
305305
pos = rep.nodeid
306-
lines.append("RERUN {}".format(pos))
306+
lines.append(f"RERUN {pos}")
307307

308308

309309
if not PYTEST_GTE_61:

test_pytest_rerunfailures.py

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414

1515

1616
def temporary_failure(count=1):
17-
return """
17+
return f"""
1818
import py
1919
path = py.path.local(__file__).dirpath().ensure('test.res')
2020
count = path.read() or 1
21-
if int(count) <= {0}:
21+
if int(count) <= {count}:
2222
path.write(int(count) + 1)
23-
raise Exception('Failure: {{0}}'.format(count))""".format(
24-
count
25-
)
23+
raise Exception('Failure: {{0}}'.format(count))"""
2624

2725

2826
def check_outcome_field(outcomes, field_name, expected_value):
2927
field_value = outcomes.get(field_name, 0)
30-
assert (
31-
field_value == expected_value
32-
), "outcomes.{} has unexpected value. Expected '{}' but got '{}'".format(
33-
field_name, expected_value, field_value
28+
assert field_value == expected_value, (
29+
f"outcomes.{field_name} has unexpected value. "
30+
f"Expected '{expected_value}' but got '{field_value}'"
3431
)
3532

3633

@@ -61,14 +58,12 @@ def test_no_rerun_on_pass(testdir):
6158
def test_no_rerun_on_skipif_mark(testdir):
6259
reason = str(random.random())
6360
testdir.makepyfile(
64-
"""
61+
f"""
6562
import pytest
66-
@pytest.mark.skipif(reason='{}')
63+
@pytest.mark.skipif(reason='{reason}')
6764
def test_skip():
6865
pass
69-
""".format(
70-
reason
71-
)
66+
"""
7267
)
7368
result = testdir.runpytest("--reruns", "1")
7469
assert_outcomes(result, passed=0, skipped=1)
@@ -77,13 +72,11 @@ def test_skip():
7772
def test_no_rerun_on_skip_call(testdir):
7873
reason = str(random.random())
7974
testdir.makepyfile(
80-
"""
75+
f"""
8176
import pytest
8277
def test_skip():
83-
pytest.skip('{}')
84-
""".format(
85-
reason
86-
)
78+
pytest.skip('{reason}')
79+
"""
8780
)
8881
result = testdir.runpytest("--reruns", "1")
8982
assert_outcomes(result, passed=0, skipped=1)
@@ -105,13 +98,11 @@ def test_xfail():
10598
def test_no_rerun_on_xfail_call(testdir):
10699
reason = str(random.random())
107100
testdir.makepyfile(
108-
"""
101+
f"""
109102
import pytest
110103
def test_xfail():
111-
pytest.xfail('{}')
112-
""".format(
113-
reason
114-
)
104+
pytest.xfail('{reason}')
105+
"""
115106
)
116107
result = testdir.runpytest("--reruns", "1")
117108
assert_outcomes(result, passed=0, xfailed=1)
@@ -144,11 +135,9 @@ def pytest_runtest_setup(item):
144135
def test_rerun_passes_after_temporary_setup_failure(testdir):
145136
testdir.makepyfile("def test_pass(): pass")
146137
testdir.makeconftest(
147-
"""
138+
f"""
148139
def pytest_runtest_setup(item):
149-
{}""".format(
150-
temporary_failure()
151-
)
140+
{temporary_failure()}"""
152141
)
153142
result = testdir.runpytest("--reruns", "1", "-r", "R")
154143
assert_outcomes(result, passed=1, rerun=1)
@@ -162,65 +151,55 @@ def test_rerun_fails_after_consistent_test_failure(testdir):
162151

163152
def test_rerun_passes_after_temporary_test_failure(testdir):
164153
testdir.makepyfile(
165-
"""
154+
f"""
166155
def test_pass():
167-
{}""".format(
168-
temporary_failure()
169-
)
156+
{temporary_failure()}"""
170157
)
171158
result = testdir.runpytest("--reruns", "1", "-r", "R")
172159
assert_outcomes(result, passed=1, rerun=1)
173160

174161

175162
def test_rerun_passes_after_temporary_test_failure_with_flaky_mark(testdir):
176163
testdir.makepyfile(
177-
"""
164+
f"""
178165
import pytest
179166
@pytest.mark.flaky(reruns=2)
180167
def test_pass():
181-
{}""".format(
182-
temporary_failure(2)
183-
)
168+
{temporary_failure(2)}"""
184169
)
185170
result = testdir.runpytest("-r", "R")
186171
assert_outcomes(result, passed=1, rerun=2)
187172

188173

189174
def test_reruns_if_flaky_mark_is_called_without_options(testdir):
190175
testdir.makepyfile(
191-
"""
176+
f"""
192177
import pytest
193178
@pytest.mark.flaky()
194179
def test_pass():
195-
{}""".format(
196-
temporary_failure(1)
197-
)
180+
{temporary_failure(1)}"""
198181
)
199182
result = testdir.runpytest("-r", "R")
200183
assert_outcomes(result, passed=1, rerun=1)
201184

202185

203186
def test_reruns_if_flaky_mark_is_called_with_positional_argument(testdir):
204187
testdir.makepyfile(
205-
"""
188+
f"""
206189
import pytest
207190
@pytest.mark.flaky(2)
208191
def test_pass():
209-
{}""".format(
210-
temporary_failure(2)
211-
)
192+
{temporary_failure(2)}"""
212193
)
213194
result = testdir.runpytest("-r", "R")
214195
assert_outcomes(result, passed=1, rerun=2)
215196

216197

217198
def test_no_extra_test_summary_for_reruns_by_default(testdir):
218199
testdir.makepyfile(
219-
"""
200+
f"""
220201
def test_pass():
221-
{}""".format(
222-
temporary_failure()
223-
)
202+
{temporary_failure()}"""
224203
)
225204
result = testdir.runpytest("--reruns", "1")
226205
assert "RERUN" not in result.stdout.str()
@@ -229,11 +208,9 @@ def test_pass():
229208

230209
def test_extra_test_summary_for_reruns(testdir):
231210
testdir.makepyfile(
232-
"""
211+
f"""
233212
def test_pass():
234-
{}""".format(
235-
temporary_failure()
236-
)
213+
{temporary_failure()}"""
237214
)
238215
result = testdir.runpytest("--reruns", "1", "-r", "R")
239216
result.stdout.fnmatch_lines_random(["RERUN test_*:*"])
@@ -242,11 +219,9 @@ def test_pass():
242219

243220
def test_verbose(testdir):
244221
testdir.makepyfile(
245-
"""
222+
f"""
246223
def test_pass():
247-
{}""".format(
248-
temporary_failure()
249-
)
224+
{temporary_failure()}"""
250225
)
251226
result = testdir.runpytest("--reruns", "1", "-v")
252227
result.stdout.fnmatch_lines_random(["test_*:* RERUN*"])
@@ -323,14 +298,12 @@ def test_fail():
323298
@pytest.mark.parametrize("delay_time", [-1, 0, 0.0, 1, 2.5])
324299
def test_reruns_with_delay_marker(testdir, delay_time):
325300
testdir.makepyfile(
326-
"""
301+
f"""
327302
import pytest
328303
329-
@pytest.mark.flaky(reruns=2, reruns_delay={})
304+
@pytest.mark.flaky(reruns=2, reruns_delay={delay_time})
330305
def test_fail_two():
331-
assert False""".format(
332-
delay_time
333-
)
306+
assert False"""
334307
)
335308

336309
time.sleep = mock.MagicMock()
@@ -498,12 +471,10 @@ def test_pytest_runtest_logfinish_is_called(testdir):
498471
hook_message = "Message from pytest_runtest_logfinish hook"
499472
testdir.makepyfile("def test_pass(): pass")
500473
testdir.makeconftest(
501-
r"""
474+
fr"""
502475
def pytest_runtest_logfinish(nodeid, location):
503-
print("\n{}\n")
504-
""".format(
505-
hook_message
506-
)
476+
print("\n{hook_message}\n")
477+
"""
507478
)
508479
result = testdir.runpytest("--reruns", "1", "-s")
509480
result.stdout.fnmatch_lines(hook_message)

0 commit comments

Comments
 (0)