roulette01
roulette01

Reputation: 2462

How to pytest parametrize over a list of instance method getters?

I have a class that has several getter methods:

class ExampleClass:
  def __init__(self):
    # impl omitted

  def getter1(self):
    # impl omitted
  
  def getter2(self):
    # impl omitted

I want to write a single unit test that parametrizes over all the getters (in this particular case they should return the same thing)

So I want to do something like this

@pytest.mark.parametrize('getter_method', ['getter1', 'getter2'])
def test_exampleclass_getters(getter_method):
    inst = ExampleClass()
    with raises(ValueError):
        getattr(bid, getter_method)(OVN_END_DT)

But I read that getattr is bad practice and should generally be avoided, though I feel like it's OK in this case. If not, are there other approaches to accomplishing what I need?

Upvotes: 0

Views: 20

Answers (0)

Related Questions