本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
教學課程:開發簡單的 IDT 測試套件
測試套件結合了下列項目:
-
包含測試邏輯的測試可執行檔
-
描述測試套件的組態檔案
本教學課程說明如何使用 IDT for AWS IoT Greengrass 來開發包含單一測試案例的 Python 測試套件。在本教學課程中,您將完成下列步驟:
先決條件
為了完成本教學,您需要以下項目:
-
主機電腦需求
-
的最新版本 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 作業系統。請確定您在 Raspberry Pi 上設定 SSH 以遠端連線至它。
-
建立測試套件目錄
IDT 會以邏輯方式將測試案例分成每個測試套件內的測試群組。每個測試案例都必須位於測試群組內。在此教學課程中,建立名為 的資料夾,MyTestSuite_1.0.0
並在此資料夾中建立下列目錄樹狀目錄:
MyTestSuite_1.0.0 └── suite └── myTestGroup └── myTestCase
建立組態檔案
您的測試套件必須包含下列必要的組態檔案:
必要的組態檔案
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 用戶端 SDK
您可以使用 IDT 用戶端 SDK 讓 IDT 與受測裝置互動,並報告測試結果。在本教學課程中,您將使用 開發套件的 Python 版本。
從
資料夾,將<device-tester-extract-location>
/sdks/python/idt_client
資料夾複製到您的MyTestSuite_1.0.0/suite/myTestGroup/myTestCase
資料夾。
若要驗證 SDK 已成功複製,請執行下列命令。
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()
-
使用用戶端 SDK 函數將下列測試邏輯新增至您的
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-tester-extract-location>
/configsdevice.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
執行測試套件
建立測試套件之後,您要確保其如預期般運作。請完成下列步驟,以使用現有的裝置集區執行測試套件。
-
將您的
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 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
故障診斷
使用以下資訊,以協助解決完成教學課程的任何問題。
測試案例未成功執行
如果測試未成功執行,IDT 會將錯誤日誌串流至主控台,協助您疑難排解測試執行。檢查錯誤日誌之前,請確認下列事項:
無法連線至正在測試的裝置
請確認下列內容:
-
您的
device.json
檔案包含正確的 IP 地址、連接埠和身分驗證資訊。 -
您可以從主機電腦透過 SSH 連線至裝置。