AndroidJUnit4.class + org.junit.Assume.assumeTrue = AssumptionViolatedException [英] AndroidJUnit4.class + org.junit.Assume.assumeTrue = AssumptionViolatedException

查看:300
本文介绍了AndroidJUnit4.class + org.junit.Assume.assumeTrue = AssumptionViolatedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法让我的Android项目转换到JUnit4,当然我想要做的主要原因是没有用。如果有人在这里有想法,会喜欢任何帮助。



我试图解决的问题是,如果构建未指向登台服务器,我想自动跳过某些测试。我已经设置了一个 BUILD_TYPE ,它使用gradle来注入基本URL。



我设置了我的设置中的 assumeThat 子句正确识别构建何时不进行分段,但不是暂停和忽略其余的测试,而是抛出异常并失败。 / p>

这是我的实时API测试的基类 - 我用 @RunWith(AndroidJUnit4.class),所以理论上应该始终使用JUnit4运行器运行:

  package com。[my package] .nonuitests.liveservertests; 

import android.support.test.runner.AndroidJUnit4;
import com。[my package] .nonuitests.BaseAndroidTest;
import org.junit.Test;
import org.junit.runner.RunWith;

/ **
*针对实时API进行测试。如果BUILD_TYPE没有暂存,那么从这个类下降的所有测试将被忽略
*。
* /
@RunWith(AndroidJUnit4.class)
公共类BaseLiveServerTests扩展BaseAndroidTest {

private static final String STAGE =staging;

/ ******************
* SETUP / TEARDOWN *
********** ******** /

@Override
public void setUp()throws Exception {
super.setUp();

// TODO:你不行吗?!
//这会导致测试的其余部分在失败时跳过,
//而是抛出AssumptionViolatedException。
assumeTrue(STAGE.equals(BuildConfig.BUILD_TYPE));
}
}

所以,我的问题:


  1. 有更好的方法吗?从理论上讲,这可以通过口味来完成,但我之前尝试过,它使其他一切方式更复杂。

  2. 我的研究表明有某种东西谷歌没有在他们的跑步者中实施导致这种爆炸,但我有一段时间弄清楚我能做什么/应该做些什么来解决这个问题。我应该继承的任何建议,以使其按预期工作?

我们非常感谢任何其他想法。谢谢!



编辑(2015年1月26日上午11:40 CST):每 Grzesuav的建议,我将刺伤作为一个实施 @Rule ,但目前仍无法正常工作。这似乎是一条很有希望的道路,但现在还不行。



编辑2(2015年1月26日下午12:15) ):好的,现在正在运行

解决方案

https:// github。 com / junit-team / junit / wiki /假设假设



ad 2)自定义运行者可以不同地处理假设语句。要修复它,你应该编写自己的Android版本,并实现一种处理假设作为本机JUnit运行程序的方式,或者为Android测试运行器制作一个bug。



ad 1 )建议我:尝试使用JUnit规则:



http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditional-ignore-tests/
http://cwd.dhemery.com/2010/12/junit -rules /


I've managed to get my Android project transitioned over to JUnit4, and of course the main reason I wanted to do it isn't working. Would love any help if anyone's got ideas here.

The problem I'm trying to solve is that I want to automatically skip certain tests if the build is not pointed at the staging server. I've got this set up with a BUILD_TYPE which is using gradle to inject the base URL.

I set up an assumeThat clause in my setup which correctly identifies when the build is not staging, but instead of halting and ignoring the rest of the test, it throws an exception and fails.

Here's my base class for my live API tests - I've annotated descending from this with @RunWith(AndroidJUnit4.class), so in theory this should always be run with the JUnit4 runner:

package com.[my package].nonuitests.liveservertests;

import android.support.test.runner.AndroidJUnit4;
import com.[my package].nonuitests.BaseAndroidTest;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests against the live API. All tests descending from this class will
 * be ignored if the BUILD_TYPE is not staging.
 */
@RunWith(AndroidJUnit4.class)
public class BaseLiveServerTests extends BaseAndroidTest {

    private static final String STAGE = "staging";

    /******************
     * SETUP/TEARDOWN *
     ******************/

    @Override
    public void setUp() throws Exception {
        super.setUp();

        //TODO: Y U NO WORK?!
        //This should cause the rest of the test to be skipped if it fails,
        //but is instead throwing an AssumptionViolatedException. 
        assumeTrue(STAGE.equals(BuildConfig.BUILD_TYPE));
    }
}

So, my questions:

  1. Is there a better way to do this? In theory this could be done with flavors, but I was trying that earlier and it made everything else way more complicated.
  2. My research indicates there's some kind of thing that Google's not implementing in their runner that's causing this to bomb out, but I'm having a hell of a time figuring out what I can/should do to fix this. Any suggestions of things I should subclass to get this to work as expected?

Any other thoughts would be most appreciated. Thanks!

Edit (1/26/15 11:40am CST): Per Grzesuav's suggestion, I took a stab at implementing this as an @Rule, but at the moment it's still not working. This seems a promising path, but it ain't working at the moment.

Edit 2 (1/26/15 12:15pm CST): OK, now it's working.

解决方案

https://github.com/junit-team/junit/wiki/Assumptions-with-assume

ad 2) Custom runners could differently treat assume statement. To fix it you should write own version of Android runner and implement a way of dealing with assumes as native JUnit runner does or make a bug for android test runner.

ad 1) Suggested by me : try use JUnit Rules :

http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/ http://cwd.dhemery.com/2010/12/junit-rules/

这篇关于AndroidJUnit4.class + org.junit.Assume.assumeTrue = AssumptionViolatedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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