我有一个测试方法,它输入2个字符串并返回一个double。
而不是像这样为每个人写一个单独的UT:
public void test1() throws Exception {
double answer = nameSimilarity.similarity("aaaa", "abbba");
assertThat(answer, greaterThan(THRESHHOLD));
}
我想写一个像这样的输入批处理文件:
string1 - string 2 - expect result to be greater than THRESHHOLD
aaaa - abbba - True
cccc - abbba - True
cccc - zzzzz - False
你怎么建议我读取文件,解析它并在每一行上运行单元测试?
在junit中是否有任何内置的此类功能与任何约定?
答案 0 :(得分:0)
你可以看一下行为驱动的开发cucumber,它可以支持这种称为" Data Tables"的测试样本数据。
例如:
Scenario Outline: Email confirmation
Given I have a user account with my name "Jojo Binks"
When an Admin grants me <Role> rights
Then I should receive an email with the body:
"""
Dear Jojo Binks,
You have been granted <Role> rights. You are <details>. Please be responsible.
-The Admins
"""
Examples:
| Role | details |
| Manager | now able to manage your employee accounts |
| Admin | able to manage any user account on the system |
答案 1 :(得分:0)
junit中没有这样的功能。另外,在Junit中,您通常会单独运行每个测试用例,并为每个测试用例分别命名。 Junit不鼓励在同一测试用例中运行多个测试(如果20个测试中只有一个成功,或者19个失败,则会失败)。 您的测试用例应具有以下的一般结构:
public void testTooManyCases() {
while get a line and !eof // catch exceptions and in finally close the file
parse the line
//e.g: String[] parts = line.split(","); //use comma to separate in the input file
//calculate the answer for the info in the line
//e.g: double answer = nameSimilarity.similarity(parts[0], parts[1]);
//assert
//e.g.: assertTrue((answer > THRESHHOLD) == new Boolean(parts[2]).booleanValue());
}
你的文件应该在每一行中有类似的东西:
AAAA,abbba,真正
CCCC,ZZZZZ,假