[Tutor] Do not understand why test is running.
boB Stepp
robertvstepp at gmail.com
Sun Aug 23 17:42:25 CEST 2015
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. 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 ‘.’
--------------------------------------------------------------------------------------------------------------------
According to this, from E:\Projects\mcm, I should be able to type
py -m unittest ./test/db/test_manager.py
but this continues to result in:
------------------------------------------------------------------------------------
E:\Projects\mcm>py -m unittest ./test/db/test_manager.py
Traceback (most recent call last):
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "C:\Python34\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python34\lib\unittest\__main__.py", line 18, in <module>
main(module=None)
File "C:\Python34\lib\unittest\main.py", line 92, in __init__
self.parseArgs(argv)
File "C:\Python34\lib\unittest\main.py", line 139, in parseArgs
self.createTests()
File "C:\Python34\lib\unittest\main.py", line 146, in createTests
self.module)
File "C:\Python34\lib\unittest\loader.py", line 146, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Python34\lib\unittest\loader.py", line 146, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Python34\lib\unittest\loader.py", line 105, in loadTestsFromName
module = __import__('.'.join(parts_copy))
ValueError: Empty module name
--------------------------------------------------------------------------------------
I believe that I understand Peter's point earlier, but if I am reading
the docs correctly, I should be able to do this. And in fact while I
was experimenting with different directory structures, I found some
that would indeed allow me to invoke unit tests by designating the
file path.
So, I can proceed with my project, but I still don't understand these
two issues.
boB
More information about the Tutor
mailing list