使用代码方式运行指定单元测试类及方法

记一下, 备忘
    private void runTest(String className, String... methodNames) throws Exception {
        assert StringUtils.isNotBlank(className);
        Class<?> testClass = Class.forName(className);
        Runner runner = getRunner(testClass);
        if (runner instanceof Suite) {
            SuiteClasses ann = testClass.getAnnotation(Suite.SuiteClasses.class);
            Class<?>[] testClasses = ann.value();
            for (Class<?> clazz : testClasses) {
                run(clazz, getRunner(clazz), methodNames);
            }
        } else {
            run(testClass, runner, methodNames);
        }
    }

    private Runner getRunner(Class<?> testClass) {
        Request request = Request.aClass(testClass);
        Runner runner = request.getRunner();
        return runner;
    }

    private void run(Class<?> testClass, Runner runner, String... methodNames) throws NoSuchMethodException,
            NoSuchFieldException, IllegalAccessException {
        if (runner instanceof IgnoredClassRunner)
            return;

        JUnit4ClassRunner j4Runner = (JUnit4ClassRunner) runner;
        Class<?> runnerClass = j4Runner.getClass();
        while (runnerClass != JUnit4ClassRunner.class) {
            runnerClass = runnerClass.getSuperclass();
            if (runnerClass == Object.class) {
                break;
            }
        }
        if (methodNames != null && methodNames.length != 0) {
            String fielName = "fTestMethods";
            Field fTestMethods = runnerClass.getDeclaredField(fielName);
            if (fTestMethods == null) {
                throw new RuntimeException(String.format("the runner class[%s]'s field[%s] is not existed!",
                        runnerClass,
                        fielName));
            }
            List<Method> methods = new ArrayList<Method>(methodNames.length);
            for (String methodName : methodNames) {
                if (StringUtils.isBlank(methodName))
                    continue;

                Method testMethod = testClass.getDeclaredMethod(methodName);
                methods.add(testMethod);
            }
            fTestMethods.setAccessible(true);
            fTestMethods.set(j4Runner, methods);
        }
        JUnitCore core = new JUnitCore();
        core.addListener(getRunListener());
        core.run(runner);
    }

    private RunListener getRunListener() {
        return new RunListener() {
            @Override
            public void testRunStarted(Description description) throws Exception {
                logger.debug("开始测试类" + description.getDisplayName());
            }

            @Override
            public void testRunFinished(Result result) throws Exception {
                logger.debug("测试结束" + "\n");
            }

            @Override
            public void testStarted(Description description) throws Exception {
                logger.debug("\t开始测试方法:" + description.getDisplayName());
            }

            @Override
            public void testFinished(Description description) throws Exception {
                logger.debug("\t结束测试方法");
            }

            @Override
            public void testFailure(Failure failure) throws Exception {
                Throwable exception = failure.getException();
                logger.error("", exception);
            }
        };
    }

你可能感兴趣的:(单元测试)