当前位置: 代码迷 >> 综合 >> 软工中级实训Stage2-Part3:testreport
  详细解决方案

软工中级实训Stage2-Part3:testreport

热度:53   发布时间:2024-03-06 02:22:03.0

testreport

1.需求分析

自主使用Junit完成对Jumper类的测试

2. 具体测试细节

2.1 Jumper类相关成员函数的介绍

由于截图无法被渲染所以能被代码替代的部分都选择了代码。

2.1.1 构造函数

    //constructorpublic Jumper() {setColor(Color.BLUE);}public Jumper(Color jumperColor) {setColor(jumperColor);}

2.1.2 act()函数

功能:选择Jumper进行move、jump、turn且jump优先级高于move

    //actpublic void act() {if(canJump()){Jump();}else if (canMove()) {move();} else {turn();}}

**2.1.3 turn()函数 **

功能:当Jumper无法进行jump或者move时候进行turn,转换角度

    ///turnpublic void turn() {setDirection(getDirection() + Location.HALF_RIGHT);}

2.1.4 move()函数与canMove()函数

功能:
1. move()函数:Jumper往前移动一步
2. canMove()函数:判断Jumper是否可以move

    // movepublic void move() {Grid<Actor> gr = getGrid();if (gr == null) {return;}//new positionLocation loc = getLocation();Location next = loc.getAdjacentLocation(getDirection());if (gr.isValid(next)) {moveTo(next);} //removeelse {removeSelfFromGrid();}Flower flower = new Flower(getColor());flower.putSelfInGrid(gr,loc);}// judge if bug can movepublic boolean canMove() {//new a thingsGrid<Actor> gr = getGrid();if (gr == null) {return false;}Location loc = getLocation();//new positionLocation next = loc.getAdjacentLocation(getDirection());if (!gr.isValid(next)) {return false;}//next positionActor neighbor = gr.get(next);return (neighbor == null) || (neighbor instanceof Flower);}

2.1.5 jump()函数与canJump()函数

功能:
1. jump()函数:Jumper往前移动两步
2. canJump()函数:判断Jumper是否可以jump

    //jumppublic void Jump(){Grid<Actor> gr = getGrid();if (gr == null) {return ;}//new positionLocation loc = getLocation();Location next = loc.getAdjacentLocation(getDirection());if(gr.isValid(next)){Location next2 = next.getAdjacentLocation(getDirection());if(gr.isValid(next2)){moveTo(next2);}// removeelse{removeSelfFromGrid();}}}//judge if bug can jumppublic boolean canJump(){Grid<Actor> gr = getGrid();if (gr == null) {return false;}//new seatLocation loc = getLocation();Location next = loc.getAdjacentLocation(getDirection());Location next2 = next.getAdjacentLocation(getDirection());//if bug couldn't find do other thingsif(!gr.isValid(next2)){return false ;}Actor neighbor = gr.get(next2);return (neighbor == null || neighbor instanceof Flower);}

2.2 函数测试

这一部分我选择了4个函数进行测试,分别是:

  1. act()函数
  2. canMove()函数
  3. move()函数
  4. Jump()函数
  5. turn()函数

2.2.1 act()函数测试


2.2.2 canMove()函数测试

    // test if bug can judge move @Testpublic void canMoveTest() {final int x = 5, y = 5;Jumper jumper = new Jumper();ActorWorld world = new ActorWorld();world.add(new Location(x, y), jumper);assertTrue("Failure - can move", jumper.canMove());Rock rock = new Rock();world.add(new Location(x-1, y), rock);assertFalse("Failure - can not move", jumper.canMove());Jumper jumper2 = new Jumper();world.add(new Location(0, 0), jumper2);assertFalse("Failure - can not move due to the edge", jumper2.canMove());}

2.2.3 move()函数测试

    // test if bug can move   @Testpublic void moveTest() {final int x = 5, y = 5;Jumper jumper = new Jumper();ActorWorld world = new ActorWorld();world.add(new Location(x, y), jumper);jumper.move();assertEquals("Failure - jumper should move forward with a distance of 1", x-1, jumper.getLocation().getRow());assertEquals("Failure - jumper should move forward with a distance of 1", y, jumper.getLocation().getCol());}

2.2.4 Jump()函数测试

    // test if bug can jump   @Testpublic void jumpTest() {final int x = 5, y = 5;Jumper jumper = new Jumper();ActorWorld world = new ActorWorld();world.add(new Location(x, y), jumper);jumper.Jump();assertEquals("Failure - jumper should move forward with a distance of 2", x-2, jumper.getLocation().getRow());assertEquals("Failure - jumper should move forward with a distance of 2", y, jumper.getLocation().getCol());}

2.2.5 turn()函数测试

   // test if bug can turn @Testpublic void turnTest() {final int x = 5, y = 5;Jumper jumper = new Jumper();ActorWorld world = new ActorWorld();world.add(new Location(x, y), jumper);jumper.turn();assertEquals("Failure - jumper should turn 45 degrees to the right", Location.NORTHEAST, jumper.getDirection());}

2.3 测试结果及详解

2.3.1 actTest

  1. 首先把jumper放置在点(5,5)上,随后jumper进行act,由于地图上并没有障碍物且jump之后并不会出界,所以此时jumper进行jump
  2. 完成jump之后,jumper的X坐标减2,此时jumper.getXposition() = 3 , 而 x - 2 = 3 ,由于x = jumper.getXposition()相等所以返回true;
  3. 放置一个rock到点(1,4)上
  4. jumper继续运动,由于此时jumper再继续向前跳跃时候,会碰到rock,所以此时jumper选择进行move,向前move一步
  5. 此时jumper执行turn,方向变为东北方向
  6. jumper完成move之后,jumper的X坐标减1,此时jumper.getXposition() = 2 ,而此时x - 3 = 2 ,所以 x - 3 = jumper.getXposition() ,并且NORTHEAST,=jumper.getDirection(),所以最终返回true

2.3.2 canMoveTest

  1. 首先把jumper放置在点(5,5)上
  2. 进行canMove()测试,由于jumper可以移动,所以返回true

2.3.3 moveTest

  1. 首先把jumper放置在点(5,5)上
  2. jumper进行move之后,jumper的x坐标减小1,此时jumper.getXposition() = 4
  3. 由于原本的x = x - 1 = 4 ,此时x与jumper.getXposition()相等,于是就**返回true
    **

2.3.3 jumpTest

  1. 首先把jumper放置在点(5,5)上
  2. jumper进行Jump之后,jumper的x坐标减小2,此时jumper.getXposition() = 3
  3. 由于原本的x = x - 2 = 3 ,此时x与jumper.getXposition()相等,于是就返回true

2.3.5 turnTest

  1. 在part2部分中,已经定义了NORTHEAST是北偏东45°,并且定义jumper面朝正北,且自身旋转顺序为顺时针;
  2. 这里jumper进行turn之后,是顺时针旋转45°,此时也就是相对于原来未知的北偏东45°方向,所以返回true
  相关解决方案