aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Skultety <eskultet@redhat.com>2023-03-29 14:48:11 +0200
committerDaniel P. Berrangé <berrange@redhat.com>2023-03-30 08:00:42 +0000
commita00092955f51b621ce4cf3267655043d133dfe47 (patch)
treec7584ba98360edd9382a13fb4d2679e7320ea0ea
parent65466bf2ffe5012e585d8b0f234d54360ecab772 (diff)
downloadlibvirt-ci-a00092955f51b621ce4cf3267655043d133dfe47.zip
libvirt-ci-a00092955f51b621ce4cf3267655043d133dfe47.tar.gz
libvirt-ci-a00092955f51b621ce4cf3267655043d133dfe47.tar.bz2
tests: utils: Introduce a module-private method _assert_equal
So, this function will eventually replace the existing assert_equal function. However, unlike the original, this one is not to be used directly and instead will be wrapped by a global fixture introduced in a future patch. The reason for that is simple. To give the developer the most convenient output possible, we're going to dump both the actual and expected outputs to the disk in order to aid them with debugging problems. Where do we dump the files though? The natural answer is "wherever pytest would create them". Indeed pytest has the 'tmp_path_factory' fixture which can create directories on demand, but what is the name for such a directory? Pytest has another fixture for that - request. And now every single test has to depend on 2 fixtures to get the test's name and to create a temporary directory using that name to dump data in there on failure. A test writer should not be concerned by this and they should only ever care about comparing the actual and expected outputs, that's it. Signed-off-by: Erik Skultety <eskultet@redhat.com>
-rw-r--r--tests/test_utils/utils.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/test_utils/utils.py b/tests/test_utils/utils.py
index 1afa152..c430249 100644
--- a/tests/test_utils/utils.py
+++ b/tests/test_utils/utils.py
@@ -108,6 +108,44 @@ class DiffOperand:
tofile="expected",))
+def _assert_equal(actual, expected, test_tmp_dir):
+ # create a diff operand for 'actual' now so that we can have a string to
+ # work with if we need to regenerate the output
+ actual = DiffOperand(actual)
+
+ if pytest.custom_args["regenerate_output"] and \
+ isinstance(expected, Path):
+
+ # Make sure the target directory exists, since creating the
+ # output file would fail otherwise
+ expected.parent.mkdir(parents=True, exist_ok=True)
+ with open(expected, "w") as fd:
+ fd.write(str(actual))
+
+ # create a diff operand for 'expected' late so that we can consume the
+ # updated output based on '--regenerate-output'
+ expected = DiffOperand(expected)
+ diff = actual.diff(expected)
+ if diff.empty():
+ return
+
+ # pytest doesn't provide any flexibility over how and when tmp directories
+ # are created, so we'll take care of it ourselves, but it needs to tell us
+ # where the directory should be created and what its name should be
+ test_tmp_dir.mkdir(parents=True, exist_ok=True)
+ actual_p = Path(test_tmp_dir, "actual")
+ expected_p = Path(test_tmp_dir, "expected")
+ with open(actual_p, "w") as actual_f, open(expected_p, "w") as expected_f:
+ actual_f.write(str(actual))
+ expected_f.write(str(expected))
+
+ raise AssertionError(
+ f"Actual and expected outputs differ"
+ f"\n{diff}\n\n"
+ f"Full output dumps available at '{test_tmp_dir}'"
+ )
+
+
def assert_equal(actual, expected, indices):
if not isinstance(actual, type(expected)):
raise AssertionError(format_err_msg(indices, f"expected {type(expected)}, got {type(actual)}"))