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

tru*_*-mt 18 android android-espresso

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

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

unr*_*gnu 18

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

1.使用 org.junit.Assume

假设与假设 - junit-team/junit Wiki - Github:

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

不幸的是,android.support.test.runner.AndroidJUnit4(com.android.support.test:runner:0.2)运行器将失败的假设视为失败的测试.

修复此行为后,以下操作将起作用(请参阅下面的选项3以获取isScreenSw600dp()源代码):

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

    @Before
    public void setUp() throws Exception {
        assumeTrue(!isScreenSw600dp());
        // other setup
    }
Run Code Online (Sandbox Code Playgroud)

具体测试方法

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

    @Test
    public void testB() {
        assumeTrue(isScreenSw600dp());
        // test for tablet only
    }
Run Code Online (Sandbox Code Playgroud)

2.使用自定义JUnit规则

JUnit规则到有条件地忽略测试:

这导致我们创建了一个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" );
  }
}
Run Code Online (Sandbox Code Playgroud)

ConditionalIgnoreRule这里的代码:JUnit规则有条件地忽略测试用例.

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


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;
    }
}
Run Code Online (Sandbox Code Playgroud)