Python unittest and discovery

后端 未结 3 1419
你的背包
你的背包 2021-02-01 14:01

I have directories, which contain files named like: test_foo.py

Each file is a test case.

I would like to

  1. Run all the tests in a direc

3条回答
  •  深忆病人
    2021-02-01 14:30

    Given how you're trying to use unittest2 from the command line on Python < 2.7, I think you may have missed the note on the unittest2 PyPI page:

    Note

    Command line usage

    In Python 2.7 you invoke the unittest command line features (including test discover) with python -m unittest . As unittest is a package, and the ability to invoke packages with python -m ... is new in Python 2.7, we can't do this for unittest2.

    Instead unittest2 comes with a script unit2. Command line usage:

    unit2 discover unit2 -v test_module
    

    There is also a copy of this script called unit2.py, useful for Windows which uses file-extensions rather than shebang lines to determine what program to execute files with. Both of these scripts are installed by distutils.

    Try the unit2 script which this note recommends as the alternative for older Pythons to the "run package as main script" feature of Python 2.7. Maybe its sources could also be useful to find out exactly how to discover-and-run tests from your own code, if that's what you'd rather do.

提交回复
热议问题