diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 294e886..3180c9b 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -34,5 +34,5 @@ jobs: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | - pip install pytest + pip install pytest py4j pytest diff --git a/common/testresult.py b/common/testresult.py index 17bdc4c..e21767e 100644 --- a/common/testresult.py +++ b/common/testresult.py @@ -3,9 +3,13 @@ Licensed under the MIT license. """ -from .pickleserializable import PickleSerializable -import pickle import base64 +import pickle + +from py4j.protocol import Py4JJavaError + +from .pickleserializable import PickleSerializable + def get_test_results(): return TestResults() @@ -30,6 +34,9 @@ def append(self, testresult): self.total_execution_time = total_execution_time def serialize(self): + for i in self.results: + if isinstance(i.exception, Py4JJavaError): + i.exception = Exception(str(i.exception)) bin_data = pickle.dumps(self) return str(base64.encodebytes(bin_data), "utf-8") diff --git a/tests/nutter/test_testresult.py b/tests/nutter/test_testresult.py index cdf4d65..bd4b29e 100644 --- a/tests/nutter/test_testresult.py +++ b/tests/nutter/test_testresult.py @@ -3,11 +3,15 @@ Licensed under the MIT license. """ -import pytest +import base64 import json -from common.testresult import TestResults, TestResult import pickle -import base64 + +import mock +import pytest +from common.testresult import TestResult, TestResults +from py4j.protocol import Py4JError, Py4JJavaError + def test__testresults_append__type_not_testresult__throws_error(): # Arrange @@ -74,6 +78,21 @@ def test__deserialize__invalid_pickle_data__throws_Exception(): with pytest.raises(Exception): test_results.deserialize(invalid_pickle) +def test__deserialize__p4jjavaerror__is_serializable_and_deserializable(): + # Arrange + test_results = TestResults() + + py4j_exception = get_mock_py4j_error_exception(get_mock_gateway_client(), mock_target_id="o123") + + test_results.append(TestResult("Test Name", True, 1, [], py4j_exception)) + + with mock.patch('py4j.protocol.get_return_value') as mock_get_return_value: + mock_get_return_value.return_value = 'foo' + serialized_data = test_results.serialize() + deserialized_data = TestResults().deserialize(serialized_data) + + assert test_results == deserialized_data + def test__eq__test_results_equal_but_not_same_ref__are_equal(): # Arrange @@ -139,3 +158,25 @@ def test__deserialize__data_is_base64_str__can_deserialize(): test_results_from_data = TestResults().deserialize(serialized_str) assert test_results == test_results_from_data + + +def get_mock_gateway_client(): + mock_client = mock.Mock() + mock_client.send_command.return_value = "0" + mock_client.converters = [] + mock_client.is_connected.return_value = True + mock_client.deque = mock.Mock() + return mock_client + + +def get_mock_java_object(mock_client, mock_target_id): + mock_java_object = mock.Mock() + mock_java_object._target_id = mock_target_id + mock_java_object._gateway_client = mock_client + return mock_java_object + + +def get_mock_py4j_error_exception(mock_client, mock_target_id): + mock_java_object = get_mock_java_object(mock_client, mock_target_id) + mock_errmsg = "An error occurred while calling {}.load.".format(mock_target_id) + return Py4JJavaError(mock_errmsg, java_exception=mock_java_object)