Re: importing modules to be tested
Alex Kleider <[email protected]> Wed, 10 Jan 2024 18:12:07 -0800
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <CAMCEyD4DayHTvZKHLG65Kitw6kGYOw90Fn53pt66ruTza0S3KQ@mail.gmail.com> |
I stand corrected: the __init__.py file is not needed. $ python -m pytest works all by itself. Thank you for your patience. On Wed, Jan 10, 2024 at 6:05 PM dn <[email protected]> wrote: > > On 11/01/24 13:59, Alex Kleider wrote: > > I've studied that thread; if the solution is there, I've not > > understood what it is. > > In fact it was that thread that prompted me to bring up this (same?) > > issue that has been on my mind for a long time. > > I can be patient and continue to use my clumsy solution for the time being. > > Again, thank you for all your contributions. > > > The issue described below IS similar to referenced thread: pytest is > running but is unable to 'see' the script from a test-script in the test > sub-directory. > > <<< > 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) > >>> > > > > > On Wed, Jan 10, 2024 at 4:46 PM dn <[email protected]> wrote: > >> > >> Sorry Alex, am in the middle of a conference. > >> Please see 'yesterday's' conversation, as referenced earlier - should > >> solve the problem... > >> > >> On 11/01/24 13:43, Alex Kleider wrote: > >>> 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 <[email protected]> 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 > >>>> > >>> > >>> > >> > >> -- > >> Regards, > >> =dn > >> > > > > > > -- > Regards, > =dn > -- [email protected] (he/him) (sent from my current gizmo) _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor