2

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.

Rafael Beckel
  • 2,199
  • 5
  • 25
  • 36

1 Answers1

3

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.
idjaw
  • 25,487
  • 7
  • 64
  • 83