美文网首页
UnitTest Tips:固定用例顺序

UnitTest Tips:固定用例顺序

作者: 罗力 | 来源:发表于2019-07-22 20:02 被阅读0次

    JUnit默认不会控制用例的执行顺序,执行顺序由运行时查找到的用例列表的顺序决定。

    如果有我们有一系列的用例,同时希望这些用例以一定的顺序执行,可以在类上添加注解:

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    

    使JUnit以字母序列的方式执行用例。接着便是以一定的顺序,命名用例即可。

    其他可选值

    /**
     * Sort the methods into a specified execution order.
     * Defines common {@link MethodSorter} implementations.
     *
     * @since 4.11
     */
    public enum MethodSorters {
        /**
         * Sorts the test methods by the method name, in lexicographic order,
         * with {@link Method#toString()} used as a tiebreaker
         */
        NAME_ASCENDING(MethodSorter.NAME_ASCENDING),
    
        /**
         * Leaves the test methods in the order returned by the JVM.
         * Note that the order from the JVM may vary from run to run
         */
        JVM(null),
    
        /**
         * Sorts the test methods in a deterministic, but not predictable, order
         */
        DEFAULT(MethodSorter.DEFAULT);
    
        private final Comparator<Method> comparator;
    
        private MethodSorters(Comparator<Method> comparator) {
            this.comparator = comparator;
        }
    
        public Comparator<Method> getComparator() {
            return comparator;
        }
    }
    

    相关文章

      网友评论

          本文标题:UnitTest Tips:固定用例顺序

          本文链接:https://www.haomeiwen.com/subject/oglglctx.html