简体   繁体   English

运行具有多个属性的同一junit套件

[英]Running same junit suite with multiple properties

I have a junit test suite, that i need to run with several different properties, so that i can have only 1 test suite, but run it several times but with different data on it. 我有一个junit测试套件,我需要运行几个不同的属性,以便我只能拥有1个测试套件,但是要运行几次,但是要使用不同的数据。

I can make the test suite read the properties from a properties file, that i can change with an environment variable, matching that with the filename of the properties file: 我可以使测试套件从属性文件中读取属性,可以使用环境变量进行更改,使其与属性文件的文件名匹配:

$ export OPERATOR=myoper

and in my java code: 并在我的Java代码中:

        String operator = System.getenv("OPERATOR");
        //Reading properties file in Java example
        Properties props = new Properties();
        //String propertiesFile = "src/test/resources/data" + operator + ".properties";
        FileInputStream fis = new FileInputStream("src/test/resources/data" + operator + ".properties");

        //loading properites from properties file
        props.load(fis);

But I cannot seem to find a way to iterate for all the properties files i could have, so, if i have say, 10 different properties files, that the test suite get executed that many times with all the different set of properties on the files. 但是我似乎找不到找到我可以拥有的所有属性文件的方法,因此,如果我说了10个不同的属性文件,则测试套件将对文件中的所有不同属性集执行了很多次。

I'm using maven and junit4 to run the suites, with the command: 我正在使用maven和junit4通过以下命令运行套件:

mvn test -Dtest=TestSuite#testCase

I'll be launching the command from a Jenkins Job. 我将从詹金斯·乔布(Jenkins Job)启动命令。

Thanks in advance! 提前致谢!

Well after a little research i came up with the following command: 经过一番研究之后,我想到了以下命令:

$ export OPERATOR=dataWeb*
$ for f in src/test/resources/$OPERATOR ; do export OPERATOR=$(basename "$f" | cut -d. -f1) && mvn test -Dtest=$TESTSUITE#$TESTCASE -q ; done

Which takes the environment variable of the operator, that is taken by the test suite to select the corresponding properties file, iterating in as many files exists in the directory with that pattern. 它使用操作员的环境变量,测试套件将其用于测试,以选择相应的属性文件,并在具有该模式的目录中存在的许多文件中进行迭代。

Hope it helps other people in the same situation. 希望它可以帮助处于相同情况的其他人。 Maybe it's a better way to achieve this with the Junit + Maven + Jenkins combo that i'm not aware of. 也许这是我不了解的Junit + Maven + Jenkins组合的一种更好的实现方法。

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

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