我最近開始玩tdd並遇到了一個問題,我不明白爲什麼有一件事正在工作,另一件事不起作用。在IntelliJ IDEA中使用DataProvider失敗的TestNG測試
下面的代碼工作對我來說:
public class Ant {
public Ant(Point startLocation, Point hive) {
this.currentLocation = new Point(startLocation);
this.hive = new Point(hive);
}
public void goHome() {
if (hive.x > currentLocation.x) {
currentLocation.x++;
} else if (hive.x < currentLocation.x){
currentLocation.x--;
}
if (hive.y > currentLocation.y) {
currentLocation.y++;
} else if (hive.y < currentLocation.y){
currentLocation.y--;
}
}
}
相應的測試:
@DataProvider(name = "goneHome")
public static Object[][] goHome() {
return new Object[][] {
{new Point(2,1), new Point(3,2), new Point(7,8)},
{new Point(20,1), new Point(19,2), new Point(7,8)},
{new Point(23,10), new Point(22,9), new Point(7,8)},
{new Point(2,10), new Point(3,9), new Point(7,8)},
{new Point(2,8), new Point(3,8), new Point(7,8)},
{new Point(7,1), new Point(7,2), new Point(7,8)}
};
}
@Test(dataProvider = "goneHome")
public void testGoHome(Point currentPosition, Point nextPosition, Point hive)
throws Exception {
Ant ant = new Ant(currentPosition, hive);
ant.move();
assertEquals(ant.getCurrentLocation(), nextPosition);
}
測試失敗,如果我改變螞蟻構造是這樣的:
public Ant(Point startLocation, Point hive) {
this.currentLocation = startLocation;
this.hive = hive;
}
由於沒有我的意思是前兩套DataProvider的測試正常工作,其餘的失敗/未完成G。 雖然我不太確定什麼失敗。如果我刪除DataProvider中的前兩組數據,仍然只有前兩個數據集(其中第三個和第四個數據集之前)不會失敗。
我使用IntelliJ並且除「失敗」測試之外的符號仍然是「加載圖標」。
調試每個單獨的測試案例顯示點設置正確。從測試中移除斷言不會改變任何東西。
有人可以向我解釋這種行爲嗎?
在此先感謝
埃貢
編輯:糾正失敗
您是否嘗試將測試作爲Maven目標運行?也許這是Idea – 2014-11-24 20:24:02
與maven合作的錯誤,謝謝!我花了太多時間思考這個^^ – 2014-11-24 20:46:18
即使最聰明的工具也可能失敗 – 2014-11-24 20:48:22