0
私の問題は、私のunittestsのいくつかは何度も実行されていますが、私はその理由を理解できません。ここでPython unittestsが複数回実行される
は、テストとコードです:
# -*- coding: utf-8 -*-
"""Tests for api.resource.decorators"""
import os
import errno
import unittest
import logging
from socket import gaierror, error as socketerror
from lockfile import LockFile
from logging import FileHandler, Formatter
from api.resource.decorators import handle_general_exceptions
class TestHandleGeneralExceptions(unittest.TestCase):
_tmp_log_path = '/tmp/test_handle_general_exceptions_log'
_tmp_lock_path = '/tmp/test_handle_general_exceptions_lock'
_lock = None
_logger = None
def setUp(self):
self._lock = self._get_lock()
self._logger = self._get_logger()
def tearDown(self):
self._remove_lockfiles()
def _remove_lockfiles(self):
# TODO: solve without system call
os.system('rm -f %s*' % self._tmp_lock_path)
def _get_lock(self):
return LockFile(self._tmp_lock_path)
def _get_logger(self):
logger = logging.getLogger('handle_general_exceptions')
logger.propagate = False # Only use this Logger
handler = FileHandler(self._tmp_log_path)
formatter = Formatter('%(asctime)s [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel('INFO')
return logger
def _raise_gaierror(self):
self._lock.acquire(-1)
raise gaierror(-2, 'Name or service not known')
def _raise_connection_timed_out(self):
self._lock.acquire(-1)
raise socketerror(errno.ETIMEDOUT, 'Connection Timed Out')
def _raise_connection_refused(self):
self._lock.acquire(-1)
raise socketerror(errno.ECONNREFUSED, 'Connection Refused')
def _is_locked(self):
return os.path.exists(self._tmp_lock_path + '.lock') == 1
def test_not_locked_if_gaierror_occurred(self):
self._raise_gaierror = handle_general_exceptions(
self._lock, self._logger, self._raise_gaierror)
self._logger.info('¯¯¯¯¯¯¯¯¯¯¯¯ _raise_gaierror() ¯¯¯¯¯¯¯¯¯¯¯¯')
self._raise_gaierror()
self._logger.info('____________ End _raise_gaierror() ____________')
self.assertFalse(self._is_locked())
def test_not_locked_if_connection_timed_out(self):
self._raise_connection_timed_out = \
handle_general_exceptions(self._lock, self._logger,
self._raise_connection_timed_out)
self._logger.info(
'¯¯¯¯¯¯¯¯¯¯¯¯ _raise_connection_timed_out() ¯¯¯¯¯¯¯¯¯¯¯¯')
self._raise_connection_timed_out()
self._logger.info(
'____________ End _raise_connection_timed_out() ____________')
self.assertFalse(self._is_locked())
def test_not_locked_if_connection_refused(self):
self._raise_connection_refused = \
handle_general_exceptions(self._lock, self._logger,
self._raise_connection_refused)
self._logger.info(
'¯¯¯¯¯¯¯¯¯¯¯¯ _raise_connection_refused() ¯¯¯¯¯¯¯¯¯¯¯¯')
self._raise_connection_refused()
self._logger.info(
'____________ End _raise_connection_refused() ____________')
self.assertFalse(self._is_locked())
そしてテストは、このようなスクリプトを介して実行されます。
#!/bin/sh
python -m unittest discover api.tests -t .
今テストtest_not_locked_if_gaierror_occurred()
が3回実行されます。
テストtest_not_locked_if_connection_timed_out()
が2回実行されます。
そして、テストtest_not_locked_if_connection_refused()
が1回実行されます。ログの出力だ
:
2016-10-21 13:20:31 [INFO] ¯¯¯¯¯¯¯¯¯¯¯¯ _raise_connection_refused() ¯¯¯¯¯¯¯¯¯¯¯¯
2016-10-21 13:20:31 [ERROR] error: [Errno 111] Connection Refused
2016-10-21 13:20:31 [INFO] ____________ End _raise_connection_refused() ____________
2016-10-21 13:20:31 [INFO] ¯¯¯¯¯¯¯¯¯¯¯¯ _raise_connection_timed_out() ¯¯¯¯¯¯¯¯¯¯¯¯
2016-10-21 13:20:31 [INFO] ¯¯¯¯¯¯¯¯¯¯¯¯ _raise_connection_timed_out() ¯¯¯¯¯¯¯¯¯¯¯¯
2016-10-21 13:20:31 [ERROR] error: [Errno 110] Connection Timed Out
2016-10-21 13:20:31 [ERROR] error: [Errno 110] Connection Timed Out
2016-10-21 13:20:31 [INFO] ____________ End _raise_connection_timed_out() ____________
2016-10-21 13:20:31 [INFO] ____________ End _raise_connection_timed_out() ____________
2016-10-21 13:20:31 [INFO] ¯¯¯¯¯¯¯¯¯¯¯¯ _raise_gaierror() ¯¯¯¯¯¯¯¯¯¯¯¯
2016-10-21 13:20:31 [INFO] ¯¯¯¯¯¯¯¯¯¯¯¯ _raise_gaierror() ¯¯¯¯¯¯¯¯¯¯¯¯
2016-10-21 13:20:31 [INFO] ¯¯¯¯¯¯¯¯¯¯¯¯ _raise_gaierror() ¯¯¯¯¯¯¯¯¯¯¯¯
2016-10-21 13:20:31 [ERROR] gaierror: [Errno -2] Name or service not known
2016-10-21 13:20:31 [ERROR] gaierror: [Errno -2] Name or service not known
2016-10-21 13:20:31 [ERROR] gaierror: [Errno -2] Name or service not known
2016-10-21 13:20:31 [INFO] ____________ End _raise_gaierror() ____________
2016-10-21 13:20:31 [INFO] ____________ End _raise_gaierror() ____________
2016-10-21 13:20:31 [INFO] ____________ End _raise_gaierror() ____________
'handle_general_exceptions'は自分で書かれた関数ですよね?私には問題はそこにあるかもしれません。 – Nf4r
はい、デコレータです。 – GiftZwergrapper
あなたはそれを見せてもらえますか? – Nf4r