Android Espresso Tests适用于手机和平板电脑

时间:2014-10-07 08:30:44

标签: android android-espresso

我的设置: - 带手机和平板电脑版的Android应用程序 - 我正在使用Android Espresso进行UI测试(现在仅用于手机版,在buildagent上使用手机)

我想做什么: - 现在我希望Espresso区分手机和平板电脑的测试 - 因此,测试A应仅由平板电脑执行,测试B应仅由手机和测试C执行 - 测试应该可以通过gradle任务执行

2 个答案:

答案 0 :(得分:18)

三个选项,所有这些选项都可通过gradlew connectedAndroidTest或自定义gradle任务执行:

1。使用org.junit.Assume

来自Assumptions with assume - junit-team/junit Wiki - Github

  

默认的JUnit运行器将处于失败假设的测试视为忽略。自定义跑步者可能表现不同。

不幸的是,android.support.test.runner.AndroidJUnit4com.android.support.test:runner:0.2)转轮将失败的假设视为失败的测试。

修复此问题后,以下操作会起作用(请参阅下面的isScreenSw600dp()来源选项3):

仅限电话:课程中的所有测试方法

    @Before
    public void setUp() throws Exception {
        assumeTrue(!isScreenSw600dp());
        // other setup
    }

特定测试方法

    @Test
    public void testA() {
        assumeTrue(!isScreenSw600dp());
        // test for phone only
    }

    @Test
    public void testB() {
        assumeTrue(isScreenSw600dp());
        // test for tablet only
    }

2。使用自定义JUnit规则

来自A JUnit Rule to Conditionally Ignore Tests

  

这使我们创建了一个 ConditionalIgnore 注释和一个相应的规则来将其挂钩到JUnit运行时。事情很简单,最好用一个例子来解释:

public class SomeTest {
  @Rule
  public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();

  @Test
  @ConditionalIgnore( condition = NotRunningOnWindows.class )
  public void testFocus() {
    // ...
  }
}

public class NotRunningOnWindows implements IgnoreCondition {
  public boolean isSatisfied() {
    return !System.getProperty( "os.name" ).startsWith( "Windows" );
  }
}

ConditionalIgnoreRule代码在这里:JUnit rule to conditionally ignore test cases

可以轻松修改此方法,以实现下面选项3中的isScreenSw600dp()方法。

3。在测试方法中使用条件

这是最不优雅的选项,特别是因为完全跳过的测试将被报告为已通过,但它很容易实现。这是一个完整的样本测试课程,可以帮助您入门:

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.util.DisplayMetrics;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class DeleteMeTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private MainActivity mActivity;
    private boolean mIsScreenSw600dp;

    public DeleteMeTest() {
        super(MainActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        setActivityInitialTouchMode(false);
        mActivity = this.getActivity();
        mIsScreenSw600dp = isScreenSw600dp();
    }

    @After
    public void tearDown() throws Exception {
        mActivity.finish();
    }

    @Test
    public void testPreconditions() {
        onView(withId(R.id.your_view_here))
                .check(matches(isDisplayed()));
    }

    @Test
    public void testA() {
        if (!mIsScreenSw600dp) {
            // test for phone only
        }
    }

    @Test
    public void testB() {
        if (mIsScreenSw600dp) {
            // test for tablet only
        }
    }

    @Test
    public void testC() {
        if (mIsScreenSw600dp) {
            // test for tablet only
        } else {
            // test for phone only
        }

        // test for both phone and tablet
    }

    private boolean isScreenSw600dp() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        float widthDp = displayMetrics.widthPixels / displayMetrics.density;
        float heightDp = displayMetrics.heightPixels / displayMetrics.density;
        float screenSw = Math.min(widthDp, heightDp);
        return screenSw >= 600;
    }
}

答案 1 :(得分:1)

我知道这个问题有点老了,但可以考虑发布,因为这是一种更简单的方法。
因此,只需为目标屏幕尺寸输入一个布尔值-
例如values-sw600dp.xml用于平板电脑,values.xml用于电话。 在两者中都放一个布尔值
例如values.xml-

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="tablet">false</bool>
</resources>

values-sw600.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="tablet">true</bool>
</resources>

然后在Test类中使用它来获取资源值-

Context targetContext = InstrumentationRegistry.getTargetContext();
targetContext.getResources().getBoolean(R.bool.tablet);
Boolean isTabletUsed = targetContext.getResources().getBoolean(R.bool.tablet);
相关问题