If initializing a class is expensive, but you need to run many paramaterized tests for a class method, you can change the method definition to allow external input. Then you can initialize once outside the test cycle and run as many methods as you need for the method. For example:
Instead of this:
class SuperCool(): def action(self): return self.attribute ** 2
Rewrite to allow external input:
class SuperCool(): def action(self, x=None): if x is None: x = self.attribute return x ** 2
Your test script may now look like this:
sc = SuperCool() @pytest.mark.parametrize("x, y", [(1, 1), (2, 4)]) def test_action_with_parametrization(x, y): assert sc.action(x) == y
But I'm not an experienced programmer, so hopefully this is not an XD anti-pattern
Trenton
source share