PYTHON 接口测试 一个导出表格的接口报错
报错信息如下:
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 ==============================
错误的发生位置在文件 "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages_pytest\main.py" 中,具体错误的类型和位置被遮蔽了。同时,错误信息显示在运行测试的过程中有多个调用栈,但最初的错误可能是由其中任何一个调用栈中的代码所导致的。
报了一个字符编码的问题,可以在导出文件时设置utf8编码:encoding=‘utf-8’
应该是编码错误,gbk编解码器不能编码\ufffd字符,用utf-8来编码
这个错误提示说明在使用gbk编码器进行编码时遇到了无法处理的非法多字节序列,导致Unicode编码出现错误。通常情况下,这种错误通常是由于原始数据中包含了一些非法字符导致的。
1.解决这个问题的方法可以尝试使用其他编码格式,比如utf-8或者iso-8859-1,来避免gbk编码无法处理非法字符的问题。你可以在代码中增加编码参数,例如:
import csv
with open('output.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['第一列', '第二列'])
2.或者你可以在代码中增加异常处理,以便在遇到无法处理的字符时,能够跳过它并继续处理其他数据,例如:
import csv
with open('output.csv', 'w', encoding='gbk', newline='') as f:
writer = csv.writer(f)
for data in datas:
try:
writer.writerow(data)
except UnicodeEncodeError:
print(f"数据 {data} 包含无法处理的字符,已跳过。")
通过这些方法,你可以尝试解决这个问题。如果问题仍然存在,你可以考虑检查输入数据是否合法,或者尝试使用其他编码器来解决问题。
从信息中可以看到这个错误是在执行pytest时出现的,而且是由一个测试用例触发的。建议检查导出表格的接口的测试用例代码,看看是否存在错误。可以尝试把测试用例拆分开来,逐一排查是否可以定位出问题。如果不行,也可以在测试用例中加入更多的日志,以便更好地排查问题。
这个错误看起来像是在使用'gbk'编码将某个字符编码为字节序列时出错,'\ufffd' 表示的是未知字符。可能的原因是数据中包含了非'gbk'编码格式的字符,导致编码时出现了错误。
根据提供的错误信息,看起来是pytest在运行测试时出现了错误。报告的信息中并没有直接指出问题的原因。但是我们可以通过阅读报告来尝试找出问题所在。
建议您尝试执行这个测试用例,并确保没有任何语法错误或其他错误。您也可以尝试在IDE中以调试模式运行测试,这样您就可以逐行执行测试并查看变量的值,以确定测试的执行路径是否符合您的预期。
如果您能提供更多的上下文信息和代码,我可能可以提供更具体的建议。
是使用'gbk编码 为字节序列时出错,'\ufffd' 表示 是未知字符