JUnit测试的层次结构
我需要下面的测试JUnit测试的层次结构,junit,grouping,hierarchy,folding,Junit,Grouping,Hierarchy,Folding,我需要下面的测试 @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() } cla
@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进行分组,JUnit就会忽略除了RAM和缓存集合之外的所有单一测试方法。在JUnit设计中,似乎不能有同级文件和文件夹。中的评论还暗示
@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
)都打包到他们的单音套装中,或者将套件扁平化——如果层次结构完全被破坏,就将其清除
那么,JUnit设计成不允许将单个文件(@test-methods)和文件夹(@runwith-suites)混合使用,对吗?为什么?这是如何解决的?可能有另一种方法可以替代
@runwith.Suite
?您的设计应该是这样的:
// folder com.myco.project
SuiteX.java
TestA.java
TestB.java
// contents of TestA.java
public class TestA{
@Test
public void someTestInA(){...}
}
// contents of TestB.java
public class TestB{
@Test
public void someTestInB(){...}
}
// contents of SuiteX.java
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestA.class,
TestB.class
})
public class FeatureTestSuite {
// the class remains empty,
// used only as a holder for the above annotations
}
正如我在注释中所述,为每个测试类使用单独的java文件。不要使用内部类。您想要创建的是一个mixin类型,JUnit运行程序不支持该类型。因此,是的,你是对的,这是不可能的开箱即用 为此,我创建了一个附加组件,可用于为测试创建分层上下文。在我看来,这是JUnit中缺少的一个特性,我也会保持联系,以便将其包含到JUnit核心中 该插件提供了一个HierarchycalContextRunner,允许使用内部类将测试分组到上下文中。每个上下文可以包含测试或其他上下文。它还允许使用@Before、@After、@规则方法和字段,以及其他功能,如标准运行程序的@Ignore.:-) 例如:
@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() {...}
}
试一试:
非常感谢您的投票和反馈。:) 我不明白你在说什么WRT文件和文件夹。不能在套件中进行测试。因此,测试是在测试类中进行的,并将测试包含在套件中。只要测试是公共的,就不限制测试驻留在哪个文件夹中,以便将它们包含在套件中。您可以添加与套件位于同一文件夹(包)中的测试,也可以添加位于不同文件夹(包)中的测试。我看到您说套件中不能有测试。你能回答这个问题吗?也许可以解释一下设计,这样我就不会在这个陷阱上浪费时间。问题是所有的类都应该是它们自己的文件。不要使用子类/内部类。我不明白。如果您告诉我们在套件中不可能有测试,而套件类可以在任何地方找到,那么为什么嵌套会产生任何问题呢?依我看,在语法上嵌套逻辑嵌套的东西是件好事。要使用内部类,您可以使用
封闭的运行程序,而不是套件运行程序。使用独立类的套件
可以1:拥有较小的文件2:运行单个测试取消子集合测试如何启用FeatureTestSuite中的单元测试?在您的示例中,我看不到任何测试。运行FeatureTestSuite
时,runner会导致执行TestA
和TestB
中的测试。这就是套件
运行程序的要点。您是否理解TestA
和TestB
是执行的(但是FeatureTestSuite
中的@test方法不是),所以执行TestA
和TestB
时执行方法是一个问题?正如我所说的,FeatureTestSuite
中不应有任何测试。套件
不包含测试,它链接其他测试类。不要试图让它去做它不打算做的事情。我相信我是想让这个答案的。你的回答有些不同。其次,您可以解释为什么它是以这种方式设计的,并提出除了套件
之外的任何允许将测试集合与方法混合的东西。在大多数情况下,每个(子)套件只有一个测试。当单个方法足够时,为每个测试创建一个类和方法是愚蠢的,就是这样。但是,我看到使用它还是有一些问题,