test_ok2/testing/test_pastebin.py

119 lines
3.6 KiB
Python
Raw Normal View History

import pytest
2019-06-03 06:32:00 +08:00
class TestPasteCapture:
@pytest.fixture
def pastebinlist(self, monkeypatch, request):
pastebinlist = []
2018-05-23 22:48:46 +08:00
plugin = request.config.pluginmanager.getplugin("pastebin")
monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append)
return pastebinlist
def test_failed(self, testdir, pastebinlist):
2018-05-23 22:48:46 +08:00
testpath = testdir.makepyfile(
"""
import pytest
def test_pass():
pass
def test_fail():
assert 0
def test_skip():
pytest.skip("")
2018-05-23 22:48:46 +08:00
"""
)
reprec = testdir.inline_run(testpath, "--paste=failed")
assert len(pastebinlist) == 1
s = pastebinlist[0]
assert s.find("def test_fail") != -1
assert reprec.countoutcomes() == [1, 1, 1]
def test_all(self, testdir, pastebinlist):
from _pytest.pytester import LineMatcher
2018-05-23 22:48:46 +08:00
testpath = testdir.makepyfile(
"""
import pytest
def test_pass():
pass
def test_fail():
assert 0
def test_skip():
pytest.skip("")
2018-05-23 22:48:46 +08:00
"""
)
reprec = testdir.inline_run(testpath, "--pastebin=all", "-v")
assert reprec.countoutcomes() == [1, 1, 1]
assert len(pastebinlist) == 1
2018-05-23 22:48:46 +08:00
contents = pastebinlist[0].decode("utf-8")
matcher = LineMatcher(contents.splitlines())
2018-05-23 22:48:46 +08:00
matcher.fnmatch_lines(
[
"*test_pass PASSED*",
"*test_fail FAILED*",
"*test_skip SKIPPED*",
"*== 1 failed, 1 passed, 1 skipped in *",
]
)
def test_non_ascii_paste_text(self, testdir):
"""Make sure that text which contains non-ascii characters is pasted
correctly. See #1219.
"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
test_unicode="""
# -*- coding: utf-8 -*-
def test():
assert '' == 1
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest("--pastebin=all")
expected_msg = "*assert '' == 1*"
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
[
expected_msg,
"*== 1 failed in *",
"*Sending information to Paste Service*",
]
)
2019-06-03 06:32:00 +08:00
class TestPaste:
@pytest.fixture
def pastebin(self, request):
2018-05-23 22:48:46 +08:00
return request.config.pluginmanager.getplugin("pastebin")
@pytest.fixture
def mocked_urlopen(self, monkeypatch):
"""
monkeypatch the actual urlopen calls done by the internal plugin
function that connects to bpaste service.
"""
calls = []
def mocked(url, data):
calls.append((url, data))
2019-06-03 06:32:00 +08:00
class DummyFile:
def read(self):
# part of html of a normal response
return b'View <a href="/raw/3c0c6750bd">raw</a>.'
2018-05-23 22:48:46 +08:00
return DummyFile()
import urllib.request
2018-05-23 22:48:46 +08:00
monkeypatch.setattr(urllib.request, "urlopen", mocked)
return calls
def test_create_new_paste(self, pastebin, mocked_urlopen):
2018-05-23 22:48:46 +08:00
result = pastebin.create_new_paste(b"full-paste-contents")
assert result == "https://bpaste.net/show/3c0c6750bd"
assert len(mocked_urlopen) == 1
url, data = mocked_urlopen[0]
assert type(data) is bytes
lexer = "python3"
2018-05-23 22:48:46 +08:00
assert url == "https://bpaste.net"
assert "lexer=%s" % lexer in data.decode()
assert "code=full-paste-contents" in data.decode()
assert "expiry=1week" in data.decode()