gpt4 book ai didi

junit - JUnit 测试的层次结构

转载 作者:行者123 更新时间:2023-12-02 21:49:40 27 4
gpt4 key购买 nike

我需要以下测试

@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()

请注意,只有缓存和内存需要分组。层次结构有助于对抗复杂性并运行相关测试,例如必要时单独缓存子系统。问题是,一旦我使用 @runwith 进行分组,JUnit 就会忽略 RAM 和 Cache 集合之外的所有单一测试方法。在 JUnit 设计中似乎不能有同级文件和文件夹。 the official example of grouping中的评论也暗示

@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 方法)与文件夹(@runwith 套件)混合使用是否正确?为什么?如何解决这个问题?可能有 @runwith.Suite 的替代方案?

最佳答案

你想创建的是mixin类型,JUnit runner不支持。所以是的,你是对的,开箱即用是不可能的。

为此,我创建了一个附加组件,可用于为您的测试创建分层上下文。在我看来,这是 JUnit 中缺少的功能,我也保持联系以将其包含到 JUnit 核心中。

该附加组件提供了一个 HierarchicalContextRunner,它允许使用内部类将您的测试分组到上下文中。每个上下文都可以包含测试或其他上下文。它还允许拥有@Before、@After、@Rule 方法和字段,以及其他功能,如标准 Runner 的 @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() {...}
}

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

非常感谢投票和反馈。 :)

关于junit - JUnit 测试的层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18894951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com