简体   繁体   中英

Why unit tests in Python need -m option?

I'm reading Python's documentation about unit tests and just noticed the -m flag when running test cases :

There are two SO questions about the purpose of the -m flag, so I know it runs modules as scripts and also there's a full explanation here about why it is needed.

My question is specific to unit tests: why do they need to run as scripts?

The documentation just throws -m in all examples, but I could not find the rationale behind it.

You are still running a module. The command is:

python -m unittest <path_to_your_script>

unittest is the module and you are passing positional arguments to unittest .

If you actually run the help against unittest:

python -m unittest --help

You will get a long output explaining how to use it. Namely, this particular piece that explains exactly how to use it:

  python -m unittest test_module               - run tests from test_module
  python -m unittest module.TestClass          - run tests from module.TestClass
  python -m unittest module.Class.test_method  - run specified test method

Even explained in the help, is a statement about the positional arguments that are passed:

positional arguments:
  tests           a list of any number of test modules, classes and test
                  methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM