中山大学中级实训--测试文档

测试文档

一、测试项目

A class called Jumper. This actor can move forward two cells in each move. It “jumps” over rocks and flowers. It does not leave anything behind it when it jumps.

二、测试环境

JUnit version 4.10

java version 1.8.0_181

通过使用Junit进行测试,通过运行已经编写好的测试案例,每个案例都根据对应的测试内容进行编写。运行测试程序后,判断程序的输出是否与预期相同,一致就说明程序正确;不一致则是程序存在该测试范围内的错误。该测试中,通过创建不同种类、位置的actor来实现程序的测试。

三、 测试案例

1. 测试Jumper两格移动

初始化:

创建一个位置为(0, 0),方向向东(Location.EAST)的Jumper。

期望:

运行一个act()后Jumper的位置变为(0, 2)

代码:

public void testJumperMove() {
    ActorWorld world = new ActorWorld();
    Jumper jumper = new Jumper();
    //set direction to east
    jumper.setDirection(Location.EAST);
    world.add(new Location(0, 0), jumper);
    jumper.act();
    assertEquals(0, jumper.getLocation().getRow());
    assertEquals(2, jumper.getLocation().getCol());
  }
2. 测试Jumper跳过障碍物

初始化:

创建一个位置为(0, 0),方向向东(Location.EAST)的Jumper。

创建一个位置为(0, 1) 的Rock。

期望:

运行一个act()后Jumper的位置变为(0, 2)

代码:

public void testJumperTurn() {
    ActorWorld world = new ActorWorld();
    Jumper jumper = new Jumper();
    Rock rock = new Rock();
    jumper.setDirection(Location.EAST);
    world.add(new Location(0, 0), jumper);
    world.add(new Location(0, 2), rock);
    jumper.act();
    rock.act();
    assertEquals(Location.SOUTHEAST, jumper.getDirection());
  }
3. 测试Jumper被阻挡转向

初始化:

创建一个位置为(0, 0),方向向东(Location.EAST)的Jumper。

创建一个位置为(0, 2) 的Rock。

期望:

运行一个act()

Jumper的位置不变为(0, 0)

Jumperd的方向变为东南(Location.SOUTHEAST)

代码:

public void testJumperTurn() {
    ActorWorld world = new ActorWorld();
    Jumper jumper = new Jumper();
    Rock rock = new Rock();
    jumper.setDirection(Location.EAST);
    world.add(new Location(0, 0), jumper);
    world.add(new Location(0, 2), rock);
    jumper.act();
    rock.act();
    assertEquals(Location.SOUTHEAST, jumper.getDirection());
    assertEquals(0, jumper.getLocation().getRow());
    assertEquals(0, jumper.getLocation().getCol());
  }
4. 测试Jumer面前为边界的转向

初始化:

创建一个位置为(0, 0),方向向西(Location.WEST)的Jumper。

期望:

运行一个act()

Jumper的位置不变为(0, 0)

Jumperd的方向变为西北(Location.NORTHWEST)

代码:

public void testJumperEdge() {
      ActorWorld world = new ActorWorld();
      Jumper jumper = new Jumper();
      world.add(new Location(0, 0), jumper);
      jumper.setDirection(Location.WEST);
      jumper.act();
      assertEquals(Location.NORTHWEST, jumper.getDirection());
      assertEquals(0, jumper.getLocation().getRow());
      assertEquals(0, jumper.getLocation().getCol());
  }
5. 测试Jumer移动一格后为边界的转向

初始化:

创建一个位置为(0, 1),方向向西(Location.WEST)的Jumper。

期望:

运行一个act()

Jumper的位置不变为(0, 1)

Jumperd的方向变为西北(Location.NORTHWEST)

代码:

public void testJumperOut() {
      ActorWorld world = new ActorWorld();
      Jumper jumper = new Jumper();
      world.add(new Location(0, 1), jumper);
      jumper.setDirection(Location.WEST);
      jumper.act();
      assertEquals(Location.NORTHWEST, jumper.getDirection());
      assertEquals(0, jumper.getLocation().getRow());
      assertEquals(1, jumper.getLocation().getCol());
  }

四、实验结果

运行命令(以源文件为当前目录):

//编译
javac -classpath .:./../../gridworld.jar:./../../junit.jar JumperTest.java 
//运行测试
java -classpath .:./../../gridworld.jar:./../../junit.jar org.junit.runner.JUnitCore JumperTest

测试结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40135006/article/details/103223386