本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
教程:开发简单的 IDT 测试套件
测试套件结合了以下内容:
-
测试包含测试逻辑的可执行文件
-
描述测试套件的 JSON 配置文件
本教程介绍如何使用适用于 FreeRTOS 的 IDT 开发一个包含单个测试用例的 Python 测试套件。在本教程中,您将完成以下步骤:
Prerequisites
要完成本教程,您需要:
-
主机要求
-
最新版本的 AWS IoT Device Tester
-
Python
3.7 或更高版本 要检查您的计算机上安装的 Python 版本,请运行以下命令:
python3 --version
在 Windows 上,如果使用此命令会返回错误,请改用
python --version
。如果返回的版本号为 3.7 或更高,请在 Powershell 终端中运行以下命令以将python3
设置为python
命令的别名。Set-Alias -Name "python3" -Value "python"
如果没有返回版本信息,或者版本号低于 3.7,请按照下载 Python
中的说明安装 Python 3.7+。有关更多信息,请参阅 Python 文档 。 -
要验证
urllib3
是否已正确安装,请运行以下命令:python3 -c 'import urllib3'
如果未安装
urllib3
,请运行以下命令进行安装:python3 -m pip install urllib3
-
-
设备要求
-
一台设备,该设备具有 Linux 操作系统和到主机所在网络的网络连接。
建议您将 Raspberry Pi
与 Raspberry Pi OS 配合使用。确保在 Raspberry Pi 上设置 SSH 以远程连接到该实例。
-
创建测试套件目录
IDT 从逻辑上将测试用例分为每个测试套件中的测试组。每个测试用例都必须位于一个测试组中。在本教程中,创建一个名为 MyTestSuite_1.0.0
的文件夹并在该文件夹中创建以下目录树:
MyTestSuite_1.0.0 └── suite └── myTestGroup └── myTestCase
创建 JSON 配置文件
您的测试套件必须包含以下必需的 JSON 配置文件:
必需的 JSON 文件
suite.json
-
包含有关测试套件的信息。请参阅配置 suite.json.
group.json
-
包含有关测试组的信息。您必须为测试套件中的每个测试组创建一个
group.json
文件。请参阅配置 group.json. test.json
-
包含有关测试用例的信息。您必须为测试套件中的每个测试用例创建一个
test.json
文件。请参阅配置 test.json.
-
在
MyTestSuite_1.0.0/suite
文件夹中,创建一个具有以下结构的suite.json
文件:{ "id": "MyTestSuite_1.0.0", "title": "My Test Suite", "details": "This is my test suite.", "userDataRequired": false }
-
在
MyTestSuite_1.0.0/myTestGroup
文件夹中,创建一个具有以下结构的group.json
文件:{ "id": "MyTestGroup", "title": "My Test Group", "details": "This is my test group.", "optional": false }
-
在
MyTestSuite_1.0.0/myTestGroup/myTestCase
文件夹中,创建一个具有以下结构的test.json
文件:{ "id": "MyTestCase", "title": "My Test Case", "details": "This is my test case.", "execution": { "timeout": 300000, "linux": { "cmd": "python3", "args": [ "myTestCase.py" ] }, "mac": { "cmd": "python3", "args": [ "myTestCase.py" ] }, "win": { "cmd": "python3", "args": [ "myTestCase.py" ] } } }
您的 MyTestSuite_1.0.0
文件夹的目录树现在应与以下内容类似:
MyTestSuite_1.0.0 └── suite ├── suite.json └── myTestGroup ├── group.json └── myTestCase └── test.json
获取 IDT 客户端开发工具包
您可以使用 IDT 客户端开发工具包使 IDT 能够与所测试设备交互并报告测试结果。在本教程中,您将使用开发工具包的 Python 版本。
从
文件夹中,将 <device-tester-extract-location>
/sdks/python/idt_client
文件夹复制到 MyTestSuite_1.0.0/suite/myTestGroup/myTestCase
文件夹。
要验证开发工具包是否已成功复制,请运行以下命令。
cd MyTestSuite_1.0.0/suite/myTestGroup/myTestCase python3 -c 'import idt_client'
创建测试用例可执行文件
测试用例可执行文件包含您要运行的测试逻辑。一个测试套件可以包含多个测试用例可执行文件。在本教程中,您将仅创建一个测试用例可执行文件。
-
创建测试套件文件。
在
MyTestSuite_1.0.0/suite/myTestGroup/myTestCase
文件夹中,创建一个包含以下内容的myTestCase.py
文件:from idt_client import * def main(): # Use the client SDK to communicate with IDT client = Client() if __name__ == "__main__": main()
-
使用客户端开发工具包函数将以下测试逻辑添加到您的
myTestCase.py
文件:-
在所测试设备上运行 SSH 命令。
from idt_client import * def main(): # Use the client SDK to communicate with IDT client = Client()
# Create an execute on device request exec_req = ExecuteOnDeviceRequest(ExecuteOnDeviceCommand("echo 'hello world'")) # Run the command exec_resp = client.execute_on_device(exec_req) # Print the standard output print(exec_resp.stdout)
if __name__ == "__main__": main() -
将测试结果发送到 IDT。
from idt_client import * def main(): # Use the client SDK to communicate with IDT client = Client() # Create an execute on device request exec_req = ExecuteOnDeviceRequest(ExecuteOnDeviceCommand("echo 'hello world'")) # Run the command exec_resp = client.execute_on_device(exec_req) # Print the standard output print(exec_resp.stdout)
# Create a send result request sr_req = SendResultRequest(TestResult(passed=True)) # Send the result client.send_result(sr_req)
if __name__ == "__main__": main()
-
为 IDT 配置设备信息
为 IDT 配置设备信息以运行测试。您必须使用以下信息更新位于 device.json
文件夹中的
模板。
<device-tester-extract-location>
/configs
[ { "id": "pool", "sku": "N/A", "devices": [ { "id": "
<device-id>
", "connectivity": { "protocol": "ssh", "ip": "<ip-address>
", "port": "<port>
", "auth": { "method": "pki | password", "credentials": { "user": "<user-name>
", "privKeyPath": "/path/to/private/key
", "password": "<password>
" } } } } ] } ]
在 devices
对象中,提供以下信息:
id
-
设备的用户定义的唯一标识符。
connectivity.ip
-
设备的 IP 地址。
connectivity.port
-
可选。用于与您的设备进行 SSH 连接的端口号。
connectivity.auth
-
连接的身份验证信息。
此属性仅在
connectivity.protocol
设置为ssh
. 时适用。connectivity.auth.method
-
用于通过给定的连接协议访问设备的身份验证方法。
支持的值为:
-
pki
-
password
-
connectivity.auth.credentials
-
用于身份验证的凭证。
connectivity.auth.credentials.user
-
用于登录您的设备的用户名。
connectivity.auth.credentials.privKeyPath
-
用于登录设备的私有密钥的完整路径。
此值仅在
connectivity.auth.method
设置为pki
. 时适用。 devices.connectivity.auth.credentials.password
-
用于登录设备的密码。
此值仅在
connectivity.auth.method
设置为password
. 时适用。
仅在 privKeyPath
设置为 method
时指定 pki
。
仅在 password
设置为 method
时指定 password
。
运行测试套件
在创建测试套件后,您需要确保它按预期运行。请完成以下步骤,以便使用现有设备池运行测试套件。
-
将
MyTestSuite_1.0.0
文件夹复制到
中。<device-tester-extract-location>
/tests -
运行以下命令:
cd
<device-tester-extract-location>
/bin ./devicetester_[linux | mac | win_x86-64]
run-suite --suite-id MyTestSuite
IDT 运行您的测试套件并将结果流式传输到控制台。当测试运行完成后,您会看到以下信息:
time="2020-10-19T09:24:47-07:00" level=info msg=Using pool: pool time="2020-10-19T09:24:47-07:00" level=info msg=Using test suite "MyTestSuite_1.0.0" for execution time="2020-10-19T09:24:47-07:00" level=info msg=b'hello world\n' suiteId=MyTestSuite groupId=myTestGroup testCaseId=myTestCase deviceId=my-device executionId=9a52f362-1227-11eb-86c9-8c8590419f30 time="2020-10-19T09:24:47-07:00" level=info msg=All tests finished. executionId=9a52f362-1227-11eb-86c9-8c8590419f30 time="2020-10-19T09:24:48-07:00" level=info msg= ========== Test Summary ========== Execution Time: 1s Tests Completed: 1 Tests Passed: 1 Tests Failed: 0 Tests Skipped: 0 ---------------------------------- Test Groups: myTestGroup: PASSED ---------------------------------- Path to AWS IoT Device Tester Report: /path/to/devicetester/results/9a52f362-1227-11eb-86c9-8c8590419f30/awsiotdevicetester_report.xml Path to Test Execution Logs: /path/to/devicetester/results/9a52f362-1227-11eb-86c9-8c8590419f30/logs Path to Aggregated JUnit Report: /path/to/devicetester/results/9a52f362-1227-11eb-86c9-8c8590419f30/MyTestSuite_Report.xml
Troubleshooting
使用以下信息可帮助解决在完成本教程时遇到的任何问题。
测试用例未成功运行
如果测试未成功运行,IDT 会将错误日志流式传输到控制台,以帮助您对测试运行进行故障排除。在检查错误日志之前,请验证以下内容:
无法连接到所测试设备
请验证以下内容:
-
您的
device.json
文件包含正确的 IP 地址、端口和身份验证信息。 -
您可以从主机通过 SSH 连接到设备。