繁体   English   中英

生成在假设中不重复的唯一 ID

[英]Generating unique ids that are not repeated in hypothesis

我想生成不重复的唯一 ID。 我尝试使用st.uuids()

这是我的代码

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)

但是,我重复了 1 次。 关于如何避免重复的任何提示?

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

这很奇怪,因为我可以在日志语句中看到该 id 正上方的那个。

我想知道@cacheable@defines_strategy_with_reusable_values是否与它有关https://github.com/HypothesisWorks/hypothesis/blob/cedbafe52934e5f710be41c51044388ef3047850/hypothesis-python/src/hy186pys

唯一 ID在测试中不会重复。 然而,假设可能会尝试在测试的许多单独迭代中传递任何特定值作为输入 - 没有这个,就不可能检测到片状,并且许多常见错误将更难以找到。

您应该确保您的测试不会设置或依赖于任何外部可变状态,或者使用setup_exampleteardown_example方法进行适当的清理。

这是收缩和示例生成的基本不变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM