@given(st.tuples(st.integers(), st.integers()))
Testing Patterns

Meaning

Generates arbitrary tuples of two integers for property-based testing using the Hypothesis library.

Primary Function

Decorator that supplies a test function with randomly generated tuples of two integers.

Communicative Purpose

Indicates that the decorated test function will receive arbitrary integer pairs for property-based validation.

Pattern

@given(st.tuples(X, Y)) where X and Y are hypothesis strategies; here X=st.integers(), Y=st.integers()

Core Structure

@given(st.tuples(strategy1, strategy2))

Função primária

Decorator that supplies a test function with randomly generated tuples of two integers.

Propósito comunicativo

Indicates that the decorated test function will receive arbitrary integer pairs for property-based validation.

Situações de gatilho

When writing property-based tests with Hypothesis that require random pairs of integers as input.

Contextos

Property-based testing, Hypothesis library, automated test generation, quickcheck-style testing.

Padrão

@given(st.tuples(X, Y)) where X and Y are hypothesis strategies; here X=st.integers(), Y=st.integers()

Estrutura central

@given(st.tuples(strategy1, strategy2))

Slots de substituição

strategy1, strategy2: any hypothesis strategy (e.g., st.integers(), st.floats(), st.text(), st.lists(...))

Colocados típicos

  • @given
  • st
  • tuples
  • integers
  • hypothesis
  • test
  • given

Substituições comuns

  • st.floats()
  • st.text()
  • st.booleans()
  • st.lists(st.integers())
  • st.sets(st.integers())

Erros comuns

forgetting to import hypothesis or st, using non-hashable mutable objects in tuples for set/dict keys, missing parentheses, applying @given to non-test functions, confusing with pytest.parametrize.

Similar / contraste

@given(st.lists(st.integers())) for lists of ints, @given(st.tuples(st.booleans(), st.integers())) for mixed-type tuples, @given(st.tuples()) for empty tuple.

Interferências

may be confused with unittest's @parameterized or pytest's @parametrize; ensure hypothesis is installed and imported; avoid mutable default arguments in test functions.

Família do chunk

  • hypothesis_given_tuple_strategy

Nuance

generates integers across the full range including negatives and large values; employs shrinking to simplify failing examples.

Efeito pragmático

signals use of generative property-based testing, emphasizing generic properties over specific examples.

Dica de memória

@given(st.tuples(st.integers(), st.integers())) – think 'given two ints tuple'.

Nota

The strategy uses hypothesis's default integer range which includes very large integers; shrinking simplifies counterexamples on failure.

Upgrade path

progress to more complex strategies like tuples of lists, recursive strategies, or using @given with multiple arguments for multiple parameters.

Tipo de construção: decorator patternTag de espaçamento: Medium-term

Log in to save chunks.