在JUnit中使用相同的测试数据重复多个测试的简明方法

时间:2014-07-24 11:26:31

标签: java junit

我有很多测试 - 在不同的测试类中 - 应该使用相同的样本数据集运行。这是一个示例测试方法。

@Test
public void testTracerErrorPerPixel() {
    // Iterate over all sample images
    // ** This is the line I want to refactor out **
    for (Image image : SampleImages.values()) {

        // Do some tests on those images

        Assert.assertTrue("Tracer error in " + image + " too large after tracing, error per pixel is " + someValue, someValue);
    }
}

@Test
public void testTracerCorrectPixelPercent() {
    // Iterate over all sample images
    // ** This is the line I want to refactor out **
    for (Image image : SampleImages.values()) {

        // Do some different tests on those images

        Assert.assertTrue("Correct pixel percent in " + image + " too low after tracing, % correct " + someValue, someValue);
    }
}

是否有更简洁的标准方法?让for (Image image : SampleImages.values()) {感觉就像这样做的错误方法。

使用TestNG等其他框架是可以接受的。

4 个答案:

答案 0 :(得分:1)

通过交换到TestNG,我能够使用DataProviders来清理它。

这是上面的示例,更改为使用DataProviders。

@Test(
    dataProvider = "images",
    dataProviderClass = Images.class
)
public void testTracerErrorPerPixel(Image image) {
    // Do some tests on those images

    Assert.assertTrue("Tracer error too large after tracing, error per pixel is " + someValue, someValue);
}

@Test(
    dataProvider = "images",
    dataProviderClass = Images.class
)
public void testTracerCorrectPixelPercent(Image image) {
    // Do some different tests on those images

    Assert.assertTrue("Correct pixel percent too low after tracing, % correct " + someValue, someValue);
}

还在Images.class中添加了DataProvider。

@DataProvider(
        name = "images",
        parallel = true
)
public static Object[][] createImages() {
    // Make the array of images
}

我明天会接受这个答案,除非其他人提供的答案在没有交换到TestNG的情况下有效。

答案 1 :(得分:1)

我们正在使用Parameterized亚军。更多信息见in the junit docs

在你的情况下,你可以写

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterTest {

    @Parameters
    public static Iterable<Object[]> data() {
        // extract to some class for readability and reuse
        return Arrays.asList(new Object[][] { 
                { new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB) }, 
                { new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB) } 
        });
    }

    private Image image;

    public ParameterTest(Image aImage) {
        image = aImage;
    }

    @Test
    public void testTracerErrorPerPixel() {
        // Do some tests on those images
        System.out.println(image);
    }

    @Test
    public void testTracerCorrectPixelPercent() {
        // Do some different tests on those images
        System.out.println(image);
    }
}

答案 2 :(得分:0)

我会按如下方式重构这个测试:

@Test
public void testVectorError() {
    // Iterate over all sample images
    for (Image image : SampleImages.values()) {

        PixelComparer comparer = compare(image);

        // Ensure that traced image is close to original image
        Assert.assertTrue("", comparer.rootMeanSquareErrorPerPixel < 0.01d);
    }
}

private PixelComparer compare(Image image) {

    Pixels original = image.getPixels();

    // Trace image to vector
    Tracer tracer = new Tracer();
    tracer.trace(pixels);

    // Redraw image at same resolution
    Pixels tracedAndRedrawn = tracer.render(original.width, original.height);

    PixelComparer comparer = new PixelComparer();
    comparer.compare(original, tracedAndRedrawn);

    return comparer;
}

compare方法可以是测试类的内部方法(如图所示),也可以放入测试实用程序类中,然后由多个测试类重用。

我认为将DRY原则应用于测试以及应用程序代码非常重要。这是重要的,而不是你使用的工具(JUnit,TestNG)。

答案 3 :(得分:0)

package stackoverflow.q_24932384;

import org.junit.Test;

import junit.framework.TestCase;

public class CommonJUnitData extends TestCase{
    private boolean areImageEqual= false;

    @Override
    protected void setUp() throws Exception {
        //Your image code which will set value of areImageEqual value, like:
        areImageEqual = compare.compare(original, tracedAndRedrawn);
    }

    @Test
    public void testTracerError() {
        //Use areImageEqual variable value
    }
}