Python unit testing with PyTest, stuck on more advanced testing

  pytest, python, unit-testing

I have been using PyTest for some time now to write some simple tests (like the ones you find in tutorials and youtube video’s) and I thought now it was time to start writing actual test for our python scripts. The scripts are way more advanced than any shown in tutorials so I am getting a bit stuck. I do not want the entire correct answer, but rather a nudge in the right direction if possible. Here is my issue:

We have a script that reads a .md text file and converts it to a pdf file based on an external template. Part of the script is here below (I removed most of it because I first just want to have 1 running test)

class DocumentationEngine:

    def __init__(self, title, subtitle, series, style='TIIStyle_Digital_Aug_2020', templateFile='template.docet', tableOfContents=True, listOfFigures=False, listOfTables=False):
        self.title = title
        self.subtitle = subtitle
        self.series = series
        self.style = style
        self.template = {}
        self.hasTOC = tableOfContents
        self.hasLOF = listOfFigures
        self.hasLOT = listOfTables
        self.loadTemplate(templateFile)

    def loadTemplate(self, file='template.docet'):
        with open(file, "r") as templatefile:
            lines = templatefile.readlines()
            key = "dummy"
            value = ""
            for line in lines:
                line = line.strip()
                if line.startswith('[') and line.endswith(']'):
                    self.template[key] = value
                    key = line[1:-1]
                    value = ""
                else:
                    value += line + 'n'

    def build(self, versions=[], content='', filename='Documenter_Autogenerated'):
        document = self.template["doc"]
        document = document.replace("%%style%%", self.style)
        document = document.replace("%%body%%",
                                    self.buildFirstPage() +
                                    self.buildTableOfContents() +
                                    self.buildListOfFigures() +
                                    self.buildListOfTables() +
                                    self.buildVersionTable(versions, filename) +
                                    self.buildContentPages(content=content) +
                                    self.buildLastPage()
                                    )
        return document


    def buildLastPage(self):
        return self.template["last_page"]

I am trying to write a simple unit test for the buildLastPage method and have been stuck for several days now.

I am not sure whether or not I need to mock the template file, use a fixture and/or if I can actually test only that method with all dependencies.

I started with the following:

from doceng import DocumentationEngine
import pytest


class Test:
    def test_buildLastPage(self):
        build_last_page = DocumentationEngine()

        assert build_last_page.template(1) == 1

which gives me an error regarding 3 required arguments. When adding the arguments like this:

from doceng import DocumentationEngine
import pytest


class Test:
    def test_buildLastPage(self, title, subtitle, series):
        build_last_page = DocumentationEngine()

        assert build_last_page.template(1) == 1

which gives me an error that the fixture is not found.

I added a fixture in conftest.py file like this:

import pytest
from doceng import DocumentationEngine


@pytest.fixture
def title(title):
    return title("test")

which will get me another error, recursive dependency involving fixture ‘title’ detected

I’m quite stuck so any nudge in the right direction for a newbie would be highly appreciated

Source: Python Questions

LEAVE A COMMENT