HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Unit Testing of parser method using pytest

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pytestmethodparsertestingusingunit

Problem

This is my first time using pytest. All feedback for this test case is much appreciated.

import pytest
from mock import mock_open, patch

def get_file_contents(file_data):
  with patch.object('builtins.open', mock_open(read_data=file_data)) as mock:
    with open('mocked_file') as f:
      return (line for line in f.readlines())

@pytest.fixture(scope="module")
def text_parser(request):
  from tparse import TextParser
  file_data = getattr(request.module, 'file_contents')
  tparse = TextParser(file_data)
  def fin():
    tparse.close()
  request.addfinalizer(fin)
  return tparse

def test_get_system_entry(self):
  file_data = 'dc nyc server server001 ipaddress 10.10.10.10'
  file_iterable = get_file_contents(file_data)
  assert file_iterable == file_data

Solution

I'm not familiar using py.test, but here are some comments I have after reading your code:

  • There are no comments - this would be helpful to the reader.



  • In text_parser, you have from tparse import TextParser. I would advise moving this import to the top of the file, for readability.



  • What is your code trying to do with these tests? What are you testing?

Context

StackExchange Code Review Q#91760, answer score: 3

Revisions (0)

No revisions yet.