Python: How to run unittest.main() for all source files in a subdirectory?

前端 未结 6 1259
野趣味
野趣味 2020-12-04 13:17

I am developing a Python module with several source files, each with its own test class derived from unittest right in the source. Consider the directory structure:

6条回答
  •  没有蜡笔的小新
    2020-12-04 13:30

    I knew there was an obvious solution:

    dirFoo\
        __init__.py
        test.py
        dirBar\
            __init__.py
            Foo.py
            Bar.py
    

    Contents of dirFoo/test.py

    from dirBar import *
    import unittest
    
    if __name__ == "__main__":
    
        unittest.main()
    

    Run the tests:

    $ python test.py
    ...........
    ----------------------------------------------------------------------
    Ran 11 tests in 2.305s
    
    OK
    

提交回复
热议问题