简体   繁体   English

增加在Python中进行单元测试的类的日志记录详细程度

[英]Increasing logging verbosity of class being unit-tested in Python

I have a Python class that uses the logging module to provide some debug output: 我有一个Python类,它使用日志记录模块来提供一些调试输出:

File someclass.py: 文件someclass.py:

import logging

class SomeClass:

    def do_stuff(self):
        # do some things
        logging.debug("I just did some stuff")
        # do some more stuff
        return True

I do unit testing on this class with the unittest module 我使用unittest模块对这个类进行单元测试

File test_someclass.py 文件test_someclass.py

import unittest
from someclass import SomeClass

class SomeClassTests(unittest.TestCase):

    def test_do_stuff(self):
        obj = SomeClass()
        self.assertFalse(obj.do_stuff())

def main():
    unittest.main()

if __name__ == '__main__':
    main()

What I want to do is show the debug messages while I am running the unit tests. 我想要做的是在运行单元测试时显示调试消息。 I tried to set the verbosity to debug from the unit test module: 我试图从单元测试模块设置详细程度调试:

import logging

# ....

def main():
    unittest.main()
    logging.basicConfig(level=logging.DEBUG)

This didn't work. 这没用。 What would be the way to achieve this? 实现这个目标的方法是什么? Even better would be enabling DEBUG verbosity for only one test. 更好的是只为一次测试启用DEBUG详细程度。

UPDATE: 更新:

Apparently it works when running it from the Python shell, but not in PyDev (it probably uses a different test runner). 显然它在从Python shell运行时有效,但在PyDev中运行它(它可能使用不同的测试运行器)。

call unittest.main() from your main() . main()调用unittest.main() main()

def main():
    logging.basicConfig(level=logging.DEBUG)
    unittest.main()

My output shows: 我的输出显示:

DEBUG:root:I just did some stuff
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

The basic example on unittests docs at: https://docs.python.org/2/library/unittest.html shows the simple way of calling and running a unit test from main. 关于单元测试文档的基本示例: https//docs.python.org/2/library/unittest.html显示了从main调用和运行单元测试的简单方法。

If you want to output debug messages on failures only, using nose test runner would be the easiest way to go since nose captures stdout and print it out on failures. 如果您只想在故障时输出调试消息,那么使用nose测试运行器将是最简单的方法,因为nose 捕获stdout并在出现故障时将其打印出来。 It works out of the box: 它开箱即用:

$ nosetests test.py
F
======================================================================
FAIL: test_stuff (test.SomeClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/../../test.py", line 7, in test_stuff
    self.assertFalse(True)
AssertionError: True is not false
-------------------- >> begin captured stdout << ---------------------
I just did some stuff

--------------------- >> end captured stdout << ----------------------

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

FAILED (failures=1)

where test.py contains: 其中test.py包含:

from unittest import TestCase

class SomeClass(TestCase):
    def test_stuff(self):
        print "I just did some stuff"
        self.assertFalse(True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 模拟:如何模拟从正在测试的 class 方法调用的 class - Python mock: How to mock a class being called from the class method being tested 如何将要测试的课程传递给单元测试课程 - how to pass a class to be tested to an unit test class 详细登录 Django 测试 - logging in Django test with verbosity Python:如何从OptionParser type =&#39;count&#39;记录logging.setLevel详细程度? - Python: How to logging.setLevel verbosity from OptionParser type='count'? 如何在 python 中使用 unittest.mock 测试 class 中的 object - How to replace an object inside a class being tested using unittest.mock in python unittest:测试时增加模块的详细程度 - unittest: increase module's verbosity when tested 如何在 class 中使用子进程并在 python 中进行测试 - How can subprocess be used in a class and tested in python Cassandra CQLEngine增加日志记录的详细程度 - Cassandra cqlengine increase logging verbosity (Python Unittest) 正在测试的模块无法导入其依赖项:ModuleNotFoundError - (Python Unittest) Module being tested cannot import its dependencies: ModuleNotFoundError 如何模拟与正在测试的方法相同的 class 中的方法? - How to mock a method from the same class that the method being tested?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM