如何测试多个带有True或False的测试值

huangapple go评论70阅读模式
英文:

How to test multiple testValues with True or False behind it

问题

I want that the method public void testinputBiggerThan() is performed with all testvalue in the method public static Collection testValues().

So I want to give multiple parameters with a true or false condition of that testValue and then this been evaluated inside AssertTrue

@RunWith(Parameterized.class)
public class ParamaterizedTest {

  @Parameterized.Parameters
  public static Collection testValues() {
    return Arrays.asList(new Object[][] {
        { 18.5, true },
        { 16.5, false },
        { 19.5, true },
        { 15.5, false },
        { 20.5, true }
    });
  }

  @Test
  public void testinputBiggerThanExpected(){
    Double expected = 17.5;
    AssertTrue(expected < //how do I get here all the values testValues() and evaluated whether this is the case 18.5 is (true because 18.5 is bigger than 17.5)& true in testValues()=true, etc );

  }
}
英文:

I want that the method public void testinputBiggerThan() is performed with all testvalue in the method public static Collection testValues().

So I want to give multiple parameters with a true or false condition of that testValue and than this been evaluated inside AssertTrue

@RunWith(Parameterized.class)
public class ParamaterizedTest {


  @Parameterized.Parameters
  public static Collection testValues() {
    return Arrays.asList(new Object[][] {
        { 18.5, true },
        { 16.5, false },
        { 19.5, true },
        { 15.5, false },
        { 20.5, true }
    });
  }

  @Test
  public void testinputBiggerThanExpected(){
    Double expected = 17.5;
    AssertTrue(expected < //how do I get here all the values testValues() and evaluated whether this is the case 18.5 is (true because 18.5 is bigger than 17.5)& true in testValues()=true, etc );

  }
}

答案1

得分: 1

以下是翻译好的部分:

需要提供一个构造函数,参数的数量和类型应与testValues()方法返回的一致,然后将这些值分配给字段并在测试中使用这些字段。

对于@Parameterized.Parameters方法返回的每组值以及每个@Test注解的方法,Parameterized测试运行器将创建您的测试类的新实例,带有该组值,并运行测试方法(这意味着如果您有5组值和3个测试方法,它将创建5 * 3个您的测试类的实例)。

因此,请修改您的代码如下:

@RunWith(Parameterized.class)
public class ParameterizedTest {

  private final double testDouble;
  private final boolean expectedResult;

  public ParameterizedTest(double testDouble, boolean expectedResult) {
    this.testDouble = testDouble;
    this.expectedResult = expectedResult;
  }

  @Parameterized.Parameters
  public static Collection testValues() {
    return Arrays.asList(new Object[][] {
        { 18.5, true },
        { 16.5, false },
        { 19.5, true },
        { 15.5, false },
        { 20.5, true }
    });
  }

  @Test
  public void testinputBiggerThanExpected(){
    Double expected = 17.5;
    assertEquals(expectedResult, expected < testDouble);
  }
}

另外,您目前正在使用JUnit 4,而在JUnit 5中使用参数化测试的方式既更简单又更强大。我建议您考虑切换。有关更多信息,请参阅JUnit 5用户指南中的参数化测试部分。

英文:

You need to provide a constructor with the right amount and types of parameters as returned by your testValues() method, then you assign the values as fields and use those fields in your test.

For each set of values returned by the @Parameterized.Parameters method, and each @Test annotated method, the Parameterized test runner will create a new instance of your test class, with the set of values, and the run the test method (this means that if you have 5 sets of values and 3 test-methods, it will create 5 * 3 instances of your test class).

So, change your code to:

@RunWith(Parameterized.class)
public class ParamaterizedTest {

  private final double testDouble;
  private final boolean expectedResult;

  public ParameterizedTest(double testDouble, boolean expectedResult) {
    this.testDouble = testDouble;
    this.expectedResult = expectedResult;
  }

  @Parameterized.Parameters
  public static Collection testValues() {
    return Arrays.asList(new Object[][] {
        { 18.5, true },
        { 16.5, false },
        { 19.5, true },
        { 15.5, false },
        { 20.5, true }
    });
  }

  @Test
  public void testinputBiggerThanExpected(){
    Double expected = 17.5;
    assertEquals(expectedResult, expected &lt; testDouble);
  }
}

See also the JUnit 4 documentation on Parameterized tests.

As an aside, you're currently using JUnit 4, and the way of using parameterized tests in JUnit 5 is both simpler and more powerful. I recommend switching. See the JUnit 5 User Guide, section Parameterized Tests for more information.

答案2

得分: 1

你可以首先迁移到JUnit5。你的测试方法 testinputBiggerThanExpected 应该使用 @ParameterizedTest 注解,而不是 @Test。阅读这个链接

public class ParamaterizedTest {

  public static Collection testValues() {
    return Arrays.asList(new Object[][] {
        { 18.5, true },
        { 16.5, false },
        { 19.5, true },
        { 15.5, false },
        { 20.5, true }
    });
  }

  @ParameterizedTest
  @MethodSource("testValues")
  public void testinputBiggerThanExpected(float floatValue, boolean booleanValue){
    Double expected = 17.5;
    assertTrue(expected < floatValue);
  }
}

或者一个更简单的方法:

public class ParamaterizedTest {

  @ParameterizedTest
  @ValueSource(floats = { 18.5, 16.5, 19.5, 15.5, 20.5 })
  public void testinputBiggerThanExpected(float floatValue) {
    Double expected = 17.5;
    assertTrue(expected < floatValue);
  }
}

[1]: https://www.baeldung.com/parameterized-tests-junit-5
英文:

You can first migrate to JUnit5. Your test method testinputBiggerThanExpected should use @ParameterizedTest annotation instead of @Test. Read this link.

public class ParamaterizedTest {

  public static Collection testValues() {
    return Arrays.asList(new Object[][] {
        { 18.5, true },
        { 16.5, false },
        { 19.5, true },
        { 15.5, false },
        { 20.5, true }
    });
  }

  @ParameterizedTest
  @MethodSource(&quot;testValues&quot;)
  public void testinputBiggerThanExpected(float floatValue, boolean booleanValue){
    Double expected = 17.5;
    assertTrue(expected &lt; floatValue);
  }
}

or a more simple approach:

public class ParamaterizedTest {

  @ParameterizedTest
  @ValueSource(floats = { 18.5, 16.5, 19.5, 15.5, 20.5 })
  public void testinputBiggerThanExpected(float floatValue) {
    Double expected = 17.5;
    assertTrue(expected &lt; floatValue);
  }
}

huangapple
  • 本文由 发表于 2023年3月21日 00:49:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定