简体   繁体   English

将参数传递给TestSuite类的JUnit测试

[英]Pass parameters to JUnit test from the TestSuite class

I want to create 2 JUnit TestSuites. 我想创建2个JUnit TestSuite。 They both utilize the same test classes, but they should each use different parameters. 它们都使用相同的测试类,但它们应该使用不同的参数。 For example, in test suite A, I want my data to be collected from file A and to be written to database A. In test suite B, I want my data to be collected from file B and to be written to databaseB. 例如,在测试套件A中,我希望从文件A收集数据并将其写入数据库A.在测试套件B中,我希望从文件B收集数据并将其写入databaseB。

The reason I use testSuites for this is because: 我之所以使用testSuites是因为:

  1. I can put all the specific parameters in the testsuite classes 我可以将所有特定参数放在testsuite类中
  2. I can reuse the testclasses 我可以重用测试类
  3. I can choose which testsuite to run. 我可以选择运行哪个测试套件。 I do not want all tests to always run with all possible paramaters! 我不希望所有测试始终与所有可能的参数一起运行!

The problem is I cannot really pass the parameters. 问题是我无法真正传递参数。 I understand the way the Parameterized class works with JUnit, but it does not allow point 3 in the list above. 我理解Parameterized类与JUnit一起工作的方式,但它不允许上面列表中的第3点。 If I use the code below it will run my test class with both databse connections, which is not what I want to achieve. 如果我使用下面的代码,它将运行我的测试类与两个数据库连接,这不是我想要实现的。

@RunWith(value = Parameterized.class)
public class TestCheckData
{
    private File file;
    private DatabaseSource databaseSource;

    public TestCheckData(File file, DatabaseSource databaseSource)
    {
        this.file = file;
        this.databaseSource = databaseSource;
    }

    @Parameters
    public static Iterable<Object[]> data1()
    {
        return Arrays.asList(new Object[][]
        {
            { TestSuiteA.DATA_FILE_A, TestSuite1.DATABASE_A }, 
            { TestSuiteB.DATA_FILE_B, TestSuite1.DATABASE_B }
        });


    }

I already find some way of passing configurations in a spring context in this question , but I'm not using any special framework. 我已经在这个问题的Spring环境找到了一些传递配置的方法,但我没有使用任何特殊的框架。

Well, this would be a little unconventional, but you could add a different Test class to the beginning of each suite run that would set the parameters you want to use for that test. 嗯,这有点不同寻常,但你可以在每个套件运行的开头添加一个不同的Test类,它将设置你想要用于该测试的参数。 So you'd have classes like: 所以你有这样的课程:

public abstract class StaticParameters {
  public static File dataFileToUse = null;
  public static DatabaseSource databaseToUse = null;
}

public class Suite1Params extends StaticParameters {
  @BeforeClass
  public static void setParams() {
    dataFileToUse = DATA_FILE_A;
    databaseToUse = DATABASE_A;
  }
}

public class Suite2Params extends StaticParameters {
  @BeforeClass
  public static void setParams() {
    dataFileToUse = DATA_FILE_B;
    databaseToUse = DATABASE_B;
  }
}

Then you'd just make Suite1Params or Suite2Params the first in your suite list. 然后,您只需将Suite1ParamsSuite2Params作为套件列表中的第一个。 You might have to add a fake @Test entry to the params classes, I'm not sure if the Suite runner requires that. 你可能不得不在params类中添加一个假的@Test条目,我不确定Suite runner是否需要这样。

You could modify the tests so that they get the parameters from a config file. 您可以修改测试,以便从配置文件中获取参数。 This way you would always only have 1 Suite. 这样你总是只有1套房。

The path of the config file can be looked up via a System property. 可以通过System属性查找配置文件的路径。

Then on the invocation of the test suite, you could pass in a different config file by changing the property using the -D option on the JVM. 然后,在调用测试套件时,您可以通过使用JVM上的-D选项更改属性来传入不同的配置文件。

So for example if you named the proprerty env.properties then your command would be: 因此,例如,如果您将proprerty命名为env.properties那么您的命令将是:

 %java -Denv.properties=prod.config runMyTests

or 要么

 %java -Denv.properties=dev.config runMyTests

etc 等等

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

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