Tox failed but running the test in tox-created venv passed

Anyone has ideas on why running `tox` fails but running the same test in the tox-created venv passed? Here is an example test: metrics.py ``` def get_test_value(): return {1 : 'd', 2: 'e'} def _test_func(): metrics = get_test_value().values() metrics = [info for info in metrics] return metrics ``` metrics_test.py: ``` from metrics import _test_func class TestMetrics(unittest.TestCase): def test_example(self): return_value1 = {'metric_func_name': 'a'} return_value2 = {'metric_func_name': 'b'} expect(target).get_test_value.once().and_return({ 'test1': return_value1, 'test2': return_value2, }) self.assertEqual(_test_func(), [return_value1, return_value2]) ``` This test fails half of the time because the order can change: ``` E AssertionError: Lists differ: [{'metric_func_name': 'b'}, {'... != [{'metric_func_name': 'a'}, {'... E E First differing element 0: E {'metric_func_name': 'b'} E {'metric_func_name': 'a'} E E - [{'metric_func_name': 'b'}, {'metric_func_name': 'a'}] E ? ^ ^ E E + [{'metric_func_name': 'a'}, {'metric_func_name': 'b'}] E ? ```

The order of items in a dictionary is undefined (in newer Python 3.x [3.6?] versions it was changed to always be insertion order), so you should use "sorted" in tests if the order is irrelevant during runtime or sort the items in your function if it is important that they are always the same. Regards, Florian Schulze On 29 Jan 2020, at 4:49, li.tiancheng@gmail.com wrote:
Anyone has ideas on why running `tox` fails but running the same test in the tox-created venv passed?
Here is an example test: metrics.py ``` def get_test_value(): return {1 : 'd', 2: 'e'}
def _test_func(): metrics = get_test_value().values() metrics = [info for info in metrics] return metrics ```
metrics_test.py: ``` from metrics import _test_func
class TestMetrics(unittest.TestCase): def test_example(self): return_value1 = {'metric_func_name': 'a'} return_value2 = {'metric_func_name': 'b'} expect(target).get_test_value.once().and_return({ 'test1': return_value1, 'test2': return_value2, }) self.assertEqual(_test_func(), [return_value1, return_value2]) ```
This test fails half of the time because the order can change: ``` E AssertionError: Lists differ: [{'metric_func_name': 'b'}, {'... != [{'metric_func_name': 'a'}, {'... E E First differing element 0: E {'metric_func_name': 'b'} E {'metric_func_name': 'a'} E E - [{'metric_func_name': 'b'}, {'metric_func_name': 'a'}] E ? ^ ^ E E + [{'metric_func_name': 'a'}, {'metric_func_name': 'b'}] E ? ``` _______________________________________________ tox-dev mailing list -- tox-dev@python.org To unsubscribe send an email to tox-dev-leave@python.org https://mail.python.org/mailman3/lists/tox-dev.python.org/
participants (2)
-
Florian Schulze
-
li.tiancheng@gmail.com