Python 2.7 unittest在運行單個腳本文件時運行良好,但在Pycharm Run中運行文件夾時失敗/調試配置。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()
這種運作良好,當我設置的Python測試腳本 'Jetbrains的Pycharm運行/調試配置'。所有5個測試通過了。
但是,當我設置Python測試文件夾,其中包含'運行/調試配置'中的'LogTestCase'腳本。 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
上述所有,謝謝。
我認爲你是對的。但是如果我使用'mock',我應該在'from lib import log'之前''修改'config'模塊。但是'log'模塊已經在其他測試中導入。我應該在這個測試中重新定義一個'log' func並模擬真正的'log'模塊嗎? – coffin5257
@ coffin5257我編輯了我的答案。 –