【问题标题】:Unit testing in PythonPython中的单元测试
【发布时间】:2015-11-30 20:58:27
【问题描述】:

这是我的第一个 Python 项目,我刚刚学习了 unittest 框架。当我执行python test_module.py 时,测试模块运行良好,但是当我想执行文档中所述的某个类或方法时,使用:

python -m unittest test_module.TestClass.test_method  # or even just test_module

我收到以下错误:

AttributeError: 'module' object has no attribute 'test_module'

我运行命令的目录包含graphm_test.py(我也尝试将名称更改为test_graphm.py),class graphm_test(unittest.TestCase): 和方法都以test_* 开头,这是我在终端上运行的命令:

python -m unittest test_graphm.py

我在任何地方都找不到与此类似的问题,很高兴知道错误背后的原因以及如何在模块内运行某个类或某个方法

【问题讨论】:

标签: python python-unittest


【解决方案1】:

有两个问题:您没有调用正确的模块名称,并且您使用的是扩展名.py。

因此,您需要在包含 graphm_test.py 文件的文件夹中,然后运行:

python -m unittest graphm_test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2021-03-14
    • 2020-05-03
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多