pytest-bdd 8: Adjust Python code for multiline steps

This commit is contained in:
Florian Bruhin 2024-12-05 16:14:02 +01:00
parent f4260a6e55
commit f91dc2d52d
3 changed files with 30 additions and 31 deletions

View File

@ -350,10 +350,9 @@ def fill_clipboard(quteproc, server, what, content):
@bdd.when(bdd.parsers.re(r'I put the following lines into the '
r'(?P<what>primary selection|clipboard):\n'
r'(?P<content>.+)$', flags=re.DOTALL))
def fill_clipboard_multiline(quteproc, server, what, content):
fill_clipboard(quteproc, server, what, textwrap.dedent(content))
r'(?P<what>primary selection|clipboard):', flags=re.DOTALL))
def fill_clipboard_multiline(quteproc, server, what, docstring):
fill_clipboard(quteproc, server, what, textwrap.dedent(docstring))
@bdd.when(bdd.parsers.parse('I hint with args "{args}"'))
@ -456,20 +455,20 @@ def path_should_be_requested(server, path):
server.wait_for(verb='GET', path='/' + path)
@bdd.then(bdd.parsers.parse("The requests should be:\n{pages}"))
def list_of_requests(server, pages):
@bdd.then(bdd.parsers.parse("The requests should be:"))
def list_of_requests(server, docstring):
"""Make sure the given requests were done from the webserver."""
expected_requests = [server.ExpectedRequest('GET', '/' + path.strip())
for path in pages.split('\n')]
for path in docstring.split('\n')]
actual_requests = server.get_requests()
assert actual_requests == expected_requests
@bdd.then(bdd.parsers.parse("The unordered requests should be:\n{pages}"))
def list_of_requests_unordered(server, pages):
@bdd.then(bdd.parsers.parse("The unordered requests should be:"))
def list_of_requests_unordered(server, docstring):
"""Make sure the given requests were done (in no particular order)."""
expected_requests = [server.ExpectedRequest('GET', '/' + path.strip())
for path in pages.split('\n')]
for path in docstring.split('\n')]
actual_requests = server.get_requests()
# Requests are not hashable, we need to convert to ExpectedRequests
actual_requests = [server.ExpectedRequest.from_request(req)
@ -533,21 +532,21 @@ def javascript_message_not_logged(quteproc, message):
message='[*] {}'.format(message))
@bdd.then(bdd.parsers.parse("The session should look like:\n{expected}"))
def compare_session(quteproc, expected):
@bdd.then(bdd.parsers.parse("The session should look like:"))
def compare_session(quteproc, docstring):
"""Compare the current sessions against the given template.
partial_compare is used, which means only the keys/values listed will be
compared.
"""
quteproc.compare_session(expected)
quteproc.compare_session(docstring)
@bdd.then(
bdd.parsers.parse("The session saved with {flags} should look like:\n{expected}"))
def compare_session_flags(quteproc, flags, expected):
bdd.parsers.parse("The session saved with {flags} should look like:"))
def compare_session_flags(quteproc, flags, docstring):
"""Compare the current session saved with custom flags."""
quteproc.compare_session(expected, flags=flags)
quteproc.compare_session(docstring, flags=flags)
@bdd.then("no crash should happen")
@ -600,17 +599,17 @@ def check_not_contents_plain(quteproc, text):
assert text not in content
@bdd.then(bdd.parsers.parse('the json on the page should be:\n{text}'))
def check_contents_json(quteproc, text):
@bdd.then(bdd.parsers.parse('the json on the page should be:'))
def check_contents_json(quteproc, docstring):
"""Check the current page's content as json."""
content = quteproc.get_content().strip()
expected = json.loads(text)
expected = json.loads(docstring)
actual = json.loads(content)
assert actual == expected
@bdd.then(bdd.parsers.parse("the following tabs should be open:\n{tabs}"))
def check_open_tabs(quteproc, request, tabs):
@bdd.then(bdd.parsers.parse("the following tabs should be open:"))
def check_open_tabs(quteproc, docstring):
"""Check the list of open tabs in the session.
This is a lightweight alternative for "The session should look like: ...".
@ -620,7 +619,7 @@ def check_open_tabs(quteproc, request, tabs):
session = quteproc.get_session()
active_suffix = ' (active)'
pinned_suffix = ' (pinned)'
tabs = tabs.splitlines()
tabs = docstring.splitlines()
assert len(session['windows']) == 1
assert len(session['windows'][0]['tabs']) == len(tabs)
@ -670,9 +669,9 @@ def clipboard_contains(quteproc, server, what, content):
what, json.dumps(expected)))
@bdd.then(bdd.parsers.parse('the clipboard should contain:\n{content}'))
def clipboard_contains_multiline(quteproc, server, content):
expected = textwrap.dedent(content).replace('(port)', str(server.port))
@bdd.then(bdd.parsers.parse('the clipboard should contain:'))
def clipboard_contains_multiline(quteproc, server, docstring):
expected = textwrap.dedent(docstring).replace('(port)', str(server.port))
quteproc.wait_for(message='Setting fake clipboard: {}'.format(
json.dumps(expected)))

View File

@ -34,8 +34,8 @@ def check_query(quteproc, name, value):
assert data[name] == value
@bdd.then(bdd.parsers.parse("the history should contain:\n{expected}"))
def check_history(quteproc, server, tmpdir, expected):
@bdd.then(bdd.parsers.parse("the history should contain:"))
def check_history(quteproc, server, tmpdir, docstring):
quteproc.wait_for(message='INSERT INTO History *', category='sql')
path = tmpdir / 'history'
quteproc.send_cmd(':debug-dump-history "{}"'.format(path))
@ -46,7 +46,7 @@ def check_history(quteproc, server, tmpdir, expected):
# ignore access times, they will differ in each run
actual = '\n'.join(re.sub('^\\d+-?', '', line).strip() for line in f)
expected = expected.replace('(port)', str(server.port))
expected = docstring.replace('(port)', str(server.port))
assert actual == expected

View File

@ -15,12 +15,12 @@ def turn_on_scroll_logging(quteproc):
quteproc.turn_on_scroll_logging()
@bdd.when(bdd.parsers.parse('I have a "{name}" session file:\n{contents}'))
def create_session_file(quteproc, name, contents):
@bdd.when(bdd.parsers.parse('I have a "{name}" session file:'))
def create_session_file(quteproc, name, docstring):
filename = os.path.join(quteproc.basedir, 'data', 'sessions',
name + '.yml')
with open(filename, 'w', encoding='utf-8') as f:
f.write(contents)
f.write(docstring)
@bdd.when(bdd.parsers.parse('I replace "{pattern}" by "{replacement}" in the '