[Tutor] importing modules to be tested

Alex Kleider alexkleider at gmail.com
Wed Jan 10 19:43:09 EST 2024


I've done what I think 'dn' is recommending but still things
aren't working.
There is still the same import error.
Again, any advice would of course be appreciated.

$ ls -lA
total 16
-rwxr-xr-x 1 alex alex  100 Jan 10 12:25 code2test.py
drwxr-xr-x 3 alex alex 4096 Jan 10 16:19 tests

$ ls -lA tests
total 8
-rwxr-xr-x 1 alex alex  233 Jan 10 16:19 test_code.py

$ cat code2test.py
#!/usr/bin/env python3
# file: code2test.py
def code():
    return "code2test.code() has been run"

$ cat tests/test_code.py
#!/usr/bin/env python3
# File: tests/test_code.py
import code2test

def test_code(self):
    assert code2test.code() ==  "code2test.code() has been run"

$ pytest
============================== test session starts
==============================
platform linux -- Python 3.9.2, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/alex/Git/Lib/Question
collected 0 items / 1 error

==================================== ERRORS
=====================================
______________________ ERROR collecting tests/test_code.py
______________________
ImportError while importing test module
'/home/alex/Git/Lib/Question/tests/test_code.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_code.py:8: in <module>
    import code2test
E   ModuleNotFoundError: No module named 'code2test'
============================ short test summary info
============================
ERROR tests/test_code.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection
!!!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.06s
================================

On Wed, Jan 10, 2024 at 4:10 PM dn <PythonList at danceswithmice.info> wrote:
>
> On 11/01/24 12:50, Alex Kleider wrote:
> > Thanks "dn" for your response.
> > I couldn't find an explicit answer to my question but am I to understand that
> > if one uses one of the testing frameworks then the problem goes away?
>
> Yes, don't waste your time by doing things the hard-way, unnecessarily!
>
>
> > Alex
> >
> >> Again, you've 'taken the bull by the horns' and bravely attempted to
> >> solve the problem.
> >>
> >> However, this is 're-inventing the wheel' and not 'standing on the
> >> shoulders of giants' (who have trod this path before).
> >>
> >> There are three popular automated-testing frameworks:
> >>
> >> - doctest
> >> - unittest
> >> - pytest
> >>
> >> NB there are others, as well as plug-ins/extensions to the above - the
> >>
>
> --
> Regards,
> =dn
>


-- 
alex at kleider.ca  (he/him)
(sent from my current gizmo)


More information about the Tutor mailing list