简体   繁体   English

将数据文件转换为JUnit的Java静态类常量

[英]Convert data file to Java static class constants for JUnit

I had written few JUnit classes for my app, but I would like to now separate my test data (which is hardcoded now) from the code to a data-only text file/properties file/xml/whatever. 我为我的应用程序编写了几个JUnit类,但是现在我想将测试数据(现在已硬编码)从代码中分离到仅数据文本文件/属性文件/ xml /任何文件中。

So I can easily give various test datas without modifying my JUnit. 因此,我无需修改JUnit就可以轻松给出各种测试数据。

I was thinking about putting them in a text file, and use a parser to parse it at the very beginning of my JUnit suite, and convert all of the data to a Java static class constants, so I can easily refer it anywhere in my JUnit. 我正在考虑将它们放在文本文件中,并在JUnit套件的一开始就使用解析器对其进行解析,然后将所有数据转换为Java静态类常量,因此我可以轻松地在JUnit中的任何位置引用它。

public final class TestDataConstants {

public static final String username = "xbeta";
public static final String password = "test123!";
public static final String authToken = "f17bf9c8-9d38-47af-a053-210130cac6f7";
...
}

Now I know I can easily write a parser for this, but I'm asking 2 questions for people who had done this before in the past with experience 现在我知道我可以轻松地为此编写一个解析器,但是我想问两个过去曾经有经验的人

  1. What format is the best and should be using for storing these test data? 哪种格式是最好的,并且应该用于存储这些测试数据的格式?
  2. What are some ways to convert these test data files to Java static class? 有哪些方法可以将这些测试数据文件转换为Java静态类? Like generating Java code on-the-fly using Java. 就像使用Java即时生成Java代码一样。

Thanks in advanced. 提前致谢。

One method is to use a .properties file and then load that in as a resource at the start of your test. 一种方法是使用.properties文件,然后在测试开始时将其作为资源加载。

For example, 例如,

test.properties: test.properties:

test.username=xbeta
test.password=test123!
test.authToken=f17bf9c8-9d38-47af-a053-210130cac6f7

Then in your tests you can access the data using something like the following, assuming the test.properties file is on your classpath: 然后,在测试中,可以使用以下内容访问数据,并假设test.properties文件位于类路径中:

// note, the .properties is removed in the call to .getBundle
ResourceBundle testProperties = ResourceBundle.getBundle("test"); 
String username = testProperties.getString("test.username");
String password = testProperties.getString("test.password");

Here is how you can do this with a @DataProvider , assuming that you put your data in a properties file: 假设您将数据放在属性文件中,可以使用@DataProvider进行以下操作:

public class A {

  @Test(dataProvider = "dp")
  public void test(String k, String v) {
    System.out.println("Testing " + k + " " + v);
    Assert.assertEquals(k.toUpperCase(), v);
  }

  @DataProvider
  public Object[][] dp() throws FileNotFoundException, IOException {
    Properties p = new Properties();
    p.load(new FileInputStream(new File("/tmp/a.properties")));
    List<Object[]> result = Lists.newArrayList();
    for (Map.Entry<Object, Object> es : p.entrySet()) {
      result.add(new Object[] { es.getKey(), es.getValue() });
    }
    return result.toArray(new Object[result.size()][]);
  }

  @Test(dataProvider = "dp")
  public void test(String k, String v) {
    System.out.println("Testing " + k + " " + v);
    Assert.assertEquals(k.toUpperCase(), v);
  }
}

The properties file: 属性文件:

abc: ABC
def: DEF
ghi: GHI

And the output: 并输出:

Testing abc ABC
Testing def DEF
Testing ghi GHI
PASSED: test("abc", "ABC")
PASSED: test("def", "DEF")
PASSED: test("ghi", "GHI")

===============================================
    Test1
    Tests run: 3, Failures: 0, Skips: 0
===============================================

Note that each set of parameter is passed to the test method (so the test method was invoked three times) and that this test method declared these as regular method parameters. 请注意,每个参数集都传递给测试方法(因此该测试方法被调用了3次),并且该测试方法将这些声明为常规方法参数。

More details on data providers: http://testng.org/doc/documentation-main.html#parameters-dataproviders 有关数据提供者的更多详细信息: http : //testng.org/doc/documentation-main.html#parameters-dataproviders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM