I have following BaseTest class:
from abc import ABC, abstractmethod
class BaseTest(ABC):
@property
@abstractmethod
def name(self):
pass
@property
@abstractmethod
def data(self):
pass
@abstractmethod
def logic(self, input):
pass
def test_runner(self, input, expectation):
#with logic for logging the test name, performance measures etc
try:
self.logic(input)
result = "SUCCESS"
except AssertionError as e:
result = e
assert result == expectation
then my plan was to have Test classes like so:
from random import randint
from base_test import BaseTest
class Test1(BaseTest):
@property
def name(self):
return "Test to assert my logic"
@property
def data(self):
random_number = randint(1, 10)
fixed_number = 20
return [(random_number, "SUCCESS"), (fixed_number, "FAIL: Number higher than 15")]
def logic(self, data):
assert number <= 15, "FAIL: Number higher than 15"
#put pytest markers
def test_execution(input, expectation):
Test1.test_runner(input, expectation)
Is this the correct approach to pytest with classes? What do I need to add for pytest to automatically detect and collect the test and then execute it once for every data entry?
I want to prevent using a Runner Class where I have to specify each Test that needs to be run.
I have tried pytest.mark.parametrize("input, expectation", data)
and def pytest_generate_tests(metafunc)
at various places in the BaseTest class, Test1 class and the conftest.py, but none of it worked so far, I guess I didn’t understand those properly.