

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 教程：构建和运行示例 IDT 测试套件
<a name="build-sample-suite"></a>

 AWS IoT Device Tester 下载内容包括示例测试套件的源代码。您可以完成本教程来构建和运行示例测试套件，以了解如何使用 IDT AWS IoT Greengrass 来运行自定义测试套件。

 在本教程中，您将完成以下步骤：

1. [构建示例测试套件](#build-sample)

1. [使用 IDT 运行示例测试套件](#run-sample)

## 先决条件
<a name="prereqs-tutorial-sample"></a><a name="prereqs-list"></a>

要完成本教程，您需要：
+ 

**主机要求**
  + 最新版本的 AWS IoT Device Tester
  + [Python](https://www.python.org/downloads/) 3.7 或更高版本

    要检查您计算机安装的 Python 版本，请运行以下命令：

    ```
    python3 --version
    ```

    在 Windows 上，如果运行此命令时返回错误，则可改用 `python --version`。如果返回的版本号为 3.7 或更高版本，则可通过在 Powershell 终端中运行以下命令将 `python3` 设置为 `python` 命令的别名。

    ```
    Set-Alias -Name "python3" -Value "python"
    ```

    如果没有返回版本信息，或者版本号小于 3.7，则按照[下载 Python](https://wiki.python.org/moin/BeginnersGuide/Download) 中的说明安装 Python 3.7\$1。有关更多信息，请参阅 [Python 文档](https://docs.python.org)。
  + [urllib3](https://urllib3.readthedocs.io/en/latest/)

    要验证 `urllib3` 是否已正确安装，请运行以下命令：

    ```
    python3 -c 'import urllib3'
    ```

    如果未安装 `urllib3`，请运行以下命令进行安装：

    ```
    python3 -m pip install urllib3
    ```
+ 

**设备要求**
  + 一种运行 Linux 操作系统的设备，其网络连接到与您主机相同的网络。

    我们建议您使用搭载 Raspberry Pi 操作系统的 [Raspberry Pi](https://www.raspberrypi.org/)。请确保您设置 Raspberry Pi 上的 [SSH](https://www.raspberrypi.org/documentation/remote-access/ssh/) 才能远程连接到它。

## 配置 IDT 的设备信息
<a name="configure-idt-sample"></a>

配置您的设备信息，以便 IDT 运行测试。您必须使用以下信息，更新位于 `<device-tester-extract-location>/configs` 文件夹中的 `device.json` 模板。

```
[
  {
    "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` 时适用。

**注意**  
只有当 `method` 设置为 `pki` 时才指定 `privKeyPath`。  
只有当 `method` 设置为 `password` 时才指定 `password`。

## 构建示例测试套件
<a name="build-sample"></a>

`<device-tester-extract-location>/samples/python` 文件夹包含示例配置文件、源代码和 IDT 客户端软件开发工具包，您可以使用提供的构建脚本将其组合成一个测试套件。以下目录树显示了这些示例文件的位置：

```
<device-tester-extract-location>
├── ...
├── tests
├── samples
│   ├── ...
│   └── python
│       ├── configuration
│       ├── src
│       └── build-scripts
│           ├── build.sh
│           └── build.ps1
└── sdks
    ├── ...
    └── python
        └── idt_client
```

要构建测试套件，请在主机上运行以下命令：

------
#### [ Windows ]

```
cd <device-tester-extract-location>/samples/python/build-scripts
./build.ps1
```

------
#### [ Linux, macOS, or UNIX ]

```
cd <device-tester-extract-location>/samples/python/build-scripts
./build.sh
```

------

这将在该 `<device-tester-extract-location>/tests` 文件夹下的 `IDTSampleSuitePython_1.0.0` 文件夹中创建示例测试套件。检查 `IDTSampleSuitePython_1.0.0` 文件夹中的文件，以了解示例测试套件的结构，并查看测试用例可执行文件和测试配置 JSON 文件的各种示例。

**注意**  
示例测试套件包含 python 源代码。请勿在测试套件代码中包含敏感信息。

下一步：使用 IDT [运行您创建的示例测试套件](#run-sample)。

## 使用 IDT 运行示例测试套件
<a name="run-sample"></a>

要运行示例测试套件，请在主机上运行以下命令：

```
cd <device-tester-extract-location>/bin
./devicetester_[linux | mac | win_x86-64] run-suite --suite-id IDTSampleSuitePython
```

IDT 会运行示例测试套件，并将结果流式传输到控制台。测试运行完毕后，您会看到以下信息：

```
========== Test Summary ==========
Execution Time:         5s
Tests Completed:        4
Tests Passed:           4
Tests Failed:           0
Tests Skipped:          0
----------------------------------
Test Groups:
    sample_group:       PASSED
----------------------------------
Path to IoT Device Tester Report: /path/to/devicetester/results/87e673c6-1226-11eb-9269-8c8590419f30/awsiotdevicetester_report.xml
Path to Test Execution Logs: /path/to/devicetester/results/87e673c6-1226-11eb-9269-8c8590419f30/logs
Path to Aggregated JUnit Report: /path/to/devicetester/results/87e673c6-1226-11eb-9269-8c8590419f30/IDTSampleSuitePython_Report.xml
```

## 问题排查
<a name="tutorial-troubleshooting-custom"></a>

使用以下信息，以帮助解决在完成本教程时遇到的任何问题。

**测试用例未成功运行**  
如果测试运行失败，IDT 会将错误日志流式传输到控制台，以帮助您对测试运行进行故障排除。请确保满足本教程的所有[先决条件](#prereqs-tutorial-sample)。

**无法连接到被测设备**

请验证以下内容：
+ 您的 `device.json` 文件包含正确的 IP 地址、端口和身份验证信息。
+ 您可以通过 SSH 从主机连接到您的设备。