java 如何使用不同的参数运行选定的 junit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30161343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:34:22  来源:igfitidea点击:

How to run selected junit tests with different parameters

javajunitcustomizationparameterized

提问by kvysh

I want to run selected test methods from any test class with different parameters

我想从具有不同参数的任何测试类中运行选定的测试方法

Ex: 1) ClassA -> Test methods A, B

例如:1) ClassA -> 测试方法 A、B

@Test
public void testA(String param) {
    System.out.println("From A: "+param);
}
@Test
public void testB(String param) {
}

2) ClassB -> Test methods C, D

2) ClassB -> 测试方法 C, D

@Test
public void testC(String param) {
    System.out.println("From C: "+param);
}
@Test
public void testD(String param) {
}

From these I want to run following tests
1) testA(from ClassA) two times with diff params "test1" & "test2"
2) testC(from ClassB) two times with diff params "test3" & "test3"

从这些我想运行以下测试
1)testA(来自 ClassA)两次使用差异参数“test1”和“test2”
2)testC(来自 ClassB)两次使用差异参数“test3”和“test3”

Here my test count should show as '4'

这里我的测试计数应该显示为“4”

Can anyone help on this...

谁能帮忙解决这个...

采纳答案by amith

Try using JUnit parameterized testing. Here is a tutorial from TutorialsPoint.com:

尝试使用 JUnit 参数化测试。这是来自 TutorialsPoint.com 的教程

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test.

  • Annotate test class with @RunWith(Parameterized.class).

  • Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.

  • Create a public constructor that takes in what is equivalent to one "row" of test data.

  • Create an instance variable for each "column" of test data.

  • Create your test case(s) using the instance variables as the source of the test data.

The test case will be invoked once for each row of data.

JUnit 4 引入了一个称为参数化测试的新特性。参数化测试允许开发人员使用不同的值一遍又一遍地运行相同的测试。创建参数化测试需要遵循五个步骤。

  • 使用@RunWith(Parameterized.class) 注释测试类。

  • 创建一个用@Parameters 注释的公共静态方法,该方法返回一个对象集合(作为数组)作为测试数据集。

  • 创建一个公共构造函数,它接收相当于一“行”测试数据的内容。

  • 为测试数据的每个“列”创建一个实例变量。

  • 使用实例变量作为测试数据的来源创建您的测试用例。

测试用例将针对每一行数据调用一次。

回答by shivani kaul

Use Parameterized tests provided with Junits, wherein you can pass the parameters at run time.

使用 Junits 提供的参数化测试,您可以在其中在运行时传递参数。

Refer to org.junit.runners.Parameterized(JUnit 4.12 offers the possibility to parametrize with expected values and without expected values in the setup array).

请参阅org.junit.runners.Parameterized(JUnit 4.12 提供了在设置数组中使用预期值和没有预期值进行参数化的可能性)。

Try this:

试试这个:

@RunWith(Parameterized.class)
public class TestA {

   @Parameterized.Parameters(name = "{index}: methodA({1})")
   public static Iterable<Object[]> data() {
      return Arrays.asList(new Object[][]{
            {"From A test1", "test1"}, {"From A test2", "test2"}
      });
   }

   private String actual;
   private String expected;

   public TestA(String expected,String actual) {
      this.expected = expected;
      this.actual = actual;
   }

   @Test
   public void test() {
      String actual = methodFromA(this.actual);
      assertEquals(expected,actual);
   }

   private String methodFromA(String input) {
      return "From A " + input;
   }
}

you can write a similar test for class B.

您可以为 B 类编写类似的测试。

For a test taking just single parameters, fro JUnit 4.12 you can do this:

对于仅采用单个参数的测试,从 JUnit 4.12 开始,您可以执行以下操作:

@RunWith(Parameterized.class)
public class TestU {

    /**
     * Provide Iterable to list single parameters
     */

    @Parameters
    public static Iterable<? extends Object> data() {
        return Arrays.asList("a", "b", "c");
    }

    /**
     * This value is initialized with values from data() array
     */

    @Parameter
    public String x;

    /**
     * Run parametrized test
     */

    @Test
    public void testMe() {
        System.out.println(x);
    }
}