[Tutor] Do not understand why test is running.

Peter Otten __peter__ at web.de
Sun Aug 23 18:47:49 CEST 2015


boB Stepp wrote:

> On Sat, Aug 22, 2015 at 3:18 AM, Peter Otten <__peter__ at web.de> wrote:
>> boB Stepp wrote:
>>
>>> In the cold light of morning, I see that in this invocation, the path
>>> is wrong.  But even if I correct it, I get the same results:
>>>
>>> e:\Projects\mcm>py -m unittest ./test/db/test_manager.py
>> [...]
>>> ValueError: Empty module name
>>
>> Make sure that there are files
>>
>> ./test/__init__.py
>> ./test/db/__init__.py
> 
> I had done this.
> 
>> and then try
>>
>> py -m unittest test.db.test_manager
> 
> I *thought* that I had done this, but perhaps...
> 
>>> e:\Projects\mcm>py ./test/db/test_manager.py
>>> Traceback (most recent call last):
>>>   File "./test/db/test_manager.py", line 16, in <module>
>>>     import mcm.db.manager
>>> ImportError: No module named 'mcm'
>>
>> Make sure the parent directory of the mcm package (I believe this is
>> E:\Projects\mcm) is in your PYTHONPATH, then try again.
> 
> ... I was not in the directory, E:\Projects\mcm.  It is my
> understanding that if I start in a particular directory when I invoke
> Python, then it looks in that location first in PYTHONPATH.  

No, it looks in the location of the script, not in the working directory:

$ mkdir sub
$ echo 'print("hello")' > foo.py
$ echo 'import foo' > main.py
$ python3 main.py
hello

That's the normal situation as the main script you're working on is 
typically in the working directory. Let's move it:

$ mv main.py sub
$ python3 sub/main.py
Traceback (most recent call last):
  File "sub/main.py", line 1, in <module>
    import foo
ImportError: No module named 'foo'

You have to add the working directory explicitly, e. g.:

$ PYTHONPATH=. python3 sub/main.py
hello

> Anyway,
> typing py -m unittest test.db.test_manager from this location works
> (And now makes sense.).
> 
> But I still have remaining questions to clear up:
> 
> Peter said earlier in this thread:
> 
> ------------------------------------------------
> If you want to trigger the
> 
> if __name__ == "__main__": ...
> 
> you have to invoke the test script with
> 
> py ./mcm/test/db/test_manager.py
> 
> the same way you would invoke any other script.
> -------------------------------------------------
> 
> If I try this or begin in E:\Projects\mcm and type py
> ./test/db/test_manager.py I get
> 
> E:\Projects\mcm>py ./test/db/test_manager.py
> Traceback (most recent call last):
>   File "./test/db/test_manager.py", line 16, in <module>
>     import mcm.db.manager
> ImportError: No module named 'mcm'
> 
> 
> I don't understand why this is the case.  I did various experiments
> with different file structures for the project, when I put the
> test_manager.py file in the same directory as the module file being
> tested, manager.py, then (after adjusting the import statement in
> test_manager.py appropriately) typing py test_manager.py runs the unit
> tests.  So, why does the above not work?  Why does it return "No
> module named 'mcm'"?  Especially in the context that now test
> discovery has no problem and runs correctly and test.db.test_manager
> runs correctly.
> 
> And am I misreading the docs at
> https://docs.python.org/3/library/unittest.html#test-discovery:
> 
> 
-------------------------------------------------------------------------------------------------------------------
> 26.3.2. Command-Line Interface
> 
> [...]
> 
> Test modules can be specified by file path as well:
> 
> python -m unittest tests/test_something.py
> 
> This allows you to use the shell filename completion to specify the
> test module. The file specified must still be importable as a module.
> The path is converted to a module name by removing the ‘.py’ and
> converting path separators into ‘.’

I didn't know this.
 
> According to this, from E:\Projects\mcm, I should be able to type
> 
> py -m unittest ./test/db/test_manager.py

Let's process the argument you provide following the above recipe:

(1) Remove .py

"./test/db/test_manager"

(2) Replace / with .

"..test.db.test_manager"

Do you see now why

>  this continues to result in:

[...]

>   File "C:\Python34\lib\unittest\loader.py", line 105, in
>   loadTestsFromName
>     module = __import__('.'.join(parts_copy))
> ValueError: Empty module name

?

Without the leading ./ it will probably work.

> I believe that I understand Peter's point earlier, but if I am reading
> the docs correctly, I should be able to do this.  

While I don't think it's a bug you might still file a feature request on 
bugs.python.org.



More information about the Tutor mailing list