zoukankan      html  css  js  c++  java
  • 实验二:单元测试

    实验目的

        (1) 用JUnit编写单元测试;
        (2) 学习代码覆盖率和性能监测工具的使用;

    实验内容

         1、 学习单元测试和代码覆盖率工具的使用

    (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
    (2)编写单元测试进行测试;
    (3)用ElcEmma查看代码覆盖率,要求覆盖达到100%。

    (1)

    package cn.chen;

    public class Action {

       public void findWord(String str){//划分单词

            String[] arrayWord =str.split(" ");

            Action a = new Action();

            a.frequency(arrayWord);

    }

       public void frequency(String[] arrayWord){

            String[] word = new String[arrayWord.length];

            int time[]=new int[arrayWord.length];

            boolean t = true;

            for(int i=0;i<arrayWord.length;i++){

                for(int j=0;j<arrayWord.length;j++){          

           if(arrayWord[i].equals(word[j])){

                        System.out.println("单词重复!!!");

                        t=false;

                    }

                }

                if(t==true){

                    word[i]=arrayWord[i];

                    time[i]=1;

                    for(int j=i+1;j<arrayWord.length;j++){             

            if(arrayWord[i].equals(arrayWord[j])){

                            time[i]++;

                        }

                    }

                }

            }

            for(int i = 0;i<arrayWord.length;i++){

                if(word[i]!=null){

                    System.out.println("单词:"+word[i]+"出现了"+time[i]+"次。");

                }

            }

    }

    }

     

    package cn.chen;

    public classMain {

        /**

         * @param args

         */

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            View v = new View();

        }

    }

     

    package cn.chen;

    import java.util.Scanner;

    public class View {

       public View(){

            Scanner input =new Scanner(System.in);

            System.out.println("请输入字符串:");

            String str=input.nextLine();

            Action a = new Action();

            a.findWord(str);

        }

    }

     (2)

    package cn.chen;

    import static org.junit.Assert.*;

    import org.junit.Before;

    import org.junit.Test;

    public class ActionTest {

       @Before

       public void setUp() throws Exception {

       }

       @Test

       public void test() {

          String str="Hello World My First Unit Test";

          Action action =new Action();

          action.findWord(str);

       }

    }

    2、 学习单元测试代码覆盖率工具的使用

    (1)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;

    (2)编写单元测试进行测试;

    (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。

    (1)

    import java.util.Scanner;

    public class test2{

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("请输入英文:");

    String str = input.nextLine();

    String[] strArr = str.split("\s+|[,]");

    StringBuffer result = new StringBuffer();

    for(int i = strArr.length -1;i >=0; i--){

    result.append(strArr[i] + " ");

    }

    result.setCharAt(str.length()-0, (char) 0);

    System.out.println("颠倒顺序后的结果为:"+result.toString());

    }

    }

    (2)

    package Test1;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    public class test2Test {
    
        @Before
        public void setUp() throws Exception {
        }
    
        @After
        public void tearDown() throws Exception {
        }
    
        @Test
        public void testTest() throws Exception  {
            String str="how are you";
            test2.test(str);
            }
        }
  • 相关阅读:
    运营设计方法论
    使用 typescript ,提升 vue 项目的开发体验(2)
    PAT 1078. 字符串压缩与解压
    PAT 1077. 互评成绩计算
    PAT 1076. Wifi密码
    PAT 1075. 链表元素分类
    PAT 1074. 宇宙无敌加法器
    PAT 1073. 多选题常见计分法
    PAT 1072. 开学寄语
    PAT 1071. 小赌怡情
  • 原文地址:https://www.cnblogs.com/kugua38/p/5368126.html
Copyright © 2011-2022 走看看