2017-09-03 71 views
0

我想將參數傳遞給testng類。 我有一個變量tc1.featureFileName,tc1.fieldValueMap如何將對象/參數傳遞給Testng

我已經解析名稱C2中的TC對象,但我不知道如何通過 tc1.fieldValueMap其中包含的輸入變量的HashMap來TestNG的。

 Class c2 = Class.forName("com.Scenarios."+tc1.featureFileName); 

     TestListenerAdapter tla = new TestListenerAdapter(); 
     TestNG testng = new TestNG(); 
     testng.setTestClasses(new Class[] {c2}); 
     testng.addListener(tla); 
     testng.run(); 
+0

雖然這個問題不是很清楚,但http://testng.org/doc/documentation-main.html#parameters可以幫助 – nullpointer

回答

1

下面是一個示例,演示如何在使用TestNG API時將參數作爲鍵/值對注入。

import org.testng.Assert; 
import org.testng.Reporter; 
import org.testng.TestNG; 
import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 
import org.testng.xml.XmlClass; 
import org.testng.xml.XmlSuite; 
import org.testng.xml.XmlTest; 

import java.util.Collections; 
import java.util.HashMap; 
import java.util.Map; 

public class TestRunner { 
    public static void main(String[] args) { 

     XmlSuite xmlSuite = new XmlSuite(); 
     xmlSuite.setName("Sample_Suite"); 
     Map<String, String> fieldValues = new ParamContainer().getValues(); 
     xmlSuite.setParameters(fieldValues); 
     XmlTest xmlTest = new XmlTest(xmlSuite); 
     xmlTest.setName("Sample_test"); 
     xmlTest.setXmlClasses(Collections.singletonList(new XmlClass(HelloWorld.class))); 
     TestNG tng = new TestNG(); 
     tng.setXmlSuites(Collections.singletonList(xmlSuite)); 
     tng.run(); 
    } 

    /** 
    * This is a test class. 
    */ 
    public static class HelloWorld { 
     @Test 
     @Parameters("name") 
     public void hi(String name) { 
      Assert.assertNotNull(name); 
      Reporter.log("Name is:" + name, true); 
     } 
    } 

    /** 
    * Simulates a class that will contain all the key/value pairs that are to be used as 
    * <code><parameters></code> for the suite. 
    */ 
    public static class ParamContainer { 
     private Map<String, String> values = new HashMap<>(); 

     ParamContainer() { 
      values.put("name", "Jack-Daniel"); 
     } 

     Map<String, String> getValues() { 
      return values; 
     } 
    } 

} 
+0

請問這與上面的問題?我沒有看到OP使用的XML套件。 – nullpointer

+0

問題是關於如何在使用TestNG API時將一堆鍵值對輸入到測試執行中。示例顯示瞭如何做到這一點。 –

+0

這個問題也涉及到另一種做法,樣本只是以其他方式轉儲。在這種情況下,最好是對項目鏈接的評論。可以有N種解決問題的方式,但是如果您將X標記爲與A有關的問題的答案,那將無濟於事。 – nullpointer

相關問題