Ami Tavory
Ami Tavory

Reputation: 76297

Pythonic Way To Test All Combinations Of Function Parameters

I have a given function (which I cannot change) of the form:

def foo(bar=False, baz=False, ban=False, baf=False, bal=False):
    ...

In some unittest code, I'd like to perform tests on all the 25 calls:

foo(bar=False, baz=False, ban=False, baf=False, bal=False)
foo(bar=False, baz=False, ban=False, baf=False, bal=True)
foo(bar=False, baz=False, ban=False, baf=True, bal=False)
foo(bar=False, baz=False, ban=False, baf=True, bal=True)
...
foo(bar=True, baz=True, ban=True, baf=True, bal=True)

(for the sake of the question, run each call, and assertEqual that the result is 10.)

What is a pythonic way of doing so?

Upvotes: 0

Views: 887

Answers (1)

zondo
zondo

Reputation: 20336

You can use itertools.product():

import itertools
for combination in itertools.product(*[(True, False)]*5):
    assertEqual(foo(*combination), 10)

Upvotes: 2

Related Questions