JUnit4 中的假设True 是错误的,而不是忽略测试
我正在使用 JUnit 4 (junit-4.8.2.jar) 在我的 Java 项目上运行测试,并且在使用 ShouldTrue 时遇到问题。我有这个基本设置:
public class ClientTest extends TestCase {
@BeforeClass
public void setUp() {
assumeTrue(isPaidAccount());
}
@Test
public void testTheThings() {
// run this test if the user has a paid account
}
}
isPaidAccount() 方法返回 false,这是预期的,但类中的测试不是被忽略,而是全部返回为错误。这发生在 Eclipse 中的 JUnit4 Test Runner 以及 Maven 中。我还尝试将假设True 移动到 @Before 方法中,如下所示:
public class ClientTest extends TestCase {
@BeforeClass
public void setUp() {
// set up
}
@Before
public void mustHavePaidAccount() {
assumeTrue(isPaidAccount());
}
@Test
public void testTheThings() {
// run this test if the user has a paid account
}
}
但是,当我这样做时,它完全忽略 @Before 方法,并且只是运行测试。同样,我在 Eclipse Test Runner 和 Maven 中都得到了相同的行为。我确信我正在做一些明显不正确的事情,但我不确定它是什么。
I'm using JUnit 4 (junit-4.8.2.jar) to run tests on my Java project and am running into problems when using assumeTrue. I have this basic set up:
public class ClientTest extends TestCase {
@BeforeClass
public void setUp() {
assumeTrue(isPaidAccount());
}
@Test
public void testTheThings() {
// run this test if the user has a paid account
}
}
The method isPaidAccount() is coming back false, which is expected, but instead of the tests in the class being ignored, they're all coming back as errors. This is happening in Eclipse with the JUnit4 Test Runner as well as with maven. I have also tried moving the assumeTrue into a @Before method like so:
public class ClientTest extends TestCase {
@BeforeClass
public void setUp() {
// set up
}
@Before
public void mustHavePaidAccount() {
assumeTrue(isPaidAccount());
}
@Test
public void testTheThings() {
// run this test if the user has a paid account
}
}
However, when I do this it completely ignores the @Before method and just runs the tests anyway. Again, I'm getting the same behavior with both the Eclipse Test Runner and the maven one. I'm sure I'm doing something obviously incorrect, but I'm not sure what it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扩展
TestCase
适用于 JUnit 3.x,使用注释适用于 JUnit 4.x。如果您删除extends TestCase
它应该可以工作。Extending
TestCase
is for JUnit 3.x and using annotations is JUnit 4.x. If you drop theextends TestCase
it should work.