با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
آزمایشهای واحد برای الگوهای سفارشی Google Tag Manager به شما کمک میکند تا عملکرد الگوهای خود را تأیید کنید. شما می توانید مجموعه ای از تست ها را برای هر قالب ایجاد کنید که می تواند بدون نیاز به استقرار برچسب شما اجرا شود، که به شما امکان می دهد به طور مداوم رفتار قالب خود را در طول توسعه آزمایش کنید. هر آزمون می تواند مقادیر ورودی نمونه، فراخوانی تابع ساختگی و رفتار کد را ارائه دهد.
محدودیت ها
تستهای واحد قوانین اعتبارسنجی را بررسی نمیکنند، اما میتوانید با استفاده از دکمه Run Code اعتبارسنجی را به صورت دستی بررسی کنید.
بررسی مجوزها در APIهای مسخره شده در تست های واحد انجام نمی شود.
این راهنما شما را با نحوه نوشتن تست های واحد برای الگوی سفارشی خود آشنا می کند. این مثال یک الگوی متغیر ایجاد می کند که یک رشته ورودی را می گیرد و نسخه بزرگ آن رشته را برمی گرداند.
یک قالب متغیر جدید ایجاد کنید. روی Templates در ناوبری سمت چپ کلیک کنید و در قسمت Variable Templates روی New کلیک کنید.
روی فیلدها کلیک کنید.
روی افزودن فیلد کلیک کنید و ورودی متن را انتخاب کنید. نام فیلد را text1 بگذارید و نام نمایشی را روی "Text 1" تنظیم کنید.
در تب Code ، کد پیشفرض را با این جاوا اسکریپت sandbox شده جایگزین کنید:
let input = data.text1;
return input.toUpperCase();
روی Tests کلیک کنید تا تب تست باز شود.
روی افزودن تست کلیک کنید و نام آزمون را از «تست بدون عنوان 1» به «رشتههای دستهدار» تغییر دهید.
روی نماد گسترش ( expand_more ) کلیک کنید تا ویرایشگر جاوا اسکریپت جعبه شنی تست نمایان شود. کد را با این جاوا اسکریپت sandbox شده جایگزین کنید:
//CallrunCodetorunthetemplate's code with a lowercase stringletvariableResult=runCode({text1:'this is a test'});//ValidatethattheresultofrunCodeisanuppercasestring.assertThat(variableResult).isEqualTo('THIS IS A TEST');
این تست رشته 'this is a test' را به متغیر منتقل می کند و تأیید می کند که متغیر مقدار مورد انتظار 'THIS IS A TEST' را برمی گرداند. runCode API برای اجرای کد الگو در تب Code استفاده می شود. آرگومان runCode یک شی است که به عنوان داده سراسری استفاده می شود. assertThat API یک شی را برمیگرداند که میتوان از آن برای اظهارنظرهای روان در مورد مقدار موضوع استفاده کرد.
برای اجرای تست روی Run Tests کلیک کنید. خروجی تست در کنسول ظاهر می شود.
دکمه ▶ Run Tests تمام تست های فعال شده در الگو را به ترتیب نشان داده شده اجرا می کند. برای تغییر ترتیب، از نماد کشیدن (⠿) استفاده کنید. با کلیک بر روی دایره سمت چپ نام آزمون می توان به طور موقت یک آزمایش را فعال یا غیرفعال کرد. برای اجرای یک تست، روی دکمه ▶ که هنگام حرکت ماوس روی تست ظاهر می شود، کلیک کنید.
کنسول باید تعداد کل تستهای اجرا شده و تعداد تستهایی که شکست خوردهاند را چاپ کند. در این حالت فقط یک تست اجرا شد و باید قبول شود.
برای افزودن تست دوم دوباره روی Add Test کلیک کنید. نام آزمون را از "تست بدون عنوان 2" به "دسته ها تعریف نشده" تغییر دهید.
روی تست کلیک کنید تا آن را گسترش دهید و ویرایشگر جاوا اسکریپت جعبه شنی را نشان دهید. جاوا اسکریپت sandboxed را در ویرایشگر وارد کنید:
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eGoogle Tag Manager uses unit tests to validate custom template functionality before deployment.\u003c/p\u003e\n"],["\u003cp\u003eUnit tests for custom templates utilize sample inputs, mock functions, and assertions to verify code behavior.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003erunCode\u003c/code\u003e API executes the template's code within the test environment, while \u003ccode\u003eassertThat\u003c/code\u003e verifies the output against expected values.\u003c/p\u003e\n"],["\u003cp\u003eThese tests help identify and address potential issues, such as handling undefined or unexpected inputs, before deploying the template.\u003c/p\u003e\n"],["\u003cp\u003eBy creating and running unit tests for your custom templates, you can improve the reliability and stability of your Google Tag Manager implementations.\u003c/p\u003e\n"]]],["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"],null,["# Tests\n\nUnit tests for Google Tag Manager custom templates help you validate the functionality of your templates. You can create a set of tests for each template\nthat can be run without needing to deploy your tag, which allows you to\ncontinuously test your template's behavior during development. Each test can\nprovide sample input values, mock function calls, and assert code behavior.\n\nLimitations\n-----------\n\n- Unit tests do not check validation rules but you can manually check validation using the **Run Code** button.\n- Permission checks do not happen on mocked APIs in unit tests.\n\nThis guide will walk you through how to write unit tests for your custom template. This example creates a variable template that takes an input string and returns\nthe uppercase version of that string.\n\n1. Create a new variable template. Click **Templates** in the left navigation and click **New** under the **Variable Templates** section.\n\n2. Click **Fields**.\n\n3. Click **Add Field** and select **Text input** . Name the field `text1` and set the display name to *\"Text 1\"*.\n\n4. In the **Code** tab, replace the default code with this sandboxed JavaScript:\n\n let input = data.text1;\n return input.toUpperCase();\n\n5. Click **Tests** to open the testing tab.\n\n6. Click **Add Test** and change the test's name from *\"Untitled test 1\"* to *\"Handles strings\"*.\n\n7. Click on the expand icon (*expand_more*) to reveal the test's sandboxed JavaScript editor. Replace the code with this sandboxed JavaScript:\n\n // Call runCode to run the template's code with a lowercase string\n let variableResult = runCode({text1: 'this is a test'});\n // Validate that the result of runCode is an uppercase string.\n assertThat(variableResult).isEqualTo('THIS IS A TEST');\n\n This test passes the string *`'this is a test'`* to the variable and verifies that the variable returns the expected value of *`'THIS IS A TEST'`* . The [`runCode`](/tag-platform/tag-manager/templates/api#runcode) API is used to run the template code in the **Code** tab. The argument to `runCode` is an object that is used as the data global. The [`assertThat`](/tag-platform/tag-manager/templates/api#assertthat) API returns an object that can be used to fluently make assertions about a subject's value.\n8. Click **▶ Run Tests** to run the test. The output of the test will appear in the Console.\n\n The **▶ Run Tests** button runs all of the enabled tests in the template, in the order shown. To change the order, use the drag icon (⠿). A test can be temporarily enabled or disabled by clicking on the circle to the left of the test name. To run a single test, click the ▶ button that appears when you move the mouse over the test.\n\n The console should print the total number of tests run and the number of tests that failed, if any. In this case, only one test was run and it should pass.\n9. Click **Add Test** again to add a second test. Change the test's name from *\"Untitled test 2\"* to *\"Handles undefined\"*.\n\n10. Click on the test to expand it and reveal the sandboxed JavaScript editor. Enter sandboxed JavaScript in the editor:\n\n let variableResult = runCode({});\n assertThat(variableResult).isEqualTo(undefined);\n\n | **Note:** [`runCode`](/tag-platform/tag-manager/templates/api#runcode) is called with an empty object, which means the value of the `text1` field will be `undefined` in this test case.\n11. Click **▶ Run Tests** to run all of the tests at once. The output of the test will appear in the console.\n\n The *Handles undefined* test should fail. Congratulations, you found a bug!\n12. Click **Code** to go back and edit the template's sandboxed JavaScript code. Update the sandboxed JavaScript as follows:\n\n const getType = require('getType');\n\n let input = data.text1;\n if (getType(input) !== 'string') {\n return input;\n }\n return input.toUpperCase();\n\n The updated code follows the best practice of validating the `input` variable before using it.\n13. Click **Tests** to go back to the list of test cases.\n\n14. Click **▶ Run Tests** to run all of the test cases again. This time the *Handles undefined* test should pass.\n\n15. Click **Save**, and close the Template Editor."]]