获取@Category 在 JUnit 的一组测试中出现的次数的计数 [英] Get a count for the number of times a @Category appears in a suite of tests in JUnit

查看:14
本文介绍了获取@Category 在 JUnit 的一组测试中出现的次数的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Java、Selenium、Junit、Maven 开发了一整套自动化测试.

I have developed a full suite of automated tests using Java, Selenium, Junit, Maven.

对于每个测试,他们有一个或多个@Category 注释,描述每个测试涵盖的软件区域.例如:

For each test, they have one or more @Category annotations describing what area of the software each test covers. For instance:

@Test
@Category({com.example.core.categories.Priority1.class,
           com.example.core.categories.Export.class,
           com.example.core.categories.MemberData.class})


@Test
@Category({com.example.core.categories.Priority1.class,
           com.example.core.categories.Import.class,
           com.example.core.categories.MemberData.class})


@Test
@Ignore
@Category({com.example.core.categories.Priority2.class,
           com.example.core.categories.Import.class,
           com.example.core.categories.MemberData.class})

我想要做的是找到一种方法来计算包含任何给定类别的测试数量.所有可能的类别都是 //com/example/core/categories 文件夹中的文件名作为源列表.

What I'm trying to do is find a way to get a count of how many tests contain any given category. All the possible categories are filenames in the //com/example/core/categories folder as a source list.

我已经尝试构建一个 shell 脚本来进行字数统计,这似乎工作正常,但我认为会有更多内置"的东西来处理 @Category.

I've tried building a shell script to do a word count, which seems to work okay, but I would think there would be something more "built-in" to deal with @Category.

我最大的问题是,即使我得到了正确的计数,一个或多个测试很可能被标记为@Ignore,这应该会使测试@Category 无效,但没有大量使用标志和读取每个文件行-逐行,以便它抛出正确的计数.

My biggest issue is that even if I get the right count, it is very possible that one or more of the tests are marked @Ignore which should nullify that tests @Category's but without heavy use of flags and reading every file line-by-line in order it throws off the correct count.

是否有一种很好的方法可以逐项列出@Ignore 中的@Category?

Is there a good way to itemize @Category's that also factors in @Ignore?

示例输出

| Category                                     | Count |
|----------------------------------------------|------:|
| com.example.core.categories.Export.class     | 1     |
| com.example.core.categories.Import.class     | 1     |
| com.example.core.categories.MemberData.class | 2     |
| com.example.core.categories.Priority1.class  | 2     |
| com.example.core.categories.Priority2.class  | 0     |
| com.example.core.categories.Priority3.class  | 0     |

推荐答案

动态按类别测试"计算机

(推荐方法)

我尝试了一种使用抽象层中的计数器来执行此操作的方法,但很痛苦,必须在每个测试方法的开头添加源代码.

I tried a way to perform this with a counter in abstract layer but it was painful, having to add source code at beginning of each Test methods.

最后,这是我为满足您的需求而编写的源代码;它很重(反射......),但它对现有源代码的干扰较小,完全满足您的需求.

At end, this is the source code I wrote to answer your needs; it is quite heavy (reflection ...), but it is the less intrusive with existing source code, and answers totally to your needs.

首先,您必须创建一个 Testsuite(包含各种其他套件,或直接包含您想要的所有测试类),以确保最后您想要统计的所有测试都已加载.

First, you must create a Testsuite (containing various others Suites, or directly all the Test classes you want), to ensure at end, that all Tests for which you want statistics, have been loaded.

在此套件中,您必须实现一个名为 @AfterClass 的最终挂钩",当整个测试套件完全由 JUnit<管理时,它将被调用一次/strong>.

In this Suite, you have to implement a "final Hook", called @AfterClass which will be called once for all, when the whole Test suite has been fully managed by JUnit.

这是我为您编写的测试套件实现:

This the the Test Suite implementation I wrote for you:

package misc.category;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.AfterClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ UnitTestWithCategory.class })
public class TestSuiteCountComputer {

    public static final String MAIN_TEST_PACKAGES = "misc.category";

    private static final Class<?>[] getClasses(final ClassLoader classLoader)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        Class<?> CL_class = classLoader.getClass();
        while (CL_class != java.lang.ClassLoader.class) {
            CL_class = CL_class.getSuperclass();
        }
        java.lang.reflect.Field ClassLoader_classes_field = CL_class.getDeclaredField("classes");
        ClassLoader_classes_field.setAccessible(true);
        Vector<?> classVector = (Vector<?>) ClassLoader_classes_field.get(classLoader);

