How to recognize test classes in a project

I use the following code to recognize test classes in a project, the whole idea is to find test classes and compare the amount of test code with production code! Here is the part of my code that is responsible for finding the test class and counting strings:

     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++;
            }

But after running the code in several projects, I realized that the idea of ​​finding test classes containing Uniteither EasyMockor Mockitois not the best practice for finding test classes, since several projects use their own test methods! So the question is to better define test classes?

thank

+4
source share
4 answers

,

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

.

.

, , , . (, !)

, , .

:

 public void compareTestAndProduction () {

    // pattern to split the name of class from it 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();
    }
}

:

1

:

2

+1

java src/main/java src/test/java

0

, , . , emma, cobertura, sonar .

, , . .

.

/ , . TestCase.

, . @TestCase.

, , , . @Test.

0

So far, you and your employees are not agreeing on how to create custom test classes, that is, if you all call the test class SomeClassTest or TestSomeClass, or maybe even use static methods for testing. But while you agree, you only need to search in this example for the word "test" in the title. Or, if you use the static method, maybe a little more complicated.

0
source

All Articles