采用有限状态机模型实现针对JPacman项目的基于模型的测试①根据docscenariosmd文中隐含的状态构建状态机模型状态图应该至少包括当游戏暂停游戏胜利和游戏失败的时候应该发生什么工作。使用UML绘制状态机。②从状态机图生成状态转换树;③构建状态转换表针对stateevent对设计测试用例;④编写测试类实现测试用例。
暂停状态:

胜利状态:

失败状态:

状态转换树:

状态转换表:
| 状态 | 事件 | 下一状态 | | ------------- |:-------------:| -----:| | 游戏进行中 | 吃豆子 | 游戏进行中 | | 游戏进行中 | 吃超级豆子 | 吃了超级豆子 | | 游戏进行中 | 吃果实 | 游戏进行中 | | 游戏进行中 | 吃到幽灵 | 游戏失败 | | 游戏进行中 | 时间耗尽 | 游戏失败 | | 游戏进行中 | 暂停 | 游戏暂停 | | 游戏暂停 | 继续 | 游戏进行中 | | 游戏暂停 | 退出 | 游戏结束 | | 吃了超级豆子 | 时间耗尽 | 游戏进行中 | | 吃了超级豆子 | 吃到幽灵 | 游戏进行中 | | 吃了超级豆子 | 吃果实 | 游戏进行中 | | 吃了超级豆子 | 吃豆子 | 游戏进行中 | | 游戏失败 | 重新开始 | 游戏进行中 | | 游戏失败 | 退出 | 游戏结束 | | 游戏胜利 | 下一关 | 游戏进行中 | | 游戏胜利 | 退出 | 游戏结束 |
测试用例:
@Test
public void testEatDot() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃豆子
game.move(Direction.EAST);
game.move(Direction.EAST);
game.move(Direction.EAST);
game.move(Direction.EAST);
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testEatSuperDot() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃超级豆子
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 断言游戏状态为吃了超级豆子
assertEquals(GameState.State.POWER_PILL, state.getState());
}
@Test
public void testEatFruit() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃果实
game.move(Direction.NORTH);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testCollideGhost() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 移动到幽灵
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.NORTH);
game.move(Direction.NORTH);
// 断言游戏状态为失败
assertEquals(GameState.State.LOST, state.getState());
}
@Test
public void testTimeUp() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 时间耗尽
for (int i = 0; i < 1000; i++) {
game.tick();
}
// 断言游戏状态为失败
assertEquals(GameState.State.LOST, state.getState());
}
@Test
public void testPause() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 暂停游戏
game.pause();
// 断言游戏状态为暂停
assertEquals(GameState.State.PAUSED, state.getState());
}
@Test
public void testResume() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 暂停游戏
game.pause();
// 继续游戏
game.resume();
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testExit() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 退出游戏
game.exit();
// 断言游戏状态为结束
assertEquals(GameState.State.EXIT, state.getState());
}
@Test
public void testPowerPillTimeUp() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃超级豆子
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 时间耗尽
for (int i = 0; i < 1000; i++) {
game.tick();
}
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testPowerPillCollideGhost() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃超级豆子
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 移动到幽灵
game.move(Direction.NORTH);
game.move(Direction.NORTH);
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testPowerPillEatFruit() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃超级豆子
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 吃果实
game.move(Direction.NORTH);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testPowerPillEatDot() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃超级豆子
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
// 吃豆子
game.move(Direction.EAST);
game.move(Direction.EAST);
game.move(Direction.EAST);
game.move(Direction.EAST);
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testRestart() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 移动到幽灵
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.WEST);
game.move(Direction.NORTH);
game.move(Direction.NORTH);
// 重新开始
game.restart();
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testNextLevel() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 吃豆子
game.move(Direction.EAST);
game.move(Direction.EAST);
game.move(Direction.EAST);
game.move(Direction.EAST);
// 下一关
game.nextLevel();
// 断言游戏状态为进行中
assertEquals(GameState.State.PLAYING, state.getState());
}
@Test
public void testExitGame() {
Game game = new Game();
GameState state = game.getGameState();
// 初始化游戏状态
state.reset();
// 开始游戏
game.start();
// 退出游戏
game.exit();
// 断言游戏状态为结束
assertEquals(GameState.State.EXIT, state.getState());
}
``
原文地址: https://www.cveoy.top/t/topic/hnvo 著作权归作者所有。请勿转载和采集!