diff --git a/NEWS.rst b/NEWS.rst index ed56311..3112e05 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -6,7 +6,7 @@ of every change, see the Git log. Latest ------ -* tbd +* Minor: Added access to the user_error on the exception object 1.5.0 ----- diff --git a/src/pytest_datarecorder/datarecorder.py b/src/pytest_datarecorder/datarecorder.py index c60dff3..33eadac 100644 --- a/src/pytest_datarecorder/datarecorder.py +++ b/src/pytest_datarecorder/datarecorder.py @@ -218,6 +218,9 @@ def __init__( if user_error: result += f"{user_error}\n" + # Make sure we can ass only the user error if we want + self.user_error = user_error + super(DataRecorderError, self).__init__(result) diff --git a/test/test_datarecorder.py b/test/test_datarecorder.py index e56112f..c46590d 100644 --- a/test/test_datarecorder.py +++ b/test/test_datarecorder.py @@ -146,8 +146,23 @@ def on_mismatch(mismatch_data, recording_data, mismatch_dir, mismatch_context): mismatch_context="scatter", ) + assert e.user_error is True + assert "Data mismatch at index [0, 3]" in str(e.value) assert mismatch_index == [0, 3] # Check that the mismatch directory contains the files assert mismatch_dir.contains_file("scatter.json") + + def on_mismatch(mismatch_data, recording_data, mismatch_dir, mismatch_context): + return "Data mismatch" + + with pytest.raises(pytest_datarecorder.datarecorder.DataRecorderError) as e: + datarecorder.record_data( + data=[5, 2, 3, 1, 5], + recording_file=recording_file, + mismatch_callback=on_mismatch, + mismatch_dir=mismatch_dir.path(), + ) + + assert e.user_error == "Data mismatch"