Ing*_*ürk 43
我们公司的同事为JUnit编写了一个免费提供的TestNG风格的DataProvider,你可以在github上找到它(https://github.com/TNG/junit-dataprovider).
我们在非常大的项目中使用它,它对我们来说效果很好.它比JUnit有一些优势,Parameterized因为它可以减少单独类的开销,也可以执行单个测试.
一个例子看起来像这样
@DataProvider
public static Object[][] provideStringAndExpectedLength() {
return new Object[][] {
{ "Hello World", 11 },
{ "Foo", 3 }
};
}
@Test
@UseDataProvider( "provideStringAndExpectedLength" )
public void testCalculateLength( String input, int expectedLength ) {
assertThat( calculateLength( input ) ).isEqualTo( expectedLength );
}
Run Code Online (Sandbox Code Playgroud)
编辑:从v1.7开始,它还支持其他方式提供数据(字符串,列表),并且可以内联提供程序,因此不一定需要单独的方法.
可以在github的手册页上找到完整的工作示例.它还有一些功能,比如在实用程序类中收集提供程序以及从其他类访问它们等.手册页非常详细,我相信你会发现任何问题都可以解答.
dka*_*zel 32
JUnit 4具有参数化测试,它与php数据提供者的功能相同
@RunWith(Parameterized.class)
public class MyTest{
@Parameters
public static Collection<Object[]> data() {
/*create and return a Collection
of Objects arrays here.
Each element in each array is
a parameter to your constructor.
*/
}
private int a,b,c;
public MyTest(int a, int b, int c) {
this.a= a;
this.b = b;
this.c = c;
}
@Test
public void test() {
//do your test with a,b
}
@Test
public void testC(){
//you can have multiple tests
//which all will run
//...test c
}
}
Run Code Online (Sandbox Code Playgroud)
根据您在灵活性和可读性方面的需求,您可以选择Parameterized- junit的内置选项,由dkatzel描述.其他选项是外部图书馆提供的外部junit跑步者,如zohhak,你可以这样做:
@TestWith({
"clerk, 45'000 USD, GOLD",
"supervisor, 60'000 GBP, PLATINUM"
})
public void canAcceptDebit(Employee employee, Money money, ClientType clientType) {
assertTrue( employee.canAcceptDebit(money, clientType) );
}
Run Code Online (Sandbox Code Playgroud)
或junitParams具有不同的功能.只挑选最适合你的东西
您可以使用 JUnit 5 的 ParameterizedTest。这是来自https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing-parameterized-tests/的一个例子:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@DisplayName("Should pass the method parameters provided by the sumProvider() method")
class MethodSourceExampleTest {
@DisplayName("Should calculate the correct sum")
@ParameterizedTest(name = "{index} => a={0}, b={1}, sum={2}")
@MethodSource("sumProvider")
void sum(int a, int b, int sum) {
assertEquals(sum, a + b);
}
private static Stream<Arguments> sumProvider() {
return Stream.of(
Arguments.of(1, 1, 2),
Arguments.of(2, 3, 5)
);
}
}
Run Code Online (Sandbox Code Playgroud)
可以从注释、方法甚至 CSV 文件加载测试参数。
| 归档时间: |
|
| 查看次数: |
42543 次 |
| 最近记录: |