Re: importing modules to be tested
Leam Hall <[email protected]> Wed, 10 Jan 2024 19:15:38 -0600
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
Hey Alex, I'm not sure if this will help, but you can look at the bp_tracker repo we worked on for an example: https://github.com/LeamHall/bp_tracker/blob/master/ With that, there are a couple of things to make it work. 1. In the test/ directory there's an empty file "__init__.py". 2. From the same directory as bp_tracker.py, run: python -m unittest That should let it discover your tests, and the imports you have should work. As to using a test framework, I usually use unittest for simplicity. I've done pytest, and will probably do it again, but that's more want than need. Like git, being able to use unittest or pytest is a skill you want to learn. Using doctest is okay, but seems much more limited. Leam On 1/10/24 18:46, dn via Tutor 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 >>> >> >> > -- Software Engineer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor