pytest运行报错:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaf in position 82: illegal multibyte

pytest运行报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaf in position 82: illegal multibyte sequence

 

 

D:\Python\environment\python3.9.7\python3.exe D:/Python/nas_autotest/run.py
Traceback (most recent call last):
  File "D:\Python\nas_autotest\run.py", line 15, in <module>
    pytest.main(["--junitxml=reports/results.xml"])  # 用例生成执行统计结果
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\__init__.py", line 143, in main
    config = _prepareconfig(args, plugins)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\__init__.py", line 318, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "D:\Python\environment\python3.9.7\lib\site-packages\pluggy\_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "D:\Python\environment\python3.9.7\lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "D:\Python\environment\python3.9.7\lib\site-packages\pluggy\_callers.py", line 55, in _multicall
    gen.send(outcome)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\helpconfig.py", line 100, in pytest_cmdline_parse
    config: Config = outcome.get_result()
  File "D:\Python\environment\python3.9.7\lib\site-packages\pluggy\_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "D:\Python\environment\python3.9.7\lib\site-packages\pluggy\_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\__init__.py", line 1003, in pytest_cmdline_parse
    self.parse(args)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\__init__.py", line 1283, in parse
    self._preparse(args, addopts=addopts)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\__init__.py", line 1157, in _preparse
    self._initini(args)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\__init__.py", line 1076, in _initini
    rootpath, inipath, inicfg = determine_setup(
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\findpaths.py", line 184, in determine_setup
    rootdir, inipath, inicfg = locate_config([ancestor])
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\findpaths.py", line 106, in locate_config
    ini_config = load_config_dict_from_file(p)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\findpaths.py", line 45, in load_config_dict_from_file
    iniconfig = _parse_ini_config(filepath)
  File "D:\Python\environment\python3.9.7\lib\site-packages\_pytest\config\findpaths.py", line 30, in _parse_ini_config
    return iniconfig.IniConfig(str(path))
  File "D:\Python\environment\python3.9.7\lib\site-packages\iniconfig\__init__.py", line 54, in __init__
    tokens = self._parse(iter(f))
  File "D:\Python\environment\python3.9.7\lib\site-packages\iniconfig\__init__.py", line 82, in _parse
    for lineno, line in enumerate(line_iter):
UnicodeDecodeError: 'gbk' codec can't decode byte 0x8d in position 590: illegal multibyte sequence

Process finished with exit code 1

解决方式:
第一种:
将pytest.ini文件中的中文字符串去掉

如果需要保留中文备注,则使用第二种方法:
点击报错内容倒数第二个

File "D:\Python\environment\python3.9.7\lib\site-packages\iniconfig\__init__.py", line 54, in __init__
    tokens = self._parse(iter(f))

这个文件 在52行吧当前的文件打开方式添加上 'utf-8'的解码方式

 

f = open(self.path,encoding='utf-8')

 

posted @ 2024-06-03 16:28  陈晓猛  阅读(31)  评论(0编辑  收藏  举报