        Class<?>[] classes = new Class[classVector.size()]; // Creates an array to avoid concurrent modification
                                                            // exception.
        return classVector.toArray(classes);
    }

    // Registers the information.
    private static final void registerTest(Map<String, AtomicInteger> testByCategoryMap, String category) {
        AtomicInteger count;
        if (testByCategoryMap.containsKey(category)) {
            count = testByCategoryMap.get(category);
        } else {
            count = new AtomicInteger(0);
            testByCategoryMap.put(category, count);
        }

        count.incrementAndGet();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        Map<String, AtomicInteger> testByCategoryMap = new HashMap<>();

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        while (classLoader != null) {
            for (Class<?> classToCheck : getClasses(classLoader)) {
                String packageName = classToCheck.getPackage() != null ? classToCheck.getPackage().getName() : "";
                if (!packageName.startsWith(MAIN_TEST_PACKAGES))
                    continue;

                // For each methods of the class.
                for (Method method : classToCheck.getDeclaredMethods()) {
                    Class<?>[] categoryClassToRegister = null;
                    boolean ignored = false;
                    for (Annotation annotation : method.getAnnotations()) {
                        if (annotation instanceof org.junit.experimental.categories.Category) {
                            categoryClassToRegister = ((org.junit.experimental.categories.Category) annotation).value();
                        } else if (annotation instanceof org.junit.Ignore) {
                            ignored = true;

                        } else {
                            // Ignore this annotation.
                            continue;
                        }
                    }

                    if (ignored) {
                        // If you want to compute count of ignored test.
                        registerTest(testByCategoryMap, "(Ignored Tests)");
                    } else if (categoryClassToRegister != null) {
                        for (Class<?> categoryClass : categoryClassToRegister) {
                            registerTest(testByCategoryMap, categoryClass.getCanonicalName());
                        }
                    }

                }

            }
            classLoader = classLoader.getParent();
        }

        System.out.println("
Final Statistics:");
        System.out.println("Count of Tests		Category");
        for (Entry<String, AtomicInteger> info : testByCategoryMap.entrySet()) {
            System.out.println("	" + info.getValue() + "		" + info.getKey());
        }

    }

}

你可以根据自己的需要,特别是我一开始创建的常量,过滤包来考虑.

You can adapt to your needs, in particular the constant I created at beginning, to filter package to consider.

那么你就没有比已经做的更多的事情了.

Then you have nothing more to do than you already do.

例如,这是我的小测试类:

For instance, this is my tiny Test Class:

package misc.category;

import org.junit.Test;
import org.junit.experimental.categories.Category;

public class UnitTestWithCategory {

    @Category({CategoryA.class, CategoryB.class})
    @Test
    public final void Test() {
        System.out.println("In Test 1");
    }

    @Category(CategoryA.class)
    @Test
    public final void Test2() {
        System.out.println("In Test 2");
    }

}

在这种情况下,输出是:

In this case, the output is:

In Test 1
In Test 2

Final Statistics:
Count of Tests      Category
    1       misc.category.CategoryB
    2       misc.category.CategoryA

使用包含 @Ignore 注释的测试用例:

And with Test case containing @Ignore annotation:

package misc.category;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

public class UnitTestWithCategory {

    @Category({CategoryA.class, CategoryB.class})
    @Test
    public final void Test() {
        System.out.println("In Test 1");
    }

    @Category(CategoryA.class)
    @Test
    public final void Test2() {
        System.out.println("In Test 2");
    }

    @Category(CategoryA.class)
    @Ignore
    @Test
    public final void Test3() {
        System.out.println("In Test 3");
    }   
}

你得到输出:

In Test 1
In Test 2

Final Statistics:
Count of Tests      Category
    1       (Ignored Tests)
    1       misc.category.CategoryB
    2       misc.category.CategoryA

如果需要,您可以轻松删除(Ignored Tests)"注册,当然还可以根据需要调整输出.

You can easily remove the "(Ignored Tests)" registration if you want, and of course adapt the output as you want.

这个最终版本的好处在于,它将处理真正加载/执行的测试类,因此您将获得已执行内容的真实统计数据,而不是像您这样的静态统计数据到此为止.

What is very nice with this final version, is that it will take care of Test Classes which have really been loaded/executed, and so you will have a real statistics of what have been executed, instead of a static statistics like you got so far.

如果您想像您要求的那样,对现有源代码不做任何处理,这是一种静态执行类别测试计算的方法.

If you want, like you asked, to have nothing to do on existing source code, this is a way to perform the Tests by Category computation statically.

这是我为你写的StaticTestWithCategoryCounter:

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

public class StaticTestWithCategoryCounter {

    public static final String ROOT_DIR_TO_SCAN = "bin";
    public static final String MAIN_TEST_PACKAGES = "misc.category";

