简体   繁体   English

如何使用junit从一种方法运行差异测试

[英]How to run differents tests from one single method with junit

I have a test that does a cycle to run the same test but with different inputs. 我有一个测试,执行一个循环来运行相同的测试,但是输入不同。 The problem is that when one assert fails, then the test stops, and it is marked as if one test failed. 问题在于,当一个断言失败时,测试将停止,并且将其标记为好像一个测试失败。

Here is the code: 这是代码:

@Test
public void test1() throws JsonProcessingException {
    this.bookingsTest(Product.ONE);
}

@Test
public void test2() throws JsonProcessingException {
    this.bookingsTest(Product.TWO);
}

public <B extends Sale> void bookingsTest(Product product) {

    List<Booking> bookings = this.prodConnector.getBookings(product, 100);

    bookings.stream().map(Booking::getBookingId).forEach((bookingId) -> {
        this.bookingTest(bookingId);
    });
}

public <B extends Sale> void bookingTest(String bookingId) {
    ...
    // Do some assert:
    Assert.assertEquals("XXX", "XXX");
}

In that case, the methods test1 and test2, execute as two different tests, but inside them, I do a cycle to check some stuff on every item that is returned by the collection. 在那种情况下,方法test1和test2作为两个不同的测试执行,但是在它们内部,我执行了一个循环来检查集合返回的每个项目中的某些内容。 But what I want is to make that test on each item to be treated as a different one, so that if one fails, the others continue executing and I can see which one failed and how many failed over the total. 但是我想要对每个项目进行该测试,以将其视为不同的项目,这样,如果一项失败,则其他项继续执行,我可以看到总项中哪一项失败,有多少失败。

what you described is parameterized testing. 您描述的是参数化测试。 there is plenty of frameworks that may help you. 有很多可以帮助您的框架。 they just have different simplicity to power ratio. 它们只是简单的功率比不同。 just pick the one that fits your needs. 只需选择一个适合您的需求即可。

junit's native way is @Parameterized but it's very verbose. junit的本机方式是@Parameterized,但是它很冗长。 other junit plugins that you may find useful are zohhak or junit-dataprovider . 您可能会发现有用的其他junit插件是zohhakjunit-dataprovider if you are not enforced to use junit and/or plain java, you can try spock or testng . 如果您没有被强制使用junit和/或纯java,则可以尝试使用spocktestng

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

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