Closed hamedsh closed 2 years ago
Agree, that is really weird. I was able to reproduce the issue using your example.
I will need some time to find the root cause of this.
I can see following following error in pycrunch-engine output:
[70736] 2021-07-11 14:43:28,653 - pycrunch.discovery.simple - WARNING - importing tests._flask_realworld.article.model.test_comment
[70736] 2021-07-11 14:43:29,167 - pycrunch.discovery.simple - ERROR - Failed to load `tests/_flask_realworld/article/model/test_comment.py`
[70736] 2021-07-11 14:43:29,168 - pycrunch.discovery.simple - ERROR - importing tests._flask_realworld.article.model.test_comment failed with exception: cannot import name 'Column' from partially initialized module 'flask_realworld.database' (most likely due to a circular import) (/Users/gleb/code/flask_realworld_article_library/flask_realworld/database.py)
Traceback (most recent call last):
File "/Users/gleb/Library/Caches/pypoetry/virtualenvs/flask-realworld-KuM9jcGi-py3.9/lib/python3.9/site-packages/pycrunch/discovery/simple.py", line 104, in find_tests_in_folder
module = importlib.import_module(module_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/gleb/code/flask_realworld_article_library/tests/_flask_realworld/article/model/test_comment.py", line 1, in <module>
from flask_realworld.articles.models.comment import Comment
File "/Users/gleb/code/flask_realworld_article_library/flask_realworld/articles/models/comment.py", line 3, in <module>
from flask_realworld.database import Model, reference_col, relationship, Column, SurrogatePK
File "/Users/gleb/code/flask_realworld_article_library/flask_realworld/database.py", line 3, in <module>
from .extensions import db
File "/Users/gleb/code/flask_realworld_article_library/flask_realworld/extensions.py", line 43, in <module>
from flask_realworld.utils import jwt_identity, identity_loader
File "/Users/gleb/code/flask_realworld_article_library/flask_realworld/utils.py", line 3, in <module>
from flask_realworld.user.models.user import User
File "/Users/gleb/code/flask_realworld_article_library/flask_realworld/user/models/user.py", line 3, in <module>
from flask_realworld.database import Column, SurrogatePK, Model
ImportError: cannot import name 'Column' from partially initialized module 'flask_realworld.database' (most likely due to a circular import) (/Users/gleb/code/flask_realworld_article_library/flask_realworld/database.py)
It may be caused by incorrect order in which test discovery is run. As Pycrunch doesn't use AST it will actually need to compile entire dependency graph for test discovery and import all referenced models.
As a workaround for now now you can move
from flask_realworld.articles.models.comment import Comment
inside each test.
Note, that it is not exact name, like test_comment.py
or test_comment_model.py
that doesn't work, but the act of renaming
the file. Renaming the file temporary fixes the issue, but the problem persist after engine restart.
A permanent solution would be for me to change test discovery to AST parsing instead of module import (not coming in following month, maybe in the middle of September)
This is now fixed.
AST test discovery did a trick, in pr. Discovery is a lot more reliable now.
pip install pycrunch-engine==1.3
or
pip install pycrunch-engine --upgrade
i have a test, like this:
flask_realworld/tests/_flask_realworld/article/model/test_comment_model.py
and it does not appear in the tests list, but when I renamed it to this:flask_realworld/tests/_flask_realworld/article/model/test_comment.py
it will appear! steps to reproduce: clone this repo: