java – 如何识别项目中的测试类

我正在使用以下代码来识别项目中的测试类,整个想法是找到测试类并将测试代码的数量与生产代码进行比较!这是我的一段代码,它负责查找测试类并计算行数:

     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") || test.contains("org.easymock")) {
                    hasTestLines = true;
                    //      break;
                }
                testCounter++;
            }

但是在几个项目上运行代码后,我意识到找到包含Unit或EasyMock或Mockito的测试类的想法并不是查找测试类的最佳实践,因为有几个项目使用自己的测试方法!那么问题是有一个比我更好的方法来定义测试类吗?

谢谢

最佳答案 不能你只是
load classes并将它们作为参数传递

像一个试验跑步者

org.junit.runner.JUnitCore.runClasses(TestClass1.class, …);

并使用测试运行器的输出.

现在,测试运行器在类中搜索测试方法.

如果该类不包含任何内容,则不会成功,因此也是如此
一个生产类. (假设所有测试类都成功!)

在我的实现中,我只计算了类的数量,但是你
可以扩展它来计算行数,然后比较它们.

这里执行:

 public void compareTestAndProduction () {

    // pattern to split the name of class from it's extension
    String pattern = "(.*)(?=.class)";

    // package to proove
    String packageName = "stackoverflow.test";

    // relative path of package
    String packagePath = "stackoverflow/test";

    // counter for number of test classes
    int testCounter = 0;

   // counter for number of production classes
    int codeCounter = 0;

    // classloader for test and production classes
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    try {
        // load package resources to file
        Enumeration enumeration = classLoader.getResources(packagePath);
        URL url = (URL) enumeration.nextElement();
        File classFiles = new File(url.getFile());

        // read all subfiles in File
        // which contains the package dir and all classes
        for (File classFile : classFiles.listFiles()) {
            String classNameWithExtension = classFile.getName();
            // proov if name of class is no directory
            if (classNameWithExtension.endsWith(".class")) {
                // extend the class with the package name
                // and get rid of the extension .class
                String className = packageName + "." + classNameWithExtension.split("[.]")[0];

                // load class
                Class c = classLoader.loadClass(className);

                // run the class with a test runnner
                // which will search class for test methods
                Result result = org.junit.runner.JUnitCore.runClasses(c.newInstance().getClass());

                // if testmethods found
                // and they are successful
                // raise testcounter
                if(result.wasSuccessful())
                    testCounter++;
                else codeCounter++;


            }
        }
        System.out.println("Test classes:\n" + testCounter);
        System.out.println("Production classes:\n" + codeCounter);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

就我而言

Test classes:

1

Production classes:

2

点赞