zoukankan      html  css  js  c++  java
  • (第四周)词频统计单元测试

    单元测试

      单元测试是编写测试代码,用来检测特定的、明确的、细颗粒的功能。单元测试并不一定保证程序功能是正确的,更不能保证整体业务是准备的。不仅仅用来保证当前代码的正确性,更重要的是用来保证代码修复、改进或重构之后的正确性。

    推荐

      虽说自己单元测试没有理解好,但仍推荐给大家两个比较好的(个人感觉)有关java单元测试的网站。

      1、Java单元测试(Junit+Mock+代码测试率),可以借鉴学习一下。

      网址:http://blog.csdn.net/alonesword/article/details/41291507#t7

      2、Eclipse中Junit4进行单元测试的详细步骤,很容易看懂。

      网址:http://tech.sina.com.cn/s/2010-01-18/14081218926.shtml

    词频统计单元测试

      有关详细的功能代码参见之前的随笔:http://www.cnblogs.com/huangxman/p/5869779.html

      对词频统计的输入文件进行词频统计(Article类中)的各个功能进行测试,并创建ArticleTest类,用来写各个方法的测试用例。

      因为写测试用例,Article类中增加的代码如下:

        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        public String[] getRawWords() {
            return rawWords;
        }
    
        public void setRawWords(String[] rawWords) {
            this.rawWords = rawWords;
        }
    
        public int getTotal() {
            return total;
        }
    
        public void setTotal(int total) {
            this.total = total;
        }

      1、ArticleTest中测试读取输入文件的Article()方法。

      输入:a bb ccc,期望:将输入的读取出来。

        @Test
        public void testArticle() {        
            Article at = new Article();
            String expected = "a bb ccc";
            assertEquals(expected,at.getContent());
            
        }    

      2、ArticleTest中测试分词的splitWord()方法。

      将输入的文件进行分词。期望:输出和输入的一样。

        @Test
        public void testSplitWord() {
            Article b1 = new Article();
            String[] expected={"a","bb","ccc"};
            b1.splitWord();
            assertArrayEquals(expected,b1.getRawWords());
        }

      3、ArticleTest中测试统计词、便利数组的sort()方法。

        @Test
        public void testSort() {
            List<String> expected = new ArrayList<String>();
            expected.add("aa");
            expected.add("bbb");
            expected.add("cccc");
            
            Article b1 = new Article();
            assertEquals(expected, b1.sort());
        }   

    4、ArticleTest中测试根据词频进行降序排序的countWordFreq()方法。

      期望的词频输出顺序应和方法调用后输出的顺序相同。

        @Test
        public void testCountWordFreq() {
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("aa", 1);
            map.put("bb", 1);
            map.put("ccc", 1);
            List<Entry<String, Integer>> expected = new ArrayList<Entry<String, Integer>>();
            for(Map.Entry<String, Integer> entry : map.entrySet()){
                expected.add(entry);
            }
            Article b1 = new Article();
            assertEquals(expected, b1.countWordFreq());
        }

    测试结果:

      最后一个没有通过,原因是期望的顺序和调用方法后的顺序不同。其它的方法均通过测试。

      感受:通过学习,查找资料,请教别人,对单元测试用了一些了解。知道测试用例就是对类中的方法进行测试。期望和调用方法的结果是一致的就成功,不一致就失败。但对与测试代码的编写还是有问题,还得继续学习。

    HTTP:https://git.coding.net/yanzouzhe/ywcptj.git

    SSH:git@git.coding.net:yanzouzhe/ywcptj.git

      PSP 

    项目:词频统计单元测试

    项目改进:已改进

    项目时间:2016.9.25—2016.9.27

    C内容 S开始时间 ST结束时间 I中断时间 T净时间
    查资料:Junit的使用方法,单元测试的方法 14:00 21:00 3:30 210
    写测试用例并调试 10:00 15:00 2:30 150
    写随笔 20:30 22:30 1:00 60
  • 相关阅读:
    Jupyter-notebook安装问题及解决
    [模块] scrapy_splash(迁移)
    pychram-redis破解
    scrapy-redis(迁移)
    123
    day44作业
    sql 的基本数据类型
    基本数据库操作
    安装数据库与配置使用环境
    线程知识点——Event事件
  • 原文地址:https://www.cnblogs.com/huangxman/p/5918210.html
Copyright © 2011-2022 走看看