2017-09-03 67 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

相关问题