[Tutor] Do not understand why test is running.

boB Stepp robertvstepp at gmail.com
Fri Aug 21 15:58:30 CEST 2015

On Fri, Aug 21, 2015 at 1:16 AM, Peter Otten <__peter__ at web.de> wrote:
> boB Stepp wrote:
>> On Thu, Aug 20, 2015 at 10:13 PM, Steven D'Aprano <steve at pearwood.info>
>> wrote:
>>> On Thu, Aug 20, 2015 at 09:01:50PM -0500, boB Stepp wrote:
>>>> import unittest
>>>> # import modules to be tested:
>>>> import mcm.db.manager
>>>> class ManagerTestCase(unittest.TestCase):
>>>>     def setUp(self):
>>>>         # Insert setup code here...
>>>>         pass
>>>>     def test_open_db(self):
>>>>         pass
>>>>     def tearDown(self):
>>>>         # Insert tear-down code here...
>>>>         pass
>>>> #if __name__ == "__main__":
>>>> #    unittest.main()
>>> The two commented out lines at the end would, if uncommented, run
>>> unittest.main() if and only if you are running this specific module as a
>>> thread. In other words, if you were to run:
>>> python /path/to/the/test.py
>>> then __name__ would be set to the string "__main__", the if clause would
>>> trigger, and unittest.main() would run. Since those lines are commented
>>> out, that cannot happen.
>> Okay, I uncommented those two lines and got:
>> E:\Projects\mcm>py -m unittest ./mcm/test/db/test_manager.py

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
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>
  File "C:\Python34\lib\unittest\main.py", line 92, in __init__
  File "C:\Python34\lib\unittest\main.py", line 139, in parseArgs
  File "C:\Python34\lib\unittest\main.py", line 146, in createTests
  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

> Yea, breaking things is an art form ;)

Alas!  I am still artless because if I do as you suggest

> 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.

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'

This is using the path correction I mention above.  (But just to be
certain, I also did the exact path Peter gave with the same results.)

> py -m unittest <more args>
> runs the unittest module which is free to do what it wants with its
> arguments. Let's see:

The reason that I have been trying to invoke this individual test
module the way I have is because in the docs it says:

26.3.2. Command-Line Interface

The unittest module can be used from the command line to run tests
from modules, classes or even individual test methods:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

You can pass in a list with any combination of module names, and fully
qualified class or method names.

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 ‘.’

So I am still quite confused!

Uh, oh, I have to leave for work.  Peter, I have not made it to the
rest of your comments yet.  That will have to wait till this evening,
but many thanks for the assistance!  If the answer is in your
remaining comments, then I apologize for the additional noise in


More information about the Tutor mailing list