cosmicpython / code

Example application code for the python architecture book
Other
2.14k stars 946 forks source link

Why does 'pytest tests/unit' work with docker but not locally? #26

Closed frankbolero closed 3 years ago

frankbolero commented 3 years ago

Hi!

I'm trying to work out why i can't run pytest tests/unit locally (i e without Docker). I've set up a virtualenv, did pip install requirements.txt.

Using your docker-compose/makefile it works to run the unit tests, but running them locally results in: ImportError while loading conftest '/Users/USERNAME/src/python/cosmicpython/code/tests/conftest.py'. ../tests/conftest.py:14: in <module> from allocation.adapters.orm import metadata, start_mappers E ModuleNotFoundError: No module named 'allocation'

The same goes on for pyCharm which does not recognize the allocation namespace.

What's actually going on here, is there some kind of magic that I'm missing? I'd like to be able to use this setup of yours as it mimics a lot of setups I'm used to from the C# world, but I'd also like to use pyCharm and PyCrunch (https://github.com/gleb-sevruk/pycrunch-engine ) for continuous testing purposes which right now seems impossible.

frankbolero commented 3 years ago

Ok. I'm gonna answer myself here. :) First of all, I'm a bit new to Python so there's that, plus tooling. I had some hour or so earlier trying to solve this and couldn't so I wrote the above and then went to try and solve it even more (a k a having a better look at the pytest documentation). :)

Running tests locally can be done by making sure you are running python -m pytest ../tests/unit from the src-directory. Perhaps there are some other ways of solving this, if you want to run pytest manually the ´make unit-tests´ does that obviously.

The pyCharm issue was solved by making sure that pyCharm actually understood where the actual sources were kept.

As for pycrunch-tooling in pyCharm, there seems to be some version mismatch with Socket.IO or Engine.IO which I guess Flask uses.

hjwp commented 3 years ago

hey frank! basically the project is designed so that you pip install the src directory, and then the allocation module becomes part of your virtualenv packages:

pip install -e src

I'm sorry i didn't document that in the Readme. I'll try and add that shortly. there is a brief mention of it in https://www.cosmicpython.com/book/appendix_project_structure.html tho