JUnit测试的层次结构 [英] Hierarchy of JUnit tests

查看:97
本文介绍了JUnit测试的层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下测试

@runwith(cache, memory)
class CollectionA is -- this is a suite (aka folder)
  class Cache {   -- this is a sub-suite (aka folder)
    @test testCache1()  -- this is a method (aka file)
    @test testCache2()
    @test testCache3()
  }
  class RAM {  -- this is a sub-suite (aka folder)
    @test testRAM1()
    @test testRAM2()
  }
  @test testIO()
  @test testKeyboard()
  @test testMouse()
  @test testMonitor()
  @test testPower()
  @test testBoot()

请注意,只需要对缓存和RAM进行分组.层次结构有助于抵御复杂性并运行相关测试,例如必要时,单独使用高速缓存子系统.我立即使用@runwith进行分组的问题是,除RAM和Cache集合外,所有单个测试方法都被JUnit忽略.似乎在JUnit设计中不能有同级文件和文件夹. 分组的官方示例中的评论也暗示了

Please note that only Cache and RAM need to be grouped. The hierarchy helps to fight the complexity and run related tests, e.g. Cache subsystem, alone, when necessary. The problem that is as soon I use @runwith to do that grouping, all the single test methods besides the RAM and Cache collections are ignored by JUnit. It seems that you cannot have sibling files and folders in JUnit design. The comments in the official example of grouping also hints that

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestA.class,
  TestA.class
})

public class FeatureTestSuite {
  // the class remains empty,
  // used only as a holder for the above annotations
  // HEY!!! WHAT ABOUT MY @Tests HERE?
}

答案表明我是否需要包装每个测试,例如testPower放入他们的单调西装或将套件弄平-如果层次结构完全消失,则摆脱它.

The answers say that whether I need to wrap every single test, e.g. testPower into their singletone suit or flatten the suite - get rid if the hierarchy completely.

那么,JUnit设计为禁止将单个文件(@test方法)与文件夹(@runwith suites)混合在一起是对的吗?为什么?如何解决呢?可能是@runwith.Suite的替代方法吗?

So, is it right that JUnit is designed to disallow mixing single files (@test methods) with the folders (@runwith suites)? Why? How can this be worked around? Might be there is an alternative to @runwith.Suite?

推荐答案

您要创建的是mixin类型,JUnit运行器不支持该类型.因此,是的,您是对的,不可能开箱即用.

What you like to create is a mixin type, which is not supported by the JUnit runner. So yes, you are right, it is not possible out of the box.

为此,我创建了一个插件,可用于为您的测试创建层次结构上下文.在我看来,这是JUnit中缺少的功能,我也一直保持联系以将其包含在JUnit核心中.

For this purpose I created an add-on that can be used to create hierarchical contexts for your test. In my point of view this is a missing feature in JUnit and I also stay in contact to get this included into the JUnit core.

该附件提供一个HierarchicalContextRunner,它允许使用内部类将测试分组为上下文.每个上下文可以包含测试或其他上下文.它还允许具有@ Before,@ After,@ Rule方法和字段,以及其他功能,例如标准Runner的@Ignore. :-)

The add-on provides an HierarchicalContextRunner which allows to use inner class to group your tests into contexts. Each context can contain tests or other contexts. It also allows to have @Before, @After, @Rule methods and fields, as well as other feature like @Ignore of the standard Runner. :-)

示例:

@RunWith(HierarchicalContextRunner.class)
public class CollectionA {
    public class Cache {
        @Test testCache1() {...}
        @Test testCache2() {...}
        @Test testCache3() {...}
    }
    public class RAM {
        @Test testRAM1() {...}
        @Test testRAM2() {...}
    }
    @Test testIO() {...}
    @Test testKeyboard() {...}
    @Test Mouse() {...}
    @Test testMonitor() {...}
    @Test testPower() {...}
    @Test testBoot() {...}
}

尝试一下: https://github.com/bechte/junit-hierarchicalcontextrunner/wiki

高度赞赏投票和反馈. :)

Votes and feedback are highly appreciated. :)

这篇关于JUnit测试的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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