Skip to content

pytest compatibility #134

@andylou2

Description

@andylou2

Hi,

In beancount v2 there is a handy utility to incorporate Importer unit tests with pytest: https://github.com/beancount/beancount/blob/v2/beancount/ingest/regression_pytest.py

I couldn't find the equivalent in beangulp. In the examples directory, I saw the tests are driven by a bash script: https://github.com/beancount/beangulp/blob/master/examples/importers/runtests.sh

Are you guys open to adding the pytest compatibility to beangulp, or accepting PRs? Something along the lines of this:

import os
from typing import List, Optional, Union

import beangulp
import beangulp.testing
import click
import pytest
from beangulp import utils
from beangulp.importer import Importer, ImporterProtocol


def run_test_programmatically(
    importer: Union[Importer, ImporterProtocol],
    documents: List[str],
    expected: Optional[str] = None,
    verbose: int = 0,
    quiet: int = 0,
    failfast: bool = False,
    generate: bool = False,
) -> None:
    """
    Programmatically run the test command that would normally be invoked via CLI.

    Args:
        importer: The importer instance to test
        documents: List of document paths to test
        expected: A directory for expected output files
        verbose: Verbosity level (default: 0)
        quiet: Quiet level (default: 0)
        failfast: Whether to stop at first failure (default: False)
        generate: If True, then generates test data
    """
    ctx_obj = beangulp.Ingest([importer])
    command = beangulp.testing._generate if generate else beangulp.testing._test

    with click.Context(command, obj=ctx_obj) as ctx:
        if generate:
            command.callback(documents=documents, expected=expected, verbose=verbose, quiet=quiet, force=True)

            # Verify that the expected files were generated
            for doc in utils.walk(documents):
                if doc.endswith(".beancount"):
                    continue

                expected_filename = f"{doc}.beancount"
                if not os.path.exists(expected_filename):
                    raise FileNotFoundError(f"Did not generate expected file: {expected_filename}")
        else:
            command.callback(documents=documents, expected=expected, verbose=verbose, quiet=quiet, failfast=failfast)

Thanks,
Andy

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions