python - Pycharm 以详细模式运行单元测试

标签 python class pycharm python-unittest verbose

有没有办法在 pycharm 中以详细模式运行单元测试。我正在寻找一种在测试函数中查看文档字符串的方法,以便我可以看到运行测试的一些信息。

class KnownValues(unittest.TestCase):
    known_values = (
        ([1, 2],[[1, 2]]),
        ([1, 2, 3], [[1, 2], [1, 2, 3]]),
        ([1, 2, 3, 4],[[1, 2], [1, 2, 3, 4]]),
        ([1, 2, 3, 4, 5],[[1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]),
        ([1, 2, 3, 4, 5, 6],[[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6]]),
              )

    def test_check(self):
        '''This should check is the function returning right'''
        for arg, result in self.known_values:
            print("Testing arg:{}".format(arg))
            assert program.lister(arg) == result


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

它返回:

Testing started at 19:38 ч. ...
Testing arg:[1, 2]

Process finished with exit code 0

我想得到:

test_check (__main__.KnownValues)
This should check is the function returning right ... Testing arg:[1, 2]
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

最佳答案

您所要做的就是使用 setUp 方法并像这样调用 _testMethodDoc 属性:

def setUp(self):
    print(self._testMethodDoc)

你当然可以为你的单元测试创​​建你自己的基类,它继承自 unittest.TestCase) 但是如果你想稍后重写 setUp 方法,你将有调用 super。可以选择缩短代码实现:

class BaseUnitTest(unittest.TestCase):
    def setUp(self):
        print(self._testMethodDoc)


class KnownValues(BaseUnitTest):
    ...

关于python - Pycharm 以详细模式运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561829/

相关文章:

python - python可以读取这种类型的配置文件吗?

python - 查找匹配字符串或正则表达式的结束偏移量

python - 有人知道任何用户信誉引擎吗?

c# - 将代码翻译成类

swift - 类层次结构 - 类只能由另一个类调用

unit-testing - 是否可以将用户输入保存在变量中以使用 intellij (pycharm) 进行连续测试

python - 建立数据源的目的是什么

python - 跨多个文档的字符串搜索 - grep?

Java - 无法添加文本

pycharm - PyCharm Professional 中的 JavaScript 语法高亮显示