什么是Java中的测试驱动开发(TDD)以及如何自动化测试用例

时间:2014-05-14 13:30:52

标签: java eclipse algorithm unit-testing tdd

我们的想法是尝试在0-9,前进,后退和上一个频道按钮上找到所需的最小点击次数,以通过给定范围内的给定频道序列。按下前进或后退按钮时,将自动跳过被阻止的频道。

这是我的工作代码(我已经多次皱眉,只是在没有作业的情况下提问,所以我决定用这个代码完成一个完整的代码。)。如何使用java和eclipse自动化测试用例来展示TDD原理? Eclipse是我编写的编辑器。

    package Algo;

    import java.util.*;

    public class indix {

        public static void main(String args[]) {

            String range;
            String blocked;
            String sequence;
            Scanner in = new Scanner(System.in);
            // Read the Input
            range = in.nextLine();
            blocked = in.nextLine();
            sequence = in.nextLine();

            String blk[] = blocked.split(" ");
            String seq[] = sequence.split(" ");
    //Put the blocked channels into a hash-map
            HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();

            for (int j = 1; j < blk.length ; j++) {
                hm.put(Integer.parseInt(blk[j]), 1);
            }

            String temp[] = range.split(" ");
          //  Start Range and End Range for the channels available from input string 
            int s_range = Integer.parseInt(temp[0]);
            int e_range = Integer.parseInt(temp[1]);
            temp = null;

            for (String s : blk) {
                System.out.print(s + " ");

            }
            System.out.println();
            for (String s : seq) {
                System.out.print(s + " ");
            }
            System.out.println();

                //Initialize a cost array for storing costs as we browse through the sequence
                int[] cost = new int[e_range + 1];

            cost[0] = 0;
    //Curr is the Current channel we are on, next is the channel we want to hop to and diff is the difference in number of channles in between curr and next
            int diff;
            int sum_cost = 0;
            Integer next;
               //Assuming that initially we start with the first channel in the range
                Integer curr = s_range;
            System.out.println("Start, End " + s_range + "," + e_range + " Seq.length "+seq.length);
            for (int i = 1; i < seq.length; i++) {

                    //Assign one by one the channels from the seqeunce in which we want to browse from the sequence list
                        next = Integer.parseInt(seq[i]);
                diff = next - curr;
                System.out.println("Diff = " + diff);
                int start, diff_adj = 0;
                start = curr + 1;


                   //Find out the number of blocked channels which will be auto skipped in between
                while (true) {

                                    if (start == next)
                        break;
                    if (hm.containsKey(start))
                        diff_adj++;
                    start++;

                }

                System.out.println("Diff Adj= " + diff_adj);
    //Adjust the difference with number of blocked channels skipped
                diff = diff - diff_adj;

                System.out.println("Length of current channel is "
                        + seq[i].length());
    //if the differene is greate than the length of the channel, the use the length of channel as the cost, else the diff value(i.e if from 102 to 104 it is easy to press forward button twice rather then pressing 104 which is three buttons. From 102 to 108, it is cheaper to press 108 directly as it costs three lcicks rather than one by one incrementing from 102. Account from skiped channels as you do this.)
                if (diff > seq[i].length())
                    cost[next] = seq[i].length() + cost[curr];
                else
                    cost[next] = diff + cost[curr];
                curr = next;
                System.out.println("The current cost is this channel is" + next
                        + "," + diff);

                sum_cost = cost[next];
            }

            System.out.println("Minimum cost is" + sum_cost);

        }

    }

My output is:

    2 103 108 
    3 102 106 109 
    Start, End 100,200 Seq.length 4
    Diff = 2
    Diff Adj= 0
    Length of current channel is 3
    The current cost is this channel is102,2
    Diff = 4
    Diff Adj= 1
    Length of current channel is 3
    The current cost is this channel is106,3
    Diff = 3
    Diff Adj= 1
    Length of current channel is 3
    The current cost is this channel is109,2
    Minimum cost is7

2 个答案:

答案 0 :(得分:2)

我们可以在eclipse中添加可用的插件后关注TDD。支持TDD的Eclipse插件(Plugins) 开始使用TDD进行了解释 http://www.javaworld.com/article/2073090/testing-debugging/getting-started-with-test-driven-development.html

答案 1 :(得分:1)

当你谈到TDD时,你会在实际实现它之前考虑所有场景/测试用例,然后w.r.t你编写测试用例的所有场景并让它们失败。 你一个接一个地通过给予适当的实施来使它们通过。 (Red green re-factor

要执行TDD,您需要工具和框架,找到更多信息here