Cleanest method for creating a pytest test fixture that takes in dynamic text data

88 Views Asked by At

I have class that takes in text data when its instantiated, and also contains parsing methods that search for host name, and data center info within the text data:

class SystemConfig:
  def __init__(self, text_data):
    self.text_data = text_data

  def get_server(self):
    match = re.search('.+ (server.+?)', self.text_data, re.S)
    if match:
      return match.group(1)

  def get_datacenter(self):
    match = re.search('.+? (dc.+?)', self.text_data, re.S)
    if match:
      return match.group(1)    

I am trying to identify the most efficient way for writing pytest fixtures for this.

The route I am heading down is as follows:

@pytest.fixture
def server_data():
  return SystemConfig('row 1 rack 2 server001')

@pytest.fixture
def datacenter_data():
  return SystemConfig('row 1 rack 2 dc001')

def test_server_data(server_data):
  assert server_data.get_server() == 'server001'

def test_server_data(server_data):
  assert server_data.get_datacenter() == 'dc001'

Instantiating a class each time I need to tweak the text data seems inefficient to me. I may need to write 20 more parsing methods to grab specific text entries.

Any guidance on the best way to to write test fixtures for this is much appreciated.

Please also let me know if you need any further clarification. Thank you in advance.

0

There are 0 best solutions below