使用2种方法的java参数化测试
嗨,我正在尝试对我的java代码使用参数化测试。我想测试两种方法。一个将英里转换为公里,另一个将公里转换为英里。是否有一种方法可以为一个参数化测试测试两种方法,或者我必须为每个方法创建另一个参数化测试使用2种方法的java参数化测试,java,parameterized,Java,Parameterized,嗨,我正在尝试对我的java代码使用参数化测试。我想测试两种方法。一个将英里转换为公里,另一个将公里转换为英里。是否有一种方法可以为一个参数化测试测试两种方法,或者我必须为每个方法创建另一个参数化测试 import static org.junit.Assert.*; import java.util.Collection; import java.util.InputMismatchException; import java.util.Arrays; import org.junit.A
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.InputMismatchException;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
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 testLengthConverter {
private double lengths;
private double expected;
public testLengthConverter(double expected , double length){
this.expected = expected;
this.lengths = length;
}
@Parameters
public static Collection<Object[]> lengthKm(){
return Arrays.asList( new Object[][]
{ {8.04672000307346, 5} ,
{0.8046720003073461, .5},
{3.10685596,5},
{0.310685596, .5}});
}
@Parameters
public static Collection<Object[]> lengthMile(){
return Arrays.asList( new Object[][]
{});
}
@Test
public void calcMile ()
{
LengthConverter lengthMile= new LengthConverter();
assertEquals(expected,lengthMile.kmToMile(lengths),.0001);
}
@Test
public void calcKm ()
{
LengthConverter lengthKm= new LengthConverter();
assertEquals(expected,lengthKm.mileToKm(lengths), .0001);
}
}
import static org.junit.Assert.*;
导入java.util.Collection;
导入java.util.InputMismatchException;
导入java.util.array;
导入org.junit.Assert;
导入org.junit.Before;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.junit.runners.Parameterized;
导入org.junit.runners.Parameterized.Parameters;
@RunWith(参数化的.class)
公共类TestLength转换器{
私人双倍长度;
私人双预期;
公共测试长度转换器(预期双倍,双倍长度){
this.expected=expected;
这个。长度=长度;
}
@参数
公共静态收集长度mm(){
返回Arrays.asList(新对象[][]
{ {8.04672000307346, 5} ,
{0.8046720003073461, .5},
{3.10685596,5},
{0.310685596, .5}});
}
@参数
公共静态集合长度文件(){
返回Arrays.asList(新对象[][]
{});
}
@试验
公共无效计算文件()
{
LengthConverter LengthFile=新的LengthConverter();
资产质量(预期,长度文件.kmt文件(长度),.0001);
}
@试验
公共无效计算器()
{
LengthConverter lengthKm=新的LengthConverter();
资产质量(预计长度、厚度、英里/公里(长度),.0001);
}
}
不同的测试方法更好,如果第二次失败@JigarJoshi,则会缩小测试范围。记住,您创建测试是为了让您的软件在出现错误时能够失败,并使您能够快速、肯定地发现错误;如果不是这样的话,它将错过整个要点——你的测试将“腐烂”,最终从长远来看失去价值。我知道这不是测试程序的最佳方式,但这是一个家庭作业。我只是想弄清楚是否有可能为一个参数化测试分别测试两个方法。谢谢你的输入