[英]Test with different parameters
我有以下方案進行測試。 我想知道哪種測試框架最適合我的要求。
場景1)參數1 ,參數2,參數3,參數4,參數5
我將通過上面的參數號 1,2,3參數..直到20。
對於每個數字(1到20),將生成一個文件,它是測試輸出數據。 我需要將此輸出數據與Expected數據(也是一個文件)進行比較,如果兩個文件(測試輸出文件和期望數據文件)相同,則將生成結果,否則為false。
測試的輸入如下:Param1,Param2,Param3,Param4,Param5,數字,預期數據文件(測試輸出將與之比較)
場景2)param1,param2,param3,param4,param5
這里,將不同的值分配給上述變量,然后將這些值再次傳遞給測試20次,並且每次將生成不同的Test輸出文件(總共20個輸出文件),然后將其與期望的數據文件進行比較。 (預期數據也有20個文件。)
我有15種情況。 哪種測試框架最適合這里? 參數化Junit是否合適? 請也提供一些准則,以使用推薦的框架。
以Spock測試為例,其中所有參數和數字都可以不同:
@Unroll
def "scenario with different parameters"() {
given:
def service = new MyService()
when:
def actualDataFile = service.doSomething(param1, param2, param3, param4, param5, number)
then:
readFileAsString(actualDataFile) == readFileAsString(expectedDataFileName)
where:
param1 | param2 | param3 | param4 | param5 | number | expectedDataFileName
'any' | 'aaa' | 'any' | 'any' | 'any' | 1 | 'expectedA.txt'
'any' | 'aay' | '0ny' | 'any' | 'any' | 2 | 'expectedB.txt'
'any' | 'ady' | '3ny' | 'a__' | 'a3y' | 3 | 'expectedC.txt'
'any' | 'ady' | '3ny' | 'a__' | 'a3y' | 4 | 'expectedD.txt'
'any' | 'ady' | '3ny' | 'a__' | 'a3y' | 5 | 'expectedE.txt'
'any' | 'ady' | '3ny' | 'a__' | 'a3y' | 6 | 'expectedF.txt'
'any' | 'ady' | '3ny' | 'a__' | 'a3y' | 7 | 'expectedG.txt'
'any' | 'ady' | '3ny' | 'a__' | 'a3y' | 8 | 'expectedH.txt'
// etc
}
1.哪種測試框架最適合這里?
答案: 參數化的類
2.參數化Junit是否合適?
答:是的,您是對的
3.也請提供一些指南,以使用推薦的框架。
回答:用於聲明實際和預期結果。
假設FileUtils.contentEquals(file1, file2)
char檢查文件char的內容,可以使用FileUtils.contentEquals(file1, file2)
commons apache io
讓我們看這個例子:
public class Calc{
public static int add(int a, int b) {
return a + b;
}
}
朱尼特
@RunWith(value = Parameterized.class)
public class ParameterizedTest {
private int numberA;
private int numberB;
private int expected;
// Inject via constructor
// for {8, 2, 10}, numberA = 8, numberB = 2, expected = 10
public ParameterizedTest(int numberA, int numberB, int expected) {
this.numberA = numberA;
this.numberB = numberB;
this.expected = expected;
}
// name attribute is optional, provide an unique name for test
// multiple parameters, uses Collection<Object[]>
@Parameters(name = "{index}: testAdd({0}+{1}) = {2}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{1, 1, 2},
{2, 2, 4},
{8, 2, 10},
{4, 5, 9},
{5, 5, 10}
});
}
@Test
public void test_addTwoNumbes() {
assertThat(Calc.add(numberA, numberB), is(expected));
}
}
通過將Junit參數化測試與某些YAML解析結合,我可以創建可讀的參數表。 每個表行將被解析為一個Map,每個值都將使用yaml解析器進行解析。 這樣,每個映射都包含類型化的實例。 在這種情況下甚至列出。
@RunWith(Parameterized.class)
public class AnotherParameterizedTest {
private final HashMap row;
@Parameterized.Parameters(name="Reverse Lists Tests # {index}:")
public static List<Map<String, Object>> data() {
final TestData testData = new TestData(""+
"| ID | List | Expected | \n"+
"| 0 | [1, 2, 3] | [3, 2, 1] | \n"+
"| 1 | [2, 3, 5] | [5, 3, 2] | \n"+
"| 2 | [5, 6, 7] | [ 7, 6, 5] | \n"
);
// parsing each row using simple YAML parser and create map per row
return testData.getDataTable();
}
public AnotherParameterizedTest(HashMap obj) {
this.row = obj;
}
@Test
public void test() throws Exception {
List orgListReversed = new ArrayList((List) row.get("List"));
Collections.reverse(orgListReversed);
assertEquals((List) row.get("Expected"), orgListReversed);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.