为什么 Python 中的单元测试需要 -m 选项?
Why unit tests in Python need -m option?
我正在阅读 Python 关于单元测试的文档,刚注意到 -m
标志 running test cases:
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 为什么需要它。
我的问题是针对单元测试的:为什么它们需要 运行 作为脚本?
文档只是在所有示例中抛出 -m
,但我找不到其背后的基本原理。
你还在运行正在学习一个模块。命令是:
python -m unittest <path_to_your_script>
unittest
是模块,您正在将位置参数传递给unittest.
如果你真的 运行 对 unittest 的帮助:
python -m unittest --help
你会得到一个很长的输出来解释如何使用它。也就是说,这篇文章准确地解释了如何使用它:
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
甚至在帮助中也有解释,是关于传递的 positional 参数的声明:
positional arguments:
tests a list of any number of test modules, classes and test
methods.
我正在阅读 Python 关于单元测试的文档,刚注意到 -m
标志 running test cases:
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 为什么需要它。
我的问题是针对单元测试的:为什么它们需要 运行 作为脚本?
文档只是在所有示例中抛出 -m
,但我找不到其背后的基本原理。
你还在运行正在学习一个模块。命令是:
python -m unittest <path_to_your_script>
unittest
是模块,您正在将位置参数传递给unittest.
如果你真的 运行 对 unittest 的帮助:
python -m unittest --help
你会得到一个很长的输出来解释如何使用它。也就是说,这篇文章准确地解释了如何使用它:
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
甚至在帮助中也有解释,是关于传递的 positional 参数的声明:
positional arguments:
tests a list of any number of test modules, classes and test
methods.