HarmonyOS鸿蒙Next中run -c testcases/Example.json,命令行执行测试套件时一直报错,官方提供的模板项目也会报错

HarmonyOS鸿蒙Next中run -c testcases/Example.json,命令行执行测试套件时一直报错,官方提供的模板项目也会报错 想使用命令行run -c Example.json执行json 文件,一直报错,如果是 run -l Example 就完全没问题,专门下载了鸿蒙官方提供的模板HypiumProjectTemplate,一样有问题,很怀疑是xdriver 的问题,有同学这个命令执行成功

main_process("run -c testcases/Example.json -ta agent_mode:bin;screenshot:true")
/usr/local/bin/python3.10 /Users/zhouyunli/Chrome/HypiumProjectTemplate 2/main.py 
[2025-11-13 17:14:53,674] [8358709248] [Main] [INFO] [*************** xDevice Test Framework 6.0.6.210 Starting ***************]
[2025-11-13 17:14:53,674] [8358709248] [Analysis] [WARNING] [tracker dependency is not ok]
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/xdevice/_core/config/config_manager.py", line 75, in __init__
    tree = ElementTree.parse(self.file_path)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/etree/ElementTree.py", line 1222, in parse
    tree.parse(source, parser)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/etree/ElementTree.py", line 580, in parse
    self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/zhouyunli/Chrome/HypiumProjectTemplate 2/main.py", line 6, in <module>
    main_process("run -c testcases/Example.json -ta agent_mode:bin;screenshot:true")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/xdevice/__main__.py", line 43, in main_process
    console.console(args)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/xdevice/_core/command/console.py", line 122, in console
    argument = self.argument_parser(para_list)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/xdevice/_core/command/console.py", line 417, in argument_parser
    self._params_post_processing(options)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/xdevice/_core/command/console.py", line 448, in _params_post_processing
    Variables.config = UserConfigManager(config_file=options.config)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/xdevice/_core/config/config_manager.py", line 83, in __init__
    raise ParamError(err_msg) from error
_core.exception.ParamError: [Environment-0103002] Parsing the user_config.xml failed, error: not well-formed (invalid token): line 1, column 0 [Suggestions] 检查user_config.xml配置文件的格式

进程已结束,退出代码为 1

更多关于HarmonyOS鸿蒙Next中run -c testcases/Example.json,命令行执行测试套件时一直报错,官方提供的模板项目也会报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS Next中执行run -c testcases/Example.json报错,可能是测试框架配置或依赖问题。检查测试套件JSON文件格式是否正确,确保测试用例路径与项目结构匹配。确认DevEco Studio版本与HarmonyOS SDK版本兼容。查看命令行输出具体错误信息,通常涉及资源加载或权限配置异常。

更多关于HarmonyOS鸿蒙Next中run -c testcases/Example.json,命令行执行测试套件时一直报错,官方提供的模板项目也会报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


根据错误信息,问题出在解析 user_config.xml 文件时遇到了格式错误(not well-formed (invalid token): line 1, column 0)。这通常意味着该文件可能为空、编码不正确、或包含不可见的非法字符(如BOM头),导致XML解析器无法识别。

虽然你执行的是 run -c testcases/Example.json,但xDevice测试框架在启动时会默认加载或依赖 user_config.xml 这个配置文件。从堆栈跟踪看,框架在初始化 UserConfigManager 时失败了。

请按以下步骤排查:

  1. 定位并检查 user_config.xml 文件 在项目根目录或 resources 等配置目录下找到 user_config.xml 文件。

  2. 验证XML文件格式与内容

    • 检查文件是否为空:确保文件有实际内容。
    • 检查编码:使用文本编辑器(如VSCode、Notepad++)将文件编码明确保存为 UTF-8(无BOM)。文件开头的BOM字符是常见原因。
    • 检查基本XML结构:确保文件有正确的根元素,例如:
      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
          <!-- 你的配置内容 -->
      </configuration>
      
    • 检查隐藏字符:尝试删除第一行(<?xml ... ?> 声明)之前的所有空格或换行符。也可以将内容复制到一个新文件中,重新保存。
  3. 确认文件路径 错误显示框架在尝试解析 self.file_path。请确认 user_config.xml 是否位于框架期望的默认路径,或者是否通过其他方式(如环境变量、其他配置文件)指定了该文件。在Hypium模板项目中,该文件通常位于 resources 目录下。

根本原因分析: run -c 命令用于执行JSON格式的测试套件,但框架的初始化流程不依赖于JSON文件本身,而是依赖于其核心的XML配置文件(user_config.xml)。因此,即使JSON文件正确,如果XML配置文件损坏,框架也无法启动,这就是为什么 run -l(可能不依赖完整初始化)能成功,而 run -c 失败的原因。

临时解决方案: 如果找不到或无法立即修复 user_config.xml,可以尝试在命令中显式指定一个有效的配置文件路径(如果框架支持该参数),或者检查项目模板中是否提供了该文件的正确版本。

回到顶部