Python单元测试运行多次

时间:2016-10-21 11:12:30

标签: python python-2.7 unit-testing testing python-unittest

我遇到了问题,我的一些单元测试运行了多次,但我无法弄清楚原因。

以下是测试代码:

# -*- 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()执行一次。

这是日志记录的输出:

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() ____________

1 个答案:

答案 0 :(得分:0)

我自己发现问题,测试没有多次运行。 奇怪的日志记录输出的原因是,我使用相同的记录器进行所有测试。

我不得不改变以下

def _get_logger(self):
   logger = logging.getLogger('handle_general_exceptions') 

这样的事情

def _get_logger(self, logger_suffix):
    logger = logging.getLogger('handle_general_exceptions' + logger_suffix)

这样,它仍然只记录到一个文件,但它使用不同的记录器。