[Tutor] import issues
dn
PythonList at DancesWithMice.info
Tue Jan 9 17:29:52 EST 2024
On 10/01/24 07:06, James Hartley wrote:
> I admit that I have not internalized how Python's import system works yet.
> Given the following directory structure:
>
> +-project
> |
> +-src
> | |
> | +-__init__.py
> | +-foo.py
> |
> +-tests
> |
> +-test_foo.py
>
> Currently, __init__.py is empty. What needs to be placed in __init__.py
Nothing - it's not relevant to this discussion.
> and test_foo.py in order for pytest to execute all tests in test_foo.py?
> pytest is being executed while in the project directory. With:
>
> from foo import Foo
>
> --or--
>
> from src.foo import Foo
>
> defined in test_foo.py, pytest reports "ModuleNotFoundError: No module
> named 'src'".
>
> Any and all comments would be appreciated. Any links to online sources
> would also be appreciated. Thanks!
I use the latter, except:
- using PyCharm's auto-testing feature. However, 'translating' those
commands back to manual-typing, would be something like:
python -m pytest /path/project/tests/test_test_foo.py --no-header
--no-summary -q
- prepending "python -m"
which configures the sys.path to include current/project directory (and
hence the "src.foo" relative-addressing) which likely accounts for the
issues outlined (above)
Web.Refs:
Specifics:
https://docs.pytest.org/en/7.4.x/explanation/pythonpath.html#pytest-vs-python-m-pytest
and the rest of the online-manual (although I have to say, even after
years of use, I find it somewhat overwhelming)
Somewhat easier is the 'book of words', Brian Okken's:
https://www.amazon.com.au/Python-Testing-pytest-Effective-Scalable/dp/1680508601/ref=sr_1_1?keywords=brian+okken&qid=1704838720&sr=8-1
(disclaimer: there are other book-sellers. No commission claimed)
Coursera on-line training: (amongst many 'Python' courses)
Testing and Debugging Python
https://www.coursera.org/projects/testing-and-debugging-python
- is a $paid project approach
Possibly slightly off (your) topic is LearnQuest's 'TDD Specialisation':
https://www.coursera.org/learn/test-driven-development-overview
(which I think can be "audited" for $free)
NB have not completed either, and thus can't comment on quality, nor
degree of pytest-content.
--
Regards,
=dn
More information about the Tutor
mailing list