繁体   English   中英

计算测试类中的行数

[英]Counting number of lines in test classes

我正在开发一种工具,该工具可以识别测试类并计算这些类中的行数。 该工具还将计算业务代码的行数并比较两个结果,这是我的代码:

    for (File f : list) {
        if (f.isDirectory()) {
            walk(f.getAbsolutePath());
        }

        if (f.getName().endsWith(".java")) {

            System.out.println("File:" + f.getName());
            countFiles++;

            Scanner testScanner = new Scanner(f);
            while (testScanner.hasNextLine()) {

                String test = testScanner.nextLine();
                if (test.contains("org.junit") || test.contains("org.mockito")) {
                    System.out.println("this is a test class");
                    testCounter++;

                    break;
                }
            }
            Scanner sc2 = new Scanner(f);

            while (sc2.hasNextLine()) {

                count++;

使用(count ++)计数业务代码可以正常工作,但是使用(testCounter ++)计数测试类中的代码数量无效,则返回的是测试类的数量,而不是这些类中的行数! 我能做什么 ?

谢谢

当遇到软件包字符串“ org.junit”或“ org.mockito”时,仅递增testCounter。 一旦找到,就可以递增并跳出循环。

当然,您不希望代码中的每一行都包含那些字符串。

假设您要计算包含org.junit or org.mockito的行数

那你想做

       while (testScanner.hasNextLine()) {

            String test = testScanner.nextLine();
            if (test.contains("org.junit") || test.contains("org.mockito")) 
            {
                hasTestLines = true;
            }
            count++;
        }

        if (hasTestLines) {
             System.out.println(String.format ("there were %d lines in file %s which also had org.junit||org.mockito", 
                                    count, f.getName());
        }
        else {
             System.out.println(String.format ("there were %d lines in file %s which did NOT have org.junit||org.mockito", 
                                    count, f.getName());
       }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM