gpt4 book ai didi

适用于手机和平板电脑的 Android Espresso 测试

转载 作者:IT老高 更新时间:2023-10-28 23:11:31 29 4
gpt4 key购买 nike

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

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

最佳答案

三个选项,都可以通过gradlew connectedAndroidTest或者自定义的gradle任务来执行:

1。使用 org.junit.Assume

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

The default JUnit runner treats tests with failing assumptions as ignored. Custom runners may behave differently.

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

一旦此行为得到修复,以下将起作用(请参阅下面的选项 3 了解 isScreenSw600dp() 源代码):

仅限手机:类中的所有测试方法

    @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 :

This led us to creating a ConditionalIgnore annotation and a corresponding rule to hook it into the JUnit runtime. The thing is simple and best explained with an example:

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;
}
}

关于适用于手机和平板电脑的 Android Espresso 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26231752/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com