使我的并行测试工作参数化

时间:2017-08-09 16:36:52

标签: java junit parameterized

我是junit测试系统的新技能,我想得到任何能够帮助修复@Parameters的人的帮助。在数组中假设有三个值,但我不确定要放入什么。通过使parallelTo测试工作,我只需要在对象测试条件中获得帮助。我对任何其他建议持开放态度。提前致谢

LineTestParallelTo.java

package line;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

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

@RunWith(Parameterized.class)
public class LineTestParallelTo {


private int parallel;
private Line p;
private boolean x;

public void LineTestParallel(int parallel, Line p)
{
    this.parallel = parallel;
    this.p = p; 
    this.x = x;
}

@Parameters
public static Collection<Object[]> testConditions() {

    Line one = new Line(1,1,2,2);
    Line two = new Line(1,1,3,3);
    Line three = new Line(1,1,3,2);

    Object x[][][] = {
            {},
            {},
            {}
    };
    return Arrays.asList(x);
}

@Test
public void test() {
    assertEquals(parallel, x.parallelTo(l) , p.parallelTo(p), 0.0001);
}

}

Line.java

package line;

public class Line {
//Always create a constructor
//private double x0, y0, x1, y1;

// construct a line object
  public Line(double x0, double y0, double x1, double y1) {
    this.x0 = x0;
    this.y0 = y0;
    this.x1 = x1;
    this.y1 = y1;
  }



  // calculate the slope of the line
  public double getSlope( ) {
    // avoid dividing by zero
    if(x1 == x0) {
      throw new ArithmeticException( );
    }

    return (y1 - y0) / (x1 - x0);
  }

  // calculate the distance of the line
  public double getDistance( ) {
    return Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
  }

  // return whether a line is parallel to another
  public boolean parallelTo(Line l) {
    // if the difference between the slopes is very small, consider them parallel
    if(Math.abs(getSlope( ) - l.getSlope( )) < .0001) {
      return true;
    } else {
      return false;
    }
  }

  // private member data
  private double x0, y0, x1, y1;


}

1 个答案:

答案 0 :(得分:0)

您需要的值是您定义为字段的3个参数的值:

private int parallel;
private Line p;
private boolean x;

举个例子:

return Arrays.asList(new Object[][][]{
            {1, one, false},
            {2, two, true},
            {3, three, true}
    });

对于那些对我如何做到这一点感兴趣的人:Junit Parameters Documentation