    private static final Class<?>[] getClasses(final ClassLoader classLoader)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        Class<?> CL_class = classLoader.getClass();
        while (CL_class != java.lang.ClassLoader.class) {
            CL_class = CL_class.getSuperclass();
        }
        java.lang.reflect.Field ClassLoader_classes_field = CL_class.getDeclaredField("classes");
        ClassLoader_classes_field.setAccessible(true);
        Vector<?> classVector = (Vector<?>) ClassLoader_classes_field.get(classLoader);

        Class<?>[] classes = new Class[classVector.size()]; // Creates an array to avoid concurrent modification
                                                            // exception.
        return classVector.toArray(classes);
    }

    // Registers the information.
    private static final void registerTest(Map<String, AtomicInteger> testByCategoryMap, String category) {
        AtomicInteger count;
        if (testByCategoryMap.containsKey(category)) {
            count = testByCategoryMap.get(category);
        } else {
            count = new AtomicInteger(0);
            testByCategoryMap.put(category, count);
        }

        count.incrementAndGet();
    }


    public static void computeCategoryCounters() throws Exception {
        Map<String, AtomicInteger> testByCategoryMap = new HashMap<>();

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        while (classLoader != null) {
            for (Class<?> classToCheck : getClasses(classLoader)) {
                String packageName = classToCheck.getPackage() != null ? classToCheck.getPackage().getName() : "";
                if (!packageName.startsWith(MAIN_TEST_PACKAGES))
                    continue;

                // For each methods of the class.
                for (Method method : classToCheck.getDeclaredMethods()) {
                    Class<?>[] categoryClassToRegister = null;
                    boolean ignored = false;
                    for (Annotation annotation : method.getAnnotations()) {
                        if (annotation instanceof org.junit.experimental.categories.Category) {
                            categoryClassToRegister = ((org.junit.experimental.categories.Category) annotation).value();
                        } else if (annotation instanceof org.junit.Ignore) {
                            ignored = true;

                        } else {
                            // Ignore this annotation.
                            continue;
                        }
                    }

                    if (ignored) {
                        // If you want to compute count of ignored test.
                        registerTest(testByCategoryMap, "(Ignored Tests)");
                    } else if (categoryClassToRegister != null) {
                        for (Class<?> categoryClass : categoryClassToRegister) {
                            registerTest(testByCategoryMap, categoryClass.getCanonicalName());
                        }
                    }

                }

            }
            classLoader = classLoader.getParent();
        }

        System.out.println("
Final Statistics:");
        System.out.println("Count of Tests		Category");
        for (Entry<String, AtomicInteger> info : testByCategoryMap.entrySet()) {
            System.out.println("	" + info.getValue() + "		" + info.getKey());
        }
    }

    public static List<String> listNameOfAvailableClasses(String rootDirectory, File directory, String packageName) throws ClassNotFoundException {
        List<String> classeNameList = new ArrayList<>();

        if (!directory.exists()) {
            return classeNameList;
        }

        File[] files = directory.listFiles();
        for (File file : files) {           

            if (file.isDirectory()) {
                if (file.getName().contains("."))
                    continue;

                classeNameList.addAll(listNameOfAvailableClasses(rootDirectory, file, packageName));
            } else if (file.getName().endsWith(".class")) {
                String qualifiedName = file.getPath().substring(rootDirectory.length() + 1);
                qualifiedName = qualifiedName.substring(0, qualifiedName.length() - 6).replaceAll(File.separator, ".");

                if (packageName ==null || qualifiedName.startsWith(packageName))
                    classeNameList.add(qualifiedName);
            }
        }

        return classeNameList;
    }

    public static List<Class<?>> loadAllAvailableClasses(String rootDirectory, String packageName) throws ClassNotFoundException {
        List<String> classeNameList = listNameOfAvailableClasses(rootDirectory, new File(rootDirectory), packageName);
        List<Class<?>> classes = new ArrayList<>();

        for (final String className: classeNameList) {
            classes.add(Class.forName(className));
        }

        return classes;
    }

    public static void main(String[] args) {
        try {           
            loadAllAvailableClasses(ROOT_DIR_TO_SCAN, MAIN_TEST_PACKAGES);
            computeCategoryCounters();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

你只需要在开始时调整两个常量来指定:

You just need to adapt the two constants at beginning to specify:

  • (字节码)类在哪里
  • 您对哪个主包感兴趣(您可以将其设置为 null 以考虑 100% 可用的包)
  • where are the (bytecode) classes
  • which main package is interesting you (can you set it to null to regard 100% available packages)

这个新版本的想法:

  • 列出与您的 2 个常量匹配的所有类文件
  • 加载所有对应的类
  • 使用未修改的动态版本源代码(现在类已经加载)

如果您需要更多信息,请告诉我.

Let me know if you need further information.

这篇关于获取@Category 在 JUnit 的一组测试中出现的次数的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