Junit:如何进行条件测试? [英] Junit : how to make conditional tests?

查看:258
本文介绍了Junit:如何进行条件测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JUnit编写条件测试.我需要测试对象的持久性,如果这些测试通过,请通过HTTP方法测试访问(以开发REST Web服务).

I'm trying to write conditional tests with JUnit. I need to test the persistence of objects and if those tests pass, test access via HTTP methods (in order to develop a REST Web Service).

此刻,我的解决方案如下:

For this moment my solution looks like this :

public class ApplicationTest {

    @Test
    public void testSuite() {
        final Request requestStatutTest = Request.method(this.getClass(), "insertStatutTest");
        final Result resStatutTest = new JUnitCore().run(requestStatutTest);
        if (resStatutTest.wasSuccessful()) {
            postStatutTest();
            getStatutTest();
            putStatutTest();
            deleteStatutTest();

    }

    public void insertStatutTest() {
    }

    public void postStatutTest() {
    }

    // etc...

}

这是一个好的解决方案吗?

Is it the good solution ?

推荐答案

您可以使用org.junit.Assume:

 @Before
 public void checkAssumptions() {
     org.junit.Assume.assumeTrue(someCondition());
     // or import static org.junit.Assume.* and then just call assumeTrue()
 }

如果条件为假,则将导致测试以违反假设的方式结束.正如文档所说:

If the condition is false, then this will result in the test ending with a violated assumption. As the docs say:

失败的假设并不意味着代码已损坏,但是测试没有提供有用的信息.

A failed assumption does not mean the code is broken, but that the test provides no useful information.

与断言失败(即测试失败)相比,这是一个较弱的条件,并且默认的JUnit运行器会将此测试视为已忽略.听起来完全像是您要寻找的东西-如果持久性有效,请运行测试,否则将其忽略.

This is a weaker condition than a failed assertion (i.e. a test failure), and the default JUnit runner will treat this test as ignored. Which sounds like exactly what you are looking for - run the test if the persistence works, otherwise ignore it.

这篇关于Junit:如何进行条件测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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