<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # TestNG `@Parameters` – 測試參數示例 > 原文: [https://howtodoinjava.com/testng/testng-parameters/](https://howtodoinjava.com/testng/testng-parameters/) [TestNG](https://howtodoinjava.com/java-testng-tutorials/) 的重要功能之一是參數化。 此功能允許用戶**將參數作為參數**傳遞給測試。 通過使用 testng `@Parameters`注解支持此功能。 我們可以通過兩種方式為 testng 測試提供參數值。 1. 通過`testng.xml` XML 配置文件 2. 通過`DataProviders` > `@Parameters`注解可用于任何`@Before`,`@After`,`@Factory`和`@Test`注解方法。 它可以用于初始化變量并將其用于類,測試中,也可以用于整個測試執行。 ## 1\. TestNG `@Parameters` – 使用`testng.xml`測試參數 如果需要在運行時將一些簡單的值(例如`String`類型)傳遞給測試方法,則可以使用這種通過 testng XML 配置文件發送參數值的方法。 您必須使用`@Parameters`注解將參數值傳遞給測試方法。 ```java @Parameters({ "param-name" }) ``` 讓我們編寫一個簡單的示例,通過 XML 配置文件將參數傳遞給測試方法。 #### 1.1 測試 在下面的測試中,我們創建了一個包含多個方法的測試類,這些方法接受來自 testng 的參數。 在 testng XML 文件中的套件級別和測試級別都設置了參數值。 如果在套件級別定義,則在測試級別定義的任何參數值都將覆蓋具有相同名稱的參數值。 您可以在測試方法`prameterTestThree()`的測試三中看到這一點。 ```java package com.howtodoinjava.test; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterTest { /** * Following method takes one parameter as input. Value of the * said parameter is defined at suite level. */ @Parameters({ "suite-param" }) @Test public void prameterTestOne(String param) { System.out.println("Test one suite param is: " + param); } /** * Following method takes one parameter as input. Value of the * said parameter is defined at test level. */ @Parameters({ "test-two-param" }) @Test public void prameterTestTwo(String param) { System.out.println("Test two param is: " + param); } /** * Following method takes two parameters as input. Value of the * test parameter is defined at test level. The suite level * parameter is overridden at the test level. */ @Parameters({ "suite-param", "test-three-param" }) @Test public void prameterTestThree(String param, String paramTwo) { System.out.println("Test three suite param is: " + param); System.out.println("Test three param is: " + paramTwo); } } ``` #### 1.2 `testng.xml` 現在將`testng.xml`文件添加到項目根目錄,并將以下代碼放入其中。 在這里,我們定義要傳遞的參數值。 ```java <suite name="Parameter test Suite" verbose="1"> <!-- This parameter will be passed to every test in this suite --> <parameter name="suite-param" value="suite level parameter" /> <test name="Parameter Test one"> <classes> <class name="com.howtodoinjava.test.ParameterTest"> <methods> <include name="prameterTestOne" /> </methods> </class> </classes> </test> <test name="Parameter Test two"> <!-- This parameter will be passed this test only --> <parameter name="test-two-param" value="Test two parameter" /> <classes> <class name="com.howtodoinjava.test.ParameterTest"> <methods> <include name="prameterTestTwo" /> </methods> </class> </classes> </test> <test name="Parameter Test three"> <!-- Overriding suite level parameter --> <parameter name="suite-param" value="overiding suite parameter" /> <!-- Test specific parameter --> <parameter name="test-three-param" value="test three parameter" /> <classes> <class name="com.howtodoinjava.test.ParameterTest"> <methods> <include name="prameterTestThree" /> </methods> </class> </classes> </test> </suite> ``` #### 1.3 演示 現在使用`testng.xml`運行以上測試。 以上測試運行的輸出如下: ```java [TestNG] Running: C:\somepath\testng.xml Test one suite param is: suite level parameter Test two param is: Test two parameter Test three suite param is: overiding suite parameter Test three param is: test three parameter =============================================== Parameter test Suite Total tests run: 3, Failures: 0, Skips: 0 =============================================== ``` 從測試結果中可以看出,只有`timeTestTwo()`被執行,因為它的執行時間少于`testng.xml`文件中定義的超時時間。 `timeTestOne()`執行被取消,因為完成所需的時間超過配置的超時時間。 ## 2\. TestNG `@Parameters` – 可選參數 TestNG 還提供了提供**可選參數**的選項,如果在定義的文件中找不到參數值,則將使用此值。 #### 2.1 使用`@Optional`注解進行測試 要傳遞可選參數,請使用`@Optional`注解。 ```java package com.howtodoinjava.test; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterTest { @Parameters({ "optional-value" }) @Test public void optionTest(@Optional("optional value") String value) { System.out.println("This is: " + value); } } ``` 前面的類文件包含一個將一個參數作為輸入的測試方法。 所述執行中的測試方法打印使用`System.out.println`方法傳遞到控制臺的參數值。 使用 XML 文件中名為`optional-value`的參數將參數值傳遞到測試方法。 使用針對所述參數的`@Optional`注解定義所述參數的可選值。 #### 2.2 使用`@Optional`注解進行測試 在此`testng.xml`文件中,上面定義了兩個測試。 在第一個測試中未定義任何參數,第二個測試在其中聲明了一個名為“`optional-value`”的參數。 ```java <suite name="Optional test Suite" verbose="1"> <test name="Optional Test one"> <classes> <class name="test.parameter.OptionalTest" /> </classes> </test> <test name="Optional Test two"> <parameter name="optional-value" value="passed from xml" /> <classes> <class name="test.parameter.OptionalTest" /> </classes> </test> </suite> ``` #### 2.3 演示 將以上代碼作為測試套件運行的輸出為: ```java [TestNG] Running: C:\somepath\testng.xml This is: optional value This is: passed from xml =============================================== Optional test Suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== ``` 從先前的測試結果可以看出,TestNG 在第一次執行測試時就將可選值傳遞給了測試方法。 發生這種情況是因為 TestNG 在第一次測試中無法在 XML 文件中找到名為`optional-value`的參數。 在第二次測試期間,它在 XML 中找到了參數值,并將該值傳遞給執行過程中的測試方法。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看