Skip to content

Commit 954151c

Browse files
authored
Merge pull request #8092 from cmecklenborg/pytester_refactor_test_pastebin
Migrate test_pastebin.py from testdir to pytester
2 parents 64bb5f2 + eeb3afb commit 954151c

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

testing/test_pastebin.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Union
44

55
import pytest
6+
from _pytest.monkeypatch import MonkeyPatch
7+
from _pytest.pytester import Pytester
68

79

810
class TestPasteCapture:
@@ -13,28 +15,28 @@ def pastebinlist(self, monkeypatch, request) -> List[Union[str, bytes]]:
1315
monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append)
1416
return pastebinlist
1517

16-
def test_failed(self, testdir, pastebinlist):
17-
testpath = testdir.makepyfile(
18+
def test_failed(self, pytester: Pytester, pastebinlist) -> None:
19+
testpath = pytester.makepyfile(
1820
"""
1921
import pytest
20-
def test_pass():
22+
def test_pass() -> None:
2123
pass
2224
def test_fail():
2325
assert 0
2426
def test_skip():
2527
pytest.skip("")
2628
"""
2729
)
28-
reprec = testdir.inline_run(testpath, "--pastebin=failed")
30+
reprec = pytester.inline_run(testpath, "--pastebin=failed")
2931
assert len(pastebinlist) == 1
3032
s = pastebinlist[0]
3133
assert s.find("def test_fail") != -1
3234
assert reprec.countoutcomes() == [1, 1, 1]
3335

34-
def test_all(self, testdir, pastebinlist):
36+
def test_all(self, pytester: Pytester, pastebinlist) -> None:
3537
from _pytest.pytester import LineMatcher
3638

37-
testpath = testdir.makepyfile(
39+
testpath = pytester.makepyfile(
3840
"""
3941
import pytest
4042
def test_pass():
@@ -45,7 +47,7 @@ def test_skip():
4547
pytest.skip("")
4648
"""
4749
)
48-
reprec = testdir.inline_run(testpath, "--pastebin=all", "-v")
50+
reprec = pytester.inline_run(testpath, "--pastebin=all", "-v")
4951
assert reprec.countoutcomes() == [1, 1, 1]
5052
assert len(pastebinlist) == 1
5153
contents = pastebinlist[0].decode("utf-8")
@@ -59,17 +61,17 @@ def test_skip():
5961
]
6062
)
6163

62-
def test_non_ascii_paste_text(self, testdir, pastebinlist):
64+
def test_non_ascii_paste_text(self, pytester: Pytester, pastebinlist) -> None:
6365
"""Make sure that text which contains non-ascii characters is pasted
6466
correctly. See #1219.
6567
"""
66-
testdir.makepyfile(
68+
pytester.makepyfile(
6769
test_unicode="""\
6870
def test():
6971
assert '☺' == 1
7072
"""
7173
)
72-
result = testdir.runpytest("--pastebin=all")
74+
result = pytester.runpytest("--pastebin=all")
7375
expected_msg = "*assert '☺' == 1*"
7476
result.stdout.fnmatch_lines(
7577
[
@@ -87,7 +89,7 @@ def pastebin(self, request):
8789
return request.config.pluginmanager.getplugin("pastebin")
8890

8991
@pytest.fixture
90-
def mocked_urlopen_fail(self, monkeypatch):
92+
def mocked_urlopen_fail(self, monkeypatch: MonkeyPatch):
9193
"""Monkeypatch the actual urlopen call to emulate a HTTP Error 400."""
9294
calls = []
9395

@@ -102,7 +104,7 @@ def mocked(url, data):
102104
return calls
103105

104106
@pytest.fixture
105-
def mocked_urlopen_invalid(self, monkeypatch):
107+
def mocked_urlopen_invalid(self, monkeypatch: MonkeyPatch):
106108
"""Monkeypatch the actual urlopen calls done by the internal plugin
107109
function that connects to bpaste service, but return a url in an
108110
unexpected format."""
@@ -124,7 +126,7 @@ def read(self):
124126
return calls
125127

126128
@pytest.fixture
127-
def mocked_urlopen(self, monkeypatch):
129+
def mocked_urlopen(self, monkeypatch: MonkeyPatch):
128130
"""Monkeypatch the actual urlopen calls done by the internal plugin
129131
function that connects to bpaste service."""
130132
calls = []
@@ -144,20 +146,20 @@ def read(self):
144146
monkeypatch.setattr(urllib.request, "urlopen", mocked)
145147
return calls
146148

147-
def test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid):
149+
def test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid) -> None:
148150
result = pastebin.create_new_paste(b"full-paste-contents")
149151
assert (
150152
result
151153
== "bad response: invalid format ('View <a href=\"/invalid/3c0c6750bd\">raw</a>.')"
152154
)
153155
assert len(mocked_urlopen_invalid) == 1
154156

155-
def test_pastebin_http_error(self, pastebin, mocked_urlopen_fail):
157+
def test_pastebin_http_error(self, pastebin, mocked_urlopen_fail) -> None:
156158
result = pastebin.create_new_paste(b"full-paste-contents")
157159
assert result == "bad response: HTTP Error 400: Bad request"
158160
assert len(mocked_urlopen_fail) == 1
159161

160-
def test_create_new_paste(self, pastebin, mocked_urlopen):
162+
def test_create_new_paste(self, pastebin, mocked_urlopen) -> None:
161163
result = pastebin.create_new_paste(b"full-paste-contents")
162164
assert result == "https://bpaste.net/show/3c0c6750bd"
163165
assert len(mocked_urlopen) == 1
@@ -169,7 +171,7 @@ def test_create_new_paste(self, pastebin, mocked_urlopen):
169171
assert "code=full-paste-contents" in data.decode()
170172
assert "expiry=1week" in data.decode()
171173

172-
def test_create_new_paste_failure(self, pastebin, monkeypatch):
174+
def test_create_new_paste_failure(self, pastebin, monkeypatch: MonkeyPatch) -> None:
173175
import io
174176
import urllib.request
175177

0 commit comments

Comments
 (0)