asset compute項目定義了一種模式,用於輕鬆建立和執行Asset compute工作者🔗的測試。
asset compute背景工作的測試會分為測試套裝,而在每個測試套裝中,有一或多個測試案例主張要測試的條件。
asset compute專案中的測試結構如下:
/actions/<worker-name>/index.js
...
/test/
asset-compute/
<worker-name>/ <--- Test suite for the worker, must match the yaml key for this worker in manifest.yml
<test-case-1>/ <--- Specific test case
file.jpg <--- Input file (ie. `source.path` or `source.url`)
params.json <--- Parameters (ie. `rendition.instructions`)
rendition.png <--- Expected output file (ie. `rendition.path`)
<test-case-2>/ <--- Another specific test case for this worker
...
每個測試轉播都可以有下列檔案:
file.<extension>
.link
以外的任何值)rendition.<extension>
params.json
validate
diff
命令mock-<host-name>.json
此測試實例聲明輸入檔案(file.jpg
)的參數化輸入(params.json
)生成預期的PNG格式副本(rendition.png
)。
首先,請刪除/test/asset-compute/simple-worker
處自動生成的simple-worker
測試案例,因為這無效,因為我們的工作人員不再只是將源複製到格式副本。
在/test/asset-compute/worker/success-parameterized
處建立新的測試案例資料夾,以測試是否成功執行產生PNG轉譯的工作器。
在success-parameterized
資料夾中,添加此測試案例的測試輸入檔案,並將其命名為file.jpg
。
在success-parameterized
資料夾中,添加一個名為params.json
的新檔案,該檔案定義工作器的輸入參數:
{
"size": "400",
"contrast": "0.25",
"brightness": "-0.50"
}
這些是傳入開發工具的Asset compute設定檔定義的相同索引鍵/值,減去worker
索引鍵。
將預期的轉譯檔案新增至此測試案例,並將其命名為rendition.png
。 此檔案表示給定輸入file.jpg
的工作器的預期輸出。
從命令行中,通過執行aio app test
來運行項目根
此測試案例旨在確保當contrast
參數設定為無效值時,工作者擲回適當錯誤。
在/test/asset-compute/worker/error-contrast
處建立新的測試用例資料夾,以測試由於無效contrast
參數值導致的工作器的重新執行。
在error-contrast
資料夾中,添加此測試案例的測試輸入檔案,並將其命名為file.jpg
。 此檔案的內容對此測試不重要,它只需要存在才能通過「損壞源」檢查,才能達到rendition.instructions
有效性檢查,該測試案例才能驗證。
在error-contrast
資料夾中,添加一個名為params.json
的新檔案,該檔案定義了包含內容的工作器的輸入參數:
{
"contrast": "10",
"errorReason": "rendition_instructions_error"
}
contrast
參數設定為10
,這是無效值,因為對比度必須介於–1和1之間,才會引發RenditionInstructionsError
。errorReason
鍵設定為與預期錯誤相關聯的「reason」,以在測試中擲回適當錯誤。 此無效的對比度參數會擲回自訂錯誤、RenditionInstructionsError
,因此將errorReason
設為此錯誤的原因,或將rendition_instructions_error
設為斷言擲回。由於執行錯誤期間不應產生任何轉譯,因此不需要rendition.<extension>
檔案。
通過執行命令aio app test
從項目的根目錄運行測試套件
Github提供最終測試案例,網址為: