Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在单元测试中使用decorator引发错误?_Python 3.x_Unit Testing - Fatal编程技术网

Python 3.x 如何在单元测试中使用decorator引发错误?

Python 3.x 如何在单元测试中使用decorator引发错误?,python-3.x,unit-testing,Python 3.x,Unit Testing,我想在单元测试失败之前做一些事情,并使用decorator来完成 这是我的密码: import requests import unittest import test class ExceptionHandler(object): def __init__(self, f): self.f = f def __call__(self, *args, **kwargs): try: self.f(*args, **kw

我想在单元测试失败之前做一些事情,并使用decorator来完成

这是我的密码:

import requests
import unittest
import test


class ExceptionHandler(object):
    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        try:
            self.f(*args, **kwargs)
        except Exception as err:
            print('do smth')
            raise err


class Testing(unittest.TestCase):
    @ExceptionHandler
    def test_connection_200(self):
        r = requests.get("http://www.google.com")
        self.assertEqual(r.status_code, 400)


if __name__ == '__main__':
    unittest.main(verbosity=2)
但这给我带来了一个:

TypeError: test_connection_200() missing 1 required positional argument: 'self'
当我的测试失败时,我如何做一些事情,然后有unitest的正常失败行为

编辑: 我想在测试失败之前做一些事情,比如写一个日志,然后继续正常的失败过程

如果可能的话,找个装饰师

由于@Thymen:

import requests
import unittest
import test


class ExceptionHandler(object):
    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        try:
            self.f(*args, **kwargs)
        except Exception as err:
            print('do smth')
            raise err


class Testing(unittest.TestCase):
    def test_connection_200(self):
        @ExceptionHandler
        def test_connection_bis(self):
            r = requests.get("https://www.google.com")
            print(r.status_code)
            self.assertEqual(r.status_code, 400)
        test_connection_bis(self)


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

我的评论可能不太清楚,因此在此以代码形式给出解决方案

class Testing(unittest.TestCase):

    def test_connection_200(self):

        @ExceptionHandler
        def test_connection():
            r = requests.get("http://www.google.com")
            self.assertEqual(r.status_code, 400)

        with self.assertRaises(AssertionError):
            test_connection()
这样做的原因是不依赖于测试调用(
test\u connection\u 200
),也不依赖于您尝试测试的实际功能(
ExceptionHandler

编辑 线路

with self.assertRaises(AssertionError):
    test_connection()
检查
test\u connection()
是否引发
AssertionError
。如果它没有引发此错误,则测试将失败


如果希望测试失败(因为
AssertionError
),可以使用
语句删除
,只调用
test\u connection()
。这将直接导致测试失败。

您可以在测试方法内部创建一个函数。这使得它独立于对测试方法的调用。在任何情况下,使用
def测试_连接_200(自身)@例外处理程序;def测试_异常();你的测试…
。我希望我的测试顺利,我想在测试失败之前做点什么@百里香d您的解决方案测试不会失败:/test不会失败,因为它正在捕获一个
AssertionError
。这就是
self.assertRaises
检查的内容。如果希望它提供错误消息,可以使用self.assertRaises(AssertionError)
删除行
,只调用
test\u connection()
。这将导致一个
断言错误
。是的,它按照我的要求工作,谢谢!