Skip to content

Commit 232e3e8

Browse files
committed
[test] Add assert_raises_process_error to assert process errors
1 parent 5c18a84 commit 232e3e8

File tree

1 file changed

+25
-0
lines changed
  • test/functional/test_framework

1 file changed

+25
-0
lines changed

test/functional/test_framework/util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313
import random
1414
import re
15+
from subprocess import CalledProcessError
1516
import time
1617

1718
from . import coverage
@@ -57,6 +58,30 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
5758
else:
5859
raise AssertionError("No exception raised")
5960

61+
def assert_raises_process_error(returncode, output, fun, *args, **kwds):
62+
"""Execute a process and asserts the process return code and output.
63+
64+
Calls function `fun` with arguments `args` and `kwds`. Catches a CalledProcessError
65+
and verifies that the return code and output are as expected. Throws AssertionError if
66+
no CalledProcessError was raised or if the return code and output are not as expected.
67+
68+
Args:
69+
returncode (int): the process return code.
70+
output (string): [a substring of] the process output.
71+
fun (function): the function to call. This should execute a process.
72+
args*: positional arguments for the function.
73+
kwds**: named arguments for the function.
74+
"""
75+
try:
76+
fun(*args, **kwds)
77+
except CalledProcessError as e:
78+
if returncode != e.returncode:
79+
raise AssertionError("Unexpected returncode %i" % e.returncode)
80+
if output not in e.output:
81+
raise AssertionError("Expected substring not found:" + e.output)
82+
else:
83+
raise AssertionError("No exception raised")
84+
6085
def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
6186
"""Run an RPC and verify that a specific JSONRPC exception code and message is raised.
6287

0 commit comments

Comments
 (0)