【问题标题】:How to get @test class context in data provider如何在数据提供者中获取@test 类上下文
【发布时间】:2014-12-08 14:33:04
【问题描述】:

我发现您可以使用 ITestContext 或 java.lang.reflect.Method 声明数据提供者。作为参数,但我想知道调用 dataprovider 方法的测试类的类名。 ITestContext 和 java.lang.reflect.Method 都不能提供。

【问题讨论】:

  • 你基本上想要@Test方法的类吧?
  • 在数据提供者中是。这不是测试类的一部分。
  • 我的答案已被删除——因为我不知道是什么原因!在 dataprovider 中使用 method.getDeclaringClass()。它会给你测试方法的类。
  • 是的,那就是 java.lang.reflect.Method 。我试过你说的。作为回报,它给了那个班级。不确定我是否在某个地方搞砸了。
  • 将其标记为答案。不知道为什么有人会删除它。你能接受它只是为了确保这次没有人删除它:) 真的对删除感到恼火

标签: testng


【解决方案1】:

method.getDeclaringClass() 应该在 dataprovider 方法中为您提供方法的类名。

【讨论】:

    【解决方案2】:

    这是我的问题的answer。如果有人有更好的答案,请告诉我。

    解决方案是数据提供者不能接受任意输入,但他们可以接受对当前测试方法的引用。所以,我们可以创建一个新的注解,DataProviderArguments,我可以将它附加到一个测试方法,然后在参数中传递类名。

    创建注释

        import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    
        /**
        * Annotation for feeding arguments to methods conforming to the
        * "@DataProvider" annotation type.
        * @author jharen
        */
        @Retention(RetentionPolicy.RUNTIME)
        public @interface DataProviderArguments
        {
          /**
          * String array of key-value pairs fed to a dynamic data provider.
          * Should be in the form of key=value, e.g., <br />
          * args={"foo=bar", "biz=baz"}
          */
        String[] value();
        }
    

    创建一个辅助类,它将读取参数并将它们传递给。

    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
    
    public class DataProviderUtils
    {
        protected static Map<String, String> resolveDataProviderArguments(Method testMethod) throws Exception
        {
            if (testMethod == null)
                throw new IllegalArgumentException("Test Method context cannot be null.");
    
            DataProviderArguments args = testMethod.getAnnotation(DataProviderArguments.class);
            if (args == null)
                throw new IllegalArgumentException("Test Method context has no DataProviderArguments annotation.");
            if (args.value() == null || args.value().length == 0)
                throw new IllegalArgumentException("Test Method context has a malformed DataProviderArguments annotation.");
            Map<String, String> arguments = new HashMap<String, String>();
            for (int i = 0; i < args.value().length; i++)
            {
                String[] parts = args.value()[i].split("=");
                arguments.put(parts[0], parts[1]);
            }
            return arguments;
        }
    

    这样,FileDataProvider 可以读取任何给定的文件:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.io.IOUtils;
    import org.testng.annotations.DataProvider;
    
    public class FileDataProvider
    {
        @DataProvider(name="getDataFromFile")
        public static Iterator<Object[]> getDataFromFile(Method testMethod) throws Exception
        {
            Map<String, String> arguments = DataProviderUtils.resolveDataProviderArguments(testMethod);
            List<String> lines = FileDataProvider.getRawLinesFromFile(arguments.get("filePath"));
            List<Object[]> data = new ArrayList<Object[]>();
            for (String line : lines)
            {
                data.add(new Object[]{line});
            }
            return data.iterator();
        }
    
        public static List<String> getRawLinesFromFile(Method testMethod) throws Exception
        {
            Map<String, String> arguments = DataProviderUtils.resolveDataProviderArguments(testMethod);
            return FileDataProvider.getRawLinesFromFile(arguments.get("filePath"));
        }
    
        @SuppressWarnings("unchecked")
        public static List<String> getRawLinesFromFile(String filePath) throws Exception
        {
            InputStream is = new FileInputStream(new File(filePath));
            List<String> lines = IOUtils.readLines(is, "UTF-8");
            is.close();
            return lines;
        }
    }
    

    终于写出这样的测试;

    @Test(dataProviderClass=com.netflix.systemtests.api.commons.dataproviders.TestAccountDataProvider.class, dataProvider="getTestAccountsFromFile")
    @DataProviderArguments("filePath=src/main/resources/input-files/testAccounts.txt")
    public void fetchInstantQueueAsJSON(String email, String accountID) throws Exception
    {
      // blah blah blah testy test goes here
    }
    

    【讨论】:

      【解决方案3】:

      您需要将 ITestNGMethod 对象作为参数传递给您的 @DataProvider 方法。通过它你应该能够提取出你需要的任何东西。

      这是一个示例

        public static Object[][] dataProvider(ITestNGMethod method) {
          Class cla = method.getRealClass();
          Method tm = method.getConstructorOrMethod().getMethod();
          Constructor constructor = method.getConstructorOrMethod().getConstructor();
          String name = cla.getName() + ".";
          if (tm == null) {
            name = constructor.getName();
          } else {
            name = name + tm.getName();
          }
      
          System.out.println("Data provider is being called  by " + name);  
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多