PYTHON 接口测试 pytetst终止

PYTHON 接口测试 一个导出表格的接口报错

img

报错信息如下:
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\main.py", line 270, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\main.py", line 324, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_hooks.py", line 265, in call
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\main.py", line 349, in pytest_runtestloop
INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_hooks.py", line 265, in call
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\runner.py", line 112, in pytest_runtest_protocol
INTERNALERROR> runtestprotocol(item, nextitem=nextitem)
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\runner.py", line 131, in runtestprotocol
INTERNALERROR> reports.append(call_and_report(item, "call", log))
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\runner.py", line 224, in call_and_report
INTERNALERROR> hook.pytest_runtest_logreport(report=report)
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_hooks.py", line 265, in call
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\pytest_plugin.py", line 310, in pytest_runtest_logreport
INTERNALERROR> self.report_test_failure(test_id, report)
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\pytest_plugin.py", line 225, in report_test_failure
INTERNALERROR> self.report_test_output(report, test_id)
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\pytest_plugin.py", line 206, in report_test_output
INTERNALERROR> dump_test_stdout(self.teamcity, test_id, test_id, data)
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\common.py", line 73, in dump_test_stdout
INTERNALERROR> messages.testStdOut(test_id, chunk, flowId=flow_id)
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\messages.py", line 187, in testStdOut
INTERNALERROR> self.message('testStdOut', name=testName, out=out, flowId=flowId)
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm_jb_runner_tools.py", line 117, in message
INTERNALERROR> _old_service_messages.message(self, messageName, **properties)
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\messages.py", line 101, in message
INTERNALERROR> retry_on_EAGAIN(self.output.write)(self.encode(message))
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pycharm\teamcity\messages.py", line 68, in encode
INTERNALERROR> value = value.encode(self.encoding)
INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> UnicodeEncodeError: 'gbk' codec can't encode character '\ufffd' in position 1217: illegal multibyte sequence

============================== 1 failed in 1.68s ==============================

这个错误通常意味着在尝试将某些字符编码为GBK格式时出现了问题,其中某些字符可能无法使用GBK编码。可能原因如下:

输入的字符串中包含了非GBK编码的字符,例如Unicode字符或其他非中文字符。

编码时使用的编码方式与应用程序或系统默认的编码方式不一致。

导出表格的接口中存在编码设置错误或格式不正确的字符。

您可以尝试以下解决方法:

使用Python的encode()方法将字符串编码为GBK格式,例如:text.encode('gbk', 'ignore')。'ignore'参数可以忽略无法编码的字符。

检查应用程序或系统的默认编码方式,并在代码中指定相同的编码方式。例如:import sys; sys.setdefaultencoding('gbk')。

检查导出表格的接口代码,确认编码设置正确,可以在代码中设置:response.headers['Content-Type'] = 'text/csv; charset=gbk'。

另外,如果在处理数据时需要使用中文字符,推荐使用UTF-8编码方式,因为它是一种通用的字符编码方式,适用于多种语言。