Generating unique ids that are not repeated in hypothesis

萝らか妹 提交于 2020-05-31 06:41:06

问题


I want to generate unique ids that aren't repeated. I tried to use st.uuids().

This is my code

class MyTest(<class that inherits from unittest.TestCase>)
  @hypothesis.seed(0)
  @hypothesis.settings(derandomize=True)
  @hypothesis.setting(use_coverage=False)
  @hypothesis.given(st.uuids())
  def test(self, test_id):
    logging.info('test_id %s', test_id)
    logging.info('self.ids %s', self.ids)
    if test_id in self.ids:
      logging.info('SHOULD NOT BE HERE')
    else:
      logging.info('HAPPY')
      self.ids.append(test_id)

However, I get 1 repeat. Any tips on how to avoid repeats?

test_id 5bc8fbbc-bde5-c099-4164-d8399f767c45

self.ids [UUID('e3e70682-c209-4cac-629f-6fbed82c07cd'),
UUID('1ee77147-c7be-894d-5073-79c36d3c338d'),
UUID('734007f9-e3b1-3a59-bac6-78e47a6e8c0f'),
UUID('97343ee7-03f6-c7c0-d787-3a75e0040c6d'),
UUID('12193edd-6aac-c6b6-8caf-d66c89075de6'),
UUID('ea37c9e0-a3f9-3fe5-0b68-afbaab69828e'),
UUID('e13a37b0-f328-3835-4a40-cfc1142acbf7'),
UUID('36a3a379-28ca-876f-6b68-1fc4502323be'),
UUID('cbf97494-2dae-a0f6-43a5-5203d7fdc239'),
UUID('94012b2d-1221-006b-266c-7dcefecc417e'),
UUID('e05fccb4-9d81-b03f-4f0c-7c859a0af299'),
UUID('c6148cd8-b446-bc4f-7fea-3b29b7b93ee5'),
UUID('cd613e30-d8f1-6adf-91b7-584a2265b1f5'),
UUID('d95bafc8-f2a4-d27b-dcf4-bb99f4bea973'),
UUID('5af6e118-6344-2432-9707-6fb276cfc8bf'),
UUID('c1a4bbe2-f5bc-beed-3a7c-be792b90ac94'),
UUID('01457085-dc53-6b6a-b47f-3aefd7768cc6'),
UUID('6beb5a8d-f4b1-3663-a3e3-c900e848b602'),
UUID('21636369-8b52-9b4a-97b7-50923ceb3ffd'),
UUID('b8a1abcd-1a69-16c7-4da4-f9fc3c6da5d7'),
UUID('5bc8fbbc-bde5-c099-4164-d8399f767c45')]

SHOULD NOT BE HERE

It's weird cause that I can see that id in the log statements just above this one.

I wonder if the @cacheable or @defines_strategy_with_reusable_values has anything to do with it https://github.com/HypothesisWorks/hypothesis/blob/cedbafe52934e5f710be41c51044388ef3047850/hypothesis-python/src/hypothesis/strategies.py#L1868


回答1:


Unique IDs are not repeated within a test. However, Hypothesis may try to pass any particular value as an input in many individual iterations of the test - without this, it would be impossible to detect flakiness and many common errors would be much more difficult to find.

You should ensure that your tests do not set or depend on any external mutable state, or use the setup_example and teardown_example methods to clean up appropriately.

This is a fundamental invariant for shrinking as well as example generation.



来源:https://stackoverflow.com/questions/50337248/generating-unique-ids-that-are-not-repeated-in-hypothesis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!