Python 为什么在使用webtest对函数进行单元测试时会遇到APPERROR?
我正在为一个函数编写一个单元测试用例。我正在测试是否能够成功进入状态代码为200的函数,但是无论我到目前为止尝试了什么,测试只通过了状态代码404。 所以我开始使用webtest库。下面是函数。 app.py 这是我的函数单元测试用例。 测试应用程序pyPython 为什么在使用webtest对函数进行单元测试时会遇到APPERROR?,python,python-3.x,unit-testing,python-unittest,Python,Python 3.x,Unit Testing,Python Unittest,我正在为一个函数编写一个单元测试用例。我正在测试是否能够成功进入状态代码为200的函数,但是无论我到目前为止尝试了什么,测试只通过了状态代码404。 所以我开始使用webtest库。下面是函数。 app.py 这是我的函数单元测试用例。 测试应用程序py class MyTestCase(unittest.TestCase): def setUp(self): self.app = create_app(db) self.app.config['TESTI
class MyTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app(db)
self.app.config['TESTING'] = True
self.app.config['LOGIN_DISABLED'] = True
self.app.config['WTF_CSRF_ENABLED'] = False
self.app.config['DEBUG'] = True
#self.client = self.app.test_client(self)
self.client = webtest.TestApp(self.app)
def tearDown(self):
pass
def test_settings_passed(self):
response = self.client.get('/settings')
self.assertEqual(response.status_code, 200)
这是完整的堆栈跟踪
test_app_new.py::MyTestCase::test_settings_passed FAILED [100%]ENV :default
############ INIT ############
############ INIT ############
############ INIT ############
test_app_new.py:36 (MyTestCase.test_settings_passed)
self = <test_app_new.MyTestCase testMethod=test_settings_passed>
def test_settings_passed(self):
#with self.client:
> response = self.client.post('/settings')
test_app_new.py:39:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../venv/lib/python3.6/site-packages/webtest/app.py:375: in post
content_type=content_type)
../venv/lib/python3.6/site-packages/webtest/app.py:753: in _gen_request
expect_errors=expect_errors)
../venv/lib/python3.6/site-packages/webtest/app.py:649: in do_request
self._check_status(status, res)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <webtest.app.TestApp object at 0x7f04e489f6d8>, status = None
res = <404 NOT FOUND text/html body=b'<!DOCTYP...p>\n'/232>
def _check_status(self, status, res):
if status == '*':
return
res_status = res.status
if (isinstance(status, string_types) and '*' in status):
if re.match(fnmatch.translate(status), res_status, re.I):
return
if isinstance(status, string_types):
if status == res_status:
return
if isinstance(status, (list, tuple)):
if res.status_int not in status:
raise AppError(
"Bad response: %s (not one of %s for %s)\n%s",
res_status, ', '.join(map(str, status)),
res.request.url, res)
return
if status is None:
if res.status_int >= 200 and res.status_int < 400:
return
raise AppError(
"Bad response: %s (not 200 OK or 3xx redirect for %s)\n%s",
res_status, res.request.url,
> res)
E webtest.app.AppError: Bad response: 404 NOT FOUND (not 200 OK or 3xx redirect for http://localhost/settings)
E <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
E <title>404 Not Found</title>
E <h1>Not Found</h1>
E <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
../venv/lib/python3.6/site-packages/webtest/app.py:681: AppError
Assertion failed
test\u app\u new.py::MyTestCase::test\u设置\u通过失败[100%]环境:默认值
############初始化############
############初始化############
############初始化############
test\u app\u new.py:36(MyTestCase.test\u settings\u通过)
自我=
def测试设置通过(自我):
#使用self.client:
>response=self.client.post(“/settings”)
测试应用程序新。py:39:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../venv/lib/python3.6/site-packages/webtest/app.py:375:in-post
内容类型=内容类型)
../venv/lib/python3.6/site packages/webtest/app.py:753:in\u gen\u请求
expect\u errors=expect\u errors)
../venv/lib/python3.6/site-packages/webtest/app.py:649:in-do\u请求
自我检查状态(状态、分辨率)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self=,status=None
res=
定义检查状态(自我、状态、恢复):
如果状态=='*':
返回
res_status=res.status
如果(isinstance(状态,字符串类型)和“*”处于状态):
如果重新匹配(fnmatch.translate(status)、res_status、re.I):
返回
如果isinstance(状态、字符串类型):
如果状态==恢复状态:
返回
如果isinstance(状态,(列表,元组)):
如果res.status\u int未处于状态:
起诉书(
“错误响应:%s(不是%s中的一个,用于%s)\n%s”,
res_status,,,join(映射(str,status)),
res.request.url,res)
返回
如果状态为“无”:
如果res.status\u int>=200且res.status\u int<400:
返回
起诉书(
“错误响应:%s(不是200 OK或%s的3xx重定向)\n%s”,
res_状态,res.request.url,
>(res)
E webtest.app.AppError:错误响应:未找到404(未找到200 OK或3xx重定向http://localhost/settings)
E
E 404未找到
E未找到
E在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试
../venv/lib/python3.6/site packages/webtest/app.py:681:AppError
断言失败
我如何解决这个问题?我不希望提出上诉,然后让测试用例通过。请帮忙
test_app_new.py::MyTestCase::test_settings_passed FAILED [100%]ENV :default
############ INIT ############
############ INIT ############
############ INIT ############
test_app_new.py:36 (MyTestCase.test_settings_passed)
self = <test_app_new.MyTestCase testMethod=test_settings_passed>
def test_settings_passed(self):
#with self.client:
> response = self.client.post('/settings')
test_app_new.py:39:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../venv/lib/python3.6/site-packages/webtest/app.py:375: in post
content_type=content_type)
../venv/lib/python3.6/site-packages/webtest/app.py:753: in _gen_request
expect_errors=expect_errors)
../venv/lib/python3.6/site-packages/webtest/app.py:649: in do_request
self._check_status(status, res)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <webtest.app.TestApp object at 0x7f04e489f6d8>, status = None
res = <404 NOT FOUND text/html body=b'<!DOCTYP...p>\n'/232>
def _check_status(self, status, res):
if status == '*':
return
res_status = res.status
if (isinstance(status, string_types) and '*' in status):
if re.match(fnmatch.translate(status), res_status, re.I):
return
if isinstance(status, string_types):
if status == res_status:
return
if isinstance(status, (list, tuple)):
if res.status_int not in status:
raise AppError(
"Bad response: %s (not one of %s for %s)\n%s",
res_status, ', '.join(map(str, status)),
res.request.url, res)
return
if status is None:
if res.status_int >= 200 and res.status_int < 400:
return
raise AppError(
"Bad response: %s (not 200 OK or 3xx redirect for %s)\n%s",
res_status, res.request.url,
> res)
E webtest.app.AppError: Bad response: 404 NOT FOUND (not 200 OK or 3xx redirect for http://localhost/settings)
E <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
E <title>404 Not Found</title>
E <h1>Not Found</h1>
E <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
../venv/lib/python3.6/site-packages/webtest/app.py:681: AppError
Assertion failed