No Generated Test Output

87 views
Skip to first unread message

Matt McCormick

unread,
Jan 7, 2024, 5:18:02 PM1/7/24
to Beancount
I'm writing my first importer and trying to generate the test inputs following the instructions here https://beancount.github.io/docs/importing_external_data.html#regression-testing-your-importers

I believe I've followed the instructions but running `pytest` does not generate anything.

I have the following directory structure:

~/beancount/importers/fidelity

~/beancount/importers/fidelity has an __init__.py file with an Importer class that inherits from beancount.ingest.importer.ImporterProtocol

class Importer(ImporterProtocol):
   ... code

The Importer class implements all the required methods from ImporterProtocol.  When I run `bean-extract` I get the output printed to the console.

I've added the test code listed in the document to __init__.py:

def test():
    importer = Importer()
    yield from regression.compare_sample_files(importer)

When I run pytest I get the following output:

=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.10.12, pytest-7.4.4, pluggy-1.3.0
rootdir: /home/matt/beancount/importers/fidelity
collected 0 items                                                                                                                                                                                                

============================================================================================== no tests ran in 0.00s ==============================================================================================

If I run `pytest` from the base beancount directory, it only runs the tests under my price sources module.

Can someone help identify what I may be missing?  Thank you

aa...@axvig.com

unread,
Jan 11, 2024, 11:26:54 AM1/11/24
to bean...@googlegroups.com, Matt McCormick

I was just working on this same sort of problem (first importer) a couple days ago and haven't gotten back to it, but it felt like I was close.  You don't mention a conftest.py file which I think is important.  The documentation on this topic is just OK but I can understand why.  My next move is to integrate all of the information in this past mailing list discussion: How to use pytests for personal importers / ingest (google.com)

Good luck!

--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/cca05bf7-9b0a-4650-8e97-7154c0abb2cen%40googlegroups.com.

Matt McCormick

unread,
Jan 11, 2024, 10:12:54 PM1/11/24
to Beancount
Thanks for the tip about conftest.py.  That wasn't mentioned in the documentation.  From the link to the past discussion I was able to figure it out and the info about conftest.py was key.  Here's my setup now and how it differs from the documentation:

1. The test() function added to myimporter__init__.py is unnecessary.  It can be removed.

2. Add `conftest.py` to base directory with the contents

# This adds the --generate option.
# pylint: disable=invalid-name
pytest_plugins = "beancount.ingest.regression_pytest"

3. Don't put your test files in the same directory as the __init__.py file.  Otherwise, the tests will attempted to be run on all files, not just files that match identity().  Instead make a sub directory under the importer and place files there.

4. Create a `test_importer.py` file with the following contents:

from pathlib import Path

from beancount.ingest import regression_pytest as regtest

from importers.fidelity import Importer

IMPORTER = Importer()


@regtest.with_importer(IMPORTER)
@regtest.with_testdir(Path(__file__).parent / 'testdata')
class TestImporter(regtest.ImporterTestBase):
    pass


In summary, my file directory setup looks like this

beancount/
beancount/my.beancount
beancount/conftest.py

beancount/importers/fidelity/__init__.py  # Importer code
beancount/importers/fidelity/testdata/  # directory for files to test
beancount/importers/fidelity/testdata/my_sample.csv

Then run the following to generate the output files to be used by the tests.

pytest --gen

This will show the tests as skipped.  

(venv) $:~/beancount$ pytest --gen

=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.10.12, pytest-7.4.4, pluggy-1.3.0
rootdir: /home/matt/beancount
collected 5 items                                                                                                                                                                                                

importers/fidelity/test_importer.py .ssss                                                                                                                                                                   
========================================================================================== 0 passed, 4 skipped in 0.03s ===========================================================================================

Then run pytest to run the tests.

pytest

(venv) $:~/beancount$ pytest

=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.10.12, pytest-7.4.4, pluggy-1.3.0
rootdir: /home/matt/beancount
collected 5 items                                                                                                                                                                                                

importers/fidelity/test_importer.py .....                                                                                                                                                              

================================================================================================ 5 passed in 0.03s ================================================================================================

aa...@axvig.com

unread,
Jan 12, 2024, 4:20:53 PM1/12/24
to bean...@googlegroups.com

Glad to hear it helped.  Following your stuff I was then able to get mine working in about 30 minutes.

I wasn't quite sure where to put test_importer.py since you didn't mention it.  Seems to be working in beancount/importers/fidelity/test_importer.py

My beancount/importers/fidelity/__init__.py is empty and the code is beancount/importers/fidelity/fidelity.py  Maybe that is why I have ridiculous things like "from importers.fidelity.fidelity import Importer".

Matt McCormick

unread,
Jan 12, 2024, 5:20:49 PM1/12/24
to bean...@googlegroups.com
Oh sorry I missed that.  Yes, the test_importer.py file goes where you mentioned using my example file directory.  Great to hear you got it working too!

I was actually able to simplify that after posting by using relative imports.  So in my file I have

from . import Importer
But that's because my code is in __init__.py.  I think that was recommended in the docs and it does make the import statements a little cleaner.  If you have yours in another file, as long as it's in the same directory, you should be able to import by:

from .fidelity import Importer

You received this message because you are subscribed to a topic in the Google Groups "Beancount" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beancount/qAsQOwc9QR0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/f50e8fd3261822af61af4842b9fba06f%40axvig.com.
Reply all
Reply to author
Forward
0 new messages