【问题标题】:how to run an individual test in python unittest如何在 python unittest 中运行单个测试
【发布时间】:2014-06-16 23:38:14
【问题描述】:

很多标准库不使用类名,如何从命令行运行单个测试用例或将其添加到测试套件中?

这里提出了类似的问题,但所有解决方案都假定测试用例驻留在具有有效类名的文件中。

Running single test from unittest.TestCase via command line

【问题讨论】:

  • “所有解决方案都假定测试用例位于具有有效类名的文件中”正确,这是因为这是使用 python unittest 设置测试的正常方法。如果您不这样做,那么您应该在您的问题中通过一个可以运行的简单示例来解释您的测试的结构
  • 我在看 test_math.py

标签: python python-unittest


【解决方案1】:

简单的方法,安装nosetest

nosetests -mtestAcosh /home/jamie/Downloads/Python-2.6.5/Lib/test/test_math.py

复杂的方式,将测试文件作为模块加载,然后使用 args 运行 unittest.main

PYTHONPATH=/home/jamie/Downloads/Python-2.6.5/Lib/test python -c'from test_math import MathTests
import unittest
unittest.main(None,None,["","test_math.MathTests.testAcosh"])
'

我碰巧拥有的 test_math.py 版本中没有 unittest.main() 编写简单的单元测试文件的常用方法是添加

if __name__ == '__main__':
    unittest.main()

当这是设置单元测试文件的方式时,通常可以按照此答案中的描述运行单个测试

Running single test from unittest.TestCase via command line

即,只需将“testClassName.test_iwanttorun”作为命令行参数提供

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多