diff --git a/ddt.py b/ddt.py index b8af6fa..f1d03c5 100644 --- a/ddt.py +++ b/ddt.py @@ -2,6 +2,7 @@ import inspect import json import os import re +import sys from functools import wraps __version__ = '0.8.0' @@ -70,7 +71,22 @@ def mk_test_name(name, value, index=0): string representation of the value, and convert the result into a valid python identifier by replacing extraneous characters with ``_``. + If hash randomization is enabled (a feature available since 2.7.3 + and enabled by default since 3.3) and a non-scalar value is passed + this will omit the name argument by default. Set `PYTHONHASHSEED` + to a fixed value before running tests in these cases to get the + names back consistently. + """ + + print(sys.flags) + + if sys.hexversion >= 0x02070300 and \ + sys.flags.hash_randomization and \ + 'PYTHONHASHSEED' not in os.environ and \ + not isinstance(value, (type(None), str, int, float)): + return "{0}_{1}".format(name, index + 1) + try: value = str(value) except UnicodeEncodeError: diff --git a/docs/example.rst b/docs/example.rst index 08bee3c..c7addb4 100644 --- a/docs/example.rst +++ b/docs/example.rst @@ -42,3 +42,16 @@ multiplied. DDT will try to give the new test cases meaningful names by converting the data values to valid python identifiers. + + +.. note:: + + Python 2.7.3 introduced *hash randomization* which is by default + enabled on Python 3.3 and later. DDT's default mechanism to + generate meaningful test names will **not** use the test data value + as part of the name for complex types if hash randomization is + enabled. + + You can disable hash randomization by setting the + ``PYTHONHASHSEED`` environment variable to a fixed value before + running tests (``export PYTHONHASHSEED=1`` for example). diff --git a/test/test_functional.py b/test/test_functional.py index d6ff53f..912eae0 100644 --- a/test/test_functional.py +++ b/test/test_functional.py @@ -1,5 +1,6 @@ import os import json +import sys import six @@ -238,7 +239,10 @@ def test_ddt_data_unicode(): assert_is_not_none(getattr(mytest, 'test_hello_1_ascii')) assert_is_not_none(getattr(mytest, 'test_hello_2_non_ascii__')) - assert_is_not_none(getattr(mytest, 'test_hello_3________data__')) + if sys.hexversion >= 0x03030300 and 'PYTHONHASHSEED' not in os.environ: + assert_is_not_none(getattr(mytest, 'test_hello_3')) + else: + assert_is_not_none(getattr(mytest, 'test_hello_3________data__')) def test_feed_data_with_invalid_identifier():