Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Add option for custom formatter#4

Open
markus-work wants to merge 5 commits intoliiight:masterfrom
markus-work:feature/custom_formatter
Open

Add option for custom formatter#4
markus-work wants to merge 5 commits intoliiight:masterfrom
markus-work:feature/custom_formatter

Conversation

@markus-work
Copy link
Copy Markdown

Hi,

I have expanded the pytest options with --custom-formatter-path where you can input a module path to a file containing the class CustomFormatter.

@liiight
Copy link
Copy Markdown
Owner

liiight commented May 10, 2022

wow, I can't believe people use this, not to mention actually put up a PR! Thanks for this!
I wonder what is your use case for this?

@markus-work
Copy link
Copy Markdown
Author

My tests don't have classes, so I got lines with only ## and I did not want that. I could also remove the markers, since I am not interested in that. And since I had implemented it, I might as well share it :)

In my formatter I also handle multiline docstrings. Here it is:

from pytest_docs.formatter import Formatter
from textwrap import dedent


class CustomFormatter(Formatter):
    name = "custom"
    marker_prefix = ""

    @staticmethod
    def module_name_format(element):
        return "# {}".format(element)

    @staticmethod
    def class_name_format(element):
        if str(element) == "":
            return ""
        return "## Class {}".format(element)

    @staticmethod
    def func_name_format(element):
        return "## {}".format(element)

    @staticmethod
    def marker_format(marker):
        return ""

    @staticmethod
    def func_desc_format(element):
        return dedent(str(element)).lstrip("\n")

@liiight
Copy link
Copy Markdown
Owner

liiight commented May 16, 2022

hmm, I'm a bit hesitant to introduce such a direct import mechanism system to support this use type. I think refactoring the existing formatter to be plugins and loaded as such would be better, as it will allow any user just creating new ones on their end without needing new code changes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants