這項測試會將 'this is a test' 字串傳遞至變數,並驗證該變數是否傳回 'THIS IS A TEST' 的預期值。runCode API 可用來執行「Code」分頁中的範本程式碼。runCode 的引數是一個物件,做為全域資料使用。assertThat API 會傳回一個物件,可用來流暢地對主體值做出斷言。
按一下「▶ 執行測試」執行測試。測試的輸出結果會顯示在控制台中。
點選「▶ Run Tests」按鈕會依照顯示順序,執行範本中所有已啟用的測試。如要變更順序,請使用拖曳圖示 (↘)。您可以按一下測試名稱左側的圓圈,暫時啟用或停用測試。如要執行單一測試,請將滑鼠遊標懸停在測試上,然後按一下隨即顯示的 ▶ 按鈕。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2024-09-10 (世界標準時間)。"],[[["Google Tag Manager uses unit tests to validate custom template functionality before deployment."],["Unit tests for custom templates utilize sample inputs, mock functions, and assertions to verify code behavior."],["The `runCode` API executes the template's code within the test environment, while `assertThat` verifies the output against expected values."],["These tests help identify and address potential issues, such as handling undefined or unexpected inputs, before deploying the template."],["By creating and running unit tests for your custom templates, you can improve the reliability and stability of your Google Tag Manager implementations."]]],["Unit tests for Google Tag Manager templates validate functionality by running tests without deployment. You create tests within the template editor under the \"Tests\" tab. Tests use `runCode` to simulate template execution with sample data and `assertThat` to verify results. The process involves creating a template, adding input fields, writing code, and creating tests to handle various inputs. Tests can be run individually or collectively. Limitations are that Unit tests do not check validation rules and permission checks do not happen.\n"]]