Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to provide a custom doctest checker #13003

Open
mgedmin opened this issue Nov 28, 2024 · 0 comments
Open

Ability to provide a custom doctest checker #13003

mgedmin opened this issue Nov 28, 2024 · 0 comments
Labels
plugin: doctests related to the doctests builtin plugin type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@mgedmin
Copy link
Contributor

mgedmin commented Nov 28, 2024

What's the problem this feature will solve?

I have a pre-existing test suite that uses doctests with a custom checker class that does some regex replacements to normalize filenames in doctest output, to make my test suite pass under Windows.

I'm having a hard time trying to switch to pytest as a test runner because I was unable to find a way of providing a custom doctest checker.

This is not the only project I've worked with that uses doctests and a regexp-based output normalizers to deal with platform (and sometimes Python version) differences; https://github.com/zopefoundation/ is full of these (although they don't currently have any plans of migrating to pytest).

Describe the solution you'd like

I think a workable interface would be to provide a fixture named doctest_checker:

import doctest
import pytest

class Checker(doctest.OutputChecker):
    def check_output(self, want, got, optionflags):
        want = re.sub(..., ..., want)
        return doctest.OutputChecker.check_output(self, want, got, optionflags)


@pytest.fixture(scope='session')
def doctest_checker():
    return Checker()

I've seen this pattern (overriding a named fixture) used to provide project-specific customizations for pytest test suites that use celery (https://docs.celeryq.dev/en/stable/userguide/testing.html#session-scope), but I'm not absolutely set on it. If it's unworkable, or if there's a better way, let's use that instead.

Alternative Solutions

I've tried looking at the pytest-doctestplus plugin, but I found no way of providing a custom doctest checker either.

Additional context

@Zac-HD Zac-HD added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature plugin: doctests related to the doctests builtin plugin labels Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: doctests related to the doctests builtin plugin type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

2 participants