Generating list of lists with custom value limitations with Hypothesis

后端 未结 3 1625
日久生厌
日久生厌 2021-02-20 02:03

The Story:

Currently, I have a function-under-test that expects a list of lists of integers with the following rules:

  1. number of s
3条回答
  •  一向
    一向 (楼主)
    2021-02-20 02:59

    Pretty late, but for posterity: the easiest solution is to pick dimensions, then build up from the element strategy.

    from hypothesis.strategies import composite, integers, lists
    
    @composite
    def complicated_rectangles(draw, max_N):
        list_len = draw(integers(1, max_N))
        sublist_len = draw(integers(0, 5))
        element_strat = integers(0, min(list_len, 5))
        sublist_strat = lists(
            element_strat, min_size=sublist_len, max_size=sublist_len)
        return draw(lists(
            sublist_strat, min_size=list_len, max_size=list_len))
    

提交回复
热议问题