[Tutor] importing modules to be tested

dn PythonList at DancesWithMice.info
Wed Jan 10 21:23:01 EST 2024


On 11/01/24 15:12, Alex Kleider wrote:
> I stand corrected: the __init__.py file is not needed.

People often include.
It is/was part of "packaging".
However, nothing that really affects testing.


> $ python -m pytest
> works all by itself.

Yes, it's all about project-directory and sub-directory structuring; and 
  consequent implications for the import system.

You detected the problem, and figured-out how to combat same.

When you have time to dive into pytest (or ??? framework), you'll likely 
find all manner of other useful aids which will add quality to your work...


NB @Leam's solution uses PSL's own unittest framework, cf pytest, which 
can be found in Pypi/'the Cheese Shop'.


> Thank you for your patience.

As long as it works
(and will work next project...!)



> On Wed, Jan 10, 2024 at 6:05 PM dn <PythonList at danceswithmice.info> 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 <PythonList at danceswithmice.info> 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 <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
>>>>>>
>>>>>
>>>>>
>>>>
>>>> --
>>>> Regards,
>>>> =dn
>>>>
>>>
>>>
>>
>> --
>> Regards,
>> =dn
>>
> 
> 

-- 
Regards,
=dn



More information about the Tutor mailing list