引发异常但未被 assertRaises 捕获
Exception raised but not caught by assertRaises
我正在尝试测试我的身份验证是否失败。 assertRaises
引发了异常,但未捕获异常。我在这里错过了什么?
def test_auth(self):
from graphql_jwt.exceptions import PermissionDenied
with self.assertRaises(PermissionDenied):
response = self.client.execute(self.query)
回溯:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/home/dan/game/venv/lib/python3.7/site-packages/promise/promise.py", line 487, in _resolve_from_executor
executor(resolve, reject)
File "/home/dan/game/venv/lib/python3.7/site-packages/promise/promise.py", line 754, in executor
return resolve(f(*args, **kwargs))
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
return next(*args, **kwargs)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphene_django/filter/fields.py", line 106, in connection_resolver
**args
File "/home/dan/game/venv/lib/python3.7/site-packages/graphene_django/fields.py", line 156, in connection_resolver
iterable = resolver(root, info, **args)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql_jwt/decorators.py", line 31, in wrapper
return func(info.context, *args, **kwargs)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql_jwt/decorators.py", line 43, in wrapper
raise exceptions.PermissionDenied()
graphql.error.located_error.GraphQLLocatedError: You do not have permission to perform this action
F.
======================================================================
FAIL: test_auth (api.tests.test_mutations.TestGame)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/dan/game/api/tests/test_mutations.py", line 57, in test_auth
response = self.client.execute(self.query)
AssertionError: PermissionDenied not raised
正在引发异常 here。
您的测试没有捕捉到 PermissionDenied
异常,因为您 运行 代码中的某些内容将该异常包装在 graphql.error.located_error.GraphQLLocatedError
的实例中。因为您正在检查错误的异常类型,所以测试失败。
我对你用的库不是很了解,异常类型的隐形改变看起来很可怕mis-feature(至少应该加上改变异常类型的代码到异常回溯,以便您可以对其进行调试)。但是您可以通过捕获包装的异常并重新抛出原始异常来解决此问题:
def test_auth(self):
from graphql_jwt.exceptions import PermissionDenied
from graphql.error.located_error import GraphQLLocatedError
with self.assertRaises(PermissionDenied):
try:
response = self.client.execute(self.query)
except GraphQLLocatedError as e:
raise e.original_error
GraphQL,根据定义捕获所有异常并将错误放入响应的错误部分。
如果您正在测试查询的执行 (self.client.execute(... query ...)),您应该得到结果并验证它有一个符合您预期的错误部分。
一种更简单的方法是专门测试解析器 - 直接调用 resolve_entity 而不是通过 GraphQL 执行层,并像测试任何其他 Python 函数一样对其进行测试。
我正在尝试测试我的身份验证是否失败。 assertRaises
引发了异常,但未捕获异常。我在这里错过了什么?
def test_auth(self):
from graphql_jwt.exceptions import PermissionDenied
with self.assertRaises(PermissionDenied):
response = self.client.execute(self.query)
回溯:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/home/dan/game/venv/lib/python3.7/site-packages/promise/promise.py", line 487, in _resolve_from_executor
executor(resolve, reject)
File "/home/dan/game/venv/lib/python3.7/site-packages/promise/promise.py", line 754, in executor
return resolve(f(*args, **kwargs))
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
return next(*args, **kwargs)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphene_django/filter/fields.py", line 106, in connection_resolver
**args
File "/home/dan/game/venv/lib/python3.7/site-packages/graphene_django/fields.py", line 156, in connection_resolver
iterable = resolver(root, info, **args)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql_jwt/decorators.py", line 31, in wrapper
return func(info.context, *args, **kwargs)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql_jwt/decorators.py", line 43, in wrapper
raise exceptions.PermissionDenied()
graphql.error.located_error.GraphQLLocatedError: You do not have permission to perform this action
F.
======================================================================
FAIL: test_auth (api.tests.test_mutations.TestGame)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/dan/game/api/tests/test_mutations.py", line 57, in test_auth
response = self.client.execute(self.query)
AssertionError: PermissionDenied not raised
正在引发异常 here。
您的测试没有捕捉到 PermissionDenied
异常,因为您 运行 代码中的某些内容将该异常包装在 graphql.error.located_error.GraphQLLocatedError
的实例中。因为您正在检查错误的异常类型,所以测试失败。
我对你用的库不是很了解,异常类型的隐形改变看起来很可怕mis-feature(至少应该加上改变异常类型的代码到异常回溯,以便您可以对其进行调试)。但是您可以通过捕获包装的异常并重新抛出原始异常来解决此问题:
def test_auth(self):
from graphql_jwt.exceptions import PermissionDenied
from graphql.error.located_error import GraphQLLocatedError
with self.assertRaises(PermissionDenied):
try:
response = self.client.execute(self.query)
except GraphQLLocatedError as e:
raise e.original_error
GraphQL,根据定义捕获所有异常并将错误放入响应的错误部分。 如果您正在测试查询的执行 (self.client.execute(... query ...)),您应该得到结果并验证它有一个符合您预期的错误部分。
一种更简单的方法是专门测试解析器 - 直接调用 resolve_entity 而不是通过 GraphQL 执行层,并像测试任何其他 Python 函数一样对其进行测试。