Python 2.7 UnitTest在运行单个脚本文件时运行良好,但在Pycharm运行/调试配置中运行文件夹时失败

时间:2016-02-20 15:22:39

标签: python python-2.7 pycharm python-unittest

Python 2.7 unittest在运行单个脚本文件时运行良好,但在Pycharm运行/调试配置中运行文件夹时失败。

单位测试案例:

import unittest
import sys
import os
import time

from tests import test_config
from test_config import LOGGING_FILE_PATH, LOGGING_FILE_NAME


class LogTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        sys.modules["config"] = test_config
        from lib import log
        cls.log = log

def check_log(self, msg):
    try:
        fp = open(os.path.join(LOGGING_FILE_PATH, LOGGING_FILE_NAME))
    except IOError as e:
        raise e
    else:
        with fp:
            self.assertIn(msg, fp.read())

def tearDown(self):
    try:
        fp = open(os.path.join(LOGGING_FILE_PATH, LOGGING_FILE_NAME), 'w')
    except IOError as e:
        raise e
    else:
        with fp:
            fp.truncate()

def setUp(self):
    time.sleep(0)

@classmethod
def tearDownClass(cls):
    if os.path.isfile(os.path.join(LOGGING_FILE_PATH, LOGGING_FILE_NAME)):
        os.remove(os.path.join(LOGGING_FILE_PATH, LOGGING_FILE_NAME))

def test_log_debug(self):
    msg = 'test log debug'
    self.log.debug(msg)
    self.check_log(msg)

def test_log_info(self):
    msg = 'test log info'
    self.log.info(msg)
    self.check_log(msg)

def test_log_warning(self):
    msg = 'test log warning'
    self.log.warning(msg)
    self.check_log(msg)

def test_log_error(self):
    msg = 'test log error'
    self.log.error(msg, exc_info=False)
    self.check_log(msg)

def test_log_critical(self):
    msg = 'test log critical'
    self.log.critical(msg)
    self.check_log(msg)


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

当我在Jetbrains Pycharm运行/调试配置中设置Python测试脚本时,这很有效。所有5项测试都通过了。

但是当我设置包含' LogTestCase'的Python测试文件夹时'运行/调试配置'中的脚本。 5个测试失败(全部10个测试。另外5个测试在另一个文件中)。

结果:

2016-02-20 23:08:59,901 - test_log.py:65:test_log_critical - CRITICAL - test log critical

2016-02-20 23:08:59,902 - log.py:136:error - ERROR - test log error

2016-02-20 23:08:59,903 - test_log.py:50:test_log_info - INFO - test log info

Error
Traceback (most recent call last):
  File "project_path/tests/test_log.py", line 66, in test_log_critical
    self.check_log(msg)
  File "project_path/tests/test_log.py", line 21, in check_log
    raise e
IOError: [Errno 2] No such file or directory: 'project_path/tests/test_log'


Failure
Expected :'test log debug'
Actual   :''
 <Click to see difference>

Traceback (most recent call last):
  File "project_path/tests/test_log.py", line 46, in test_log_debug
    self.check_log(msg)
  File "project_path/tests/test_log.py", line 24, in check_log
    self.assertIn(msg, fp.read())
AssertionError: 'test log debug' not found in ''


Failure
Expected :'test log error'
Actual   :''
 <Click to see difference>

Traceback (most recent call last):
  File "project_path/tests/test_log.py", line 61, in test_log_error
    self.check_log(msg)
  File "project_path/tests/test_log.py", line 24, in check_log
    self.assertIn(msg, fp.read())
AssertionError: 'test log error' not found in ''


Failure
Expected :'test log info'
Actual   :''
 <Click to see difference>

Traceback (most recent call last):
  File "project_path/tests/test_log.py", line 51, in test_log_info
    self.check_log(msg)
  File "project_path/tests/test_log.py", line 24, in check_log
    self.assertIn(msg, fp.read())
AssertionError: 'test log info' not found in ''


Failure
Expected :'test log warning'
Actual   :''
 <Click to see difference>

Traceback (most recent call last):
  File "project_path/tests/test_log.py", line 56, in test_log_warning
    self.check_log(msg)
  File "project_path/tests/test_log.py", line 24, in check_log
    self.assertIn(msg, fp.read())
AssertionError: 'test log warning' not found in ''

2016-02-20 23:08:59,903 - test_log.py:55:test_log_warning - WARNING - test log warning

Process finished with exit code 0

以上所有,谢谢。

1 个答案:

答案 0 :(得分:0)

我可以猜到的是,在另一个测试中你还有类似的东西

@classmethod
def setUpClass(cls):
    sys.modules["config"] = test_config
    from lib import log
    cls.log = log

当您单独运行测试时,您没有任何问题。但是当你在像pycharm do这样的测试套件中运行所有这些时,第二个from lib import log没有效果(模块已经加载):log模块,我可以通过{{{{}}来猜测它的配置1}}模块仍然使用上一次测试中配置的模块。

我的建议是查看mock框架(由python 2.7中的pip提供)及其patch方法:通过这种方式,您可以完全控制上下文,您可以在其中应用黑客改变。

您也可以尝试使用config来解决这个问题但我强烈反对您如果您开始使用这些技巧而难以维持测试隔离并且您会遇到一些各种非常几率的行为。深入研究reload:你会喜欢它。

[修改

没有看到patch如何使用log我无法通过真正的解决方案真正回答您的问题。我可以猜测补丁的最佳位置是config模块,但如何做到这一点是不可能的。

  • log模块使用什么日志?
  • 如何进口?

这里真正需要的是一种以某种方式配置记录器并设置路径和文件名的方法。我想你可以通过在config模块中引入一个新函数或通过补丁来实现它,但细节真的是扭曲的log模块代码。