New*_*bie 3 testing unit-testing
我应该这样做
Test1(){
Assert(TestCase1);
Assert(TestCase1);
Assert(TestCase3);
...
}
Run Code Online (Sandbox Code Playgroud)
要么
Test1(){
Assert(TestCase1);
}
Test2(){
Assert(TestCase2);
}
Test3(){
Assert(TestCase3);
}
...
Run Code Online (Sandbox Code Playgroud)
注意:所有测试用例都是密切相关的.它们之间只有布尔差异.
一个测试用例=一个要测试的条件,一些人将条件转换为断言,这是错误的,一个条件可以由一个或多个断言组成
示例:假设您正在开发一款国际象棋游戏,并且您刚刚实现了移动功能,并且您想测试它,请检查以下测试用例.
public void testPawnCanMoveTwoSquaresAheadFromInitialRow (){
[...]
//Test moving first a white pawn
assertPawnCanMoveTwoSquaersAheadFromInitialRow ("a2", "a4");
//Test moving fater a black pawn
assertPawnCanMoveTwoSquaersAheadFromInitialRow ("h7", "h5");
}
private void assertPawnCanMoveTwoSquaersAheadFromInitialRow (String from, String to){
[...]
Piece movingPiece = board.getSquareContent(from);
assertTrue (movingPiece.isPawn ());
assertTrue (board.move (from, to));
assertTrue (board.getSquareContent(from).isEmpty());
assertTrue (board.getSquareContent(to).isPawn());
[...]
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,此示例非常清楚,如果失败,您将确切知道应用程序失败的位置,使添加新测试用例变得非常容易,并且您可以看到测试只有一个条件,但使用了很多断言.
您可能想查看我在博客上写的这篇最近的文章:如何编写好的测试