Java编程:对象测试、数值类型及格式化
1. 使用查询测试类
1.1 测试命令
编写一个能正常运行的类并非易事,需要一套测试来确保类的功能正确。测试命令时,若通过编写并运行一个创建机器人并移动它的短程序,然后查看屏幕来验证机器人是否正确移动,这种方法依赖人工记忆和验证,存在风险。更好的方式是编写一个名为测试框架(test harness)的程序来尽可能自动化测试过程。
编写测试包含以下五个步骤:
1. 决定要测试的方法。
2. 设置已知情况。
3. 确定执行该方法的预期结果。
4. 执行该方法。
5. 验证结果。
以下是一个测试 SimpleBot 类中 move 方法的示例代码:
import becker.util.Test;
public class TestHarness
{
public static void main(String[] args)
{
// Set up a known situation (an empty city; a robot on (4, 2) facing east).
SimpleCity c = new SimpleCity();
SimpleBot karel = new SimpleBot(c, 4, 2, Constants.EAST);
// Execute the move method.
karel.move()
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



