mercredi 25 novembre 2020

Unittest possibilities matrix

I'm trying to write unit test for the method of some class defined as below (python):

def can_do_action():
    return any(
      [
        cond1 and cond2 and cond3 and cond4,
        cond5
      ]
    )

cond1 to cond5 are boolean values that represent some business logic. Since 5 boolean values exist, there are 32 possible cases to occur. I've implemented my unittest by defining all possibilites and expected results matrix like below:

# matrix of tuples of (cond1, cond2, cond3, cond4, cond5, result)
matrix = [
    (True, True, True, True, True, True),
    (True, True, True, True, False, True),
    .
    .
    # 32 tuples in total
]

After defining this matrix, I am iterating over tuple, constructing object based ond first 5 items in each tuple (cond1 to cond5), call the method on constructed object and then assert that result is equal to last item (result) of tuple. This strategy successfully tests each case and cover all possibilities.

My question is about conventions and best practices to follow in such situations. I think that my method is too complex for writing unittest, although it covers all possible cases. Can you suggest better strategy to follow?

P.S. I would be thankful, if you can point me to the resources (preferably book) for writing better unittests.

Aucun commentaire:

Enregistrer un commentaire