将测试文件保存为UTF-8格式
将测试文件保存为UTF-8格式可能会解决此问题。在代码编辑器中,可以尝试选择“另存为”或“编码”选项,并将文件编码设置为UTF-8。
设置环境变量PYTHONIOENCODING
在终端或命令行中,可以尝试设置环境变量PYTHONIOENCODING,以使用正确的编码格式运行pytest。例如,在Windows上,您可以输入以下命令:
set PYTHONIOENCODING=utf-8
在pytest.ini文件中添加配置
在pytest.ini文件中添加以下配置可以尝试解决此问题:
[pytest] addopts = --encoding=utf-8
如果以上方法没有解决问题,您可以尝试查看哪个文件导致了该错误,然后针对特定文件进行更改或调整编码。
最近学习pytest时,配置pytest.ini后,运行pytest报错,错误如下:
D:\pythonproject\AndroidUIauto\Test_App>pytest
Traceback (most recent call last):
File "d:\python\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\python\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Python\Python37\Scripts\pytest.exe\__main__.py", line 9, in
File "d:\python\python37\lib\site-packages\_pytest\config\__init__.py", line 60, in main
config = _prepareconfig(args, plugins)
File "d:\python\python37\lib\site-packages\_pytest\config\__init__.py", line 195, in _prepareconfig
pluginmanager=pluginmanager, args=args
File "d:\python\python37\lib\site-packages\pluggy\hooks.py", line 289, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "d:\python\python37\lib\site-packages\pluggy\manager.py", line 68, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "d:\python\python37\lib\site-packages\pluggy\manager.py", line 62, in
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "d:\python\python37\lib\site-packages\pluggy\callers.py", line 203, in _multicall
gen.send(outcome)
File "d:\python\python37\lib\site-packages\_pytest\helpconfig.py", line 93, in pytest_cmdline_parse
config = outcome.get_result()
File "d:\python\python37\lib\site-packages\pluggy\callers.py", line 80, in get_result
raise ex[1].with_traceback(ex[2])
File "d:\python\python37\lib\site-packages\pluggy\callers.py", line 187, in _multicall
res = hook_impl.function(*args)
File "d:\python\python37\lib\site-packages\_pytest\config\__init__.py", line 654, in pytest_cmdline_parse
self.parse(args)
File "d:\python\python37\lib\site-packages\_pytest\config\__init__.py", line 871, in parse
self._preparse(args, addopts=addopts)
File "d:\python\python37\lib\site-packages\_pytest\config\__init__.py", line 804, in _preparse
self._initini(args)
File "d:\python\python37\lib\site-packages\_pytest\config\__init__.py", line 727, in _initini
config=self,
File "d:\python\python37\lib\site-packages\_pytest\config\findpaths.py", line 122, in determine_setup
rootdir, inifile, inicfg = getcfg([ancestor], config=config)
File "d:\python\python37\lib\site-packages\_pytest\config\findpaths.py", line 35, in getcfg
iniconfig = py.iniconfig.IniConfig(p)
File "d:\python\python37\lib\site-packages\py\_vendored_packages\iniconfig.py", line 54, in __init__
tokens = self._parse(iter(f))
File "d:\python\python37\lib\site-packages\py\_vendored_packages\iniconfig.py", line 82, in _parse
for lineno, line in enumerate(line_iter):
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb0 in position 26: illegal multibyte sequence
解决办法:
更改pytest.ini 文件编码方式为GBK,再次运行不在报错。
原因:
目前未知,不知道具体为什么要这样更改,先记录以便后续跟进。