我的设置:- Android手机和平板电脑版本的应用程序-我正在使用Android Espresso for UI-Tests (现在仅适用于手机版本,手机处于buildagent)
我想做的是:-现在我希望Espresso区分手机和平板电脑的测试-因此测试A应该只由平板电脑执行,测试B应该只由手机执行,测试C两者都应该执行-测试应该可以通过gradle任务执行
发布于 2015-05-16 09:36:09
三个选项,均可通过gradlew connectedAndroidTest或自定义gradle任务执行:
1.使用org.junit.Assume
来自Assumptions with assume - junit-team/junit Wiki - Github
默认的JUnit运行器
会将假设失败的测试视为被忽略。自定义runners可能会有不同的行为。
不幸的是,android.support.test.runner.AndroidJUnit4 (com.android.support.test:runner:0.2)运行者将失败的假设视为失败的测试。
修复此行为后,将执行以下操作(有关isScreenSw600dp()源代码,请参阅下面的选项3):
仅限Phone :类中的所有测试方法
@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;
}
}发布于 2018-10-31 02:56:49
我知道这个问题有点老生常谈,但我认为这是一种更简单的方式。
所以只需为目标屏幕大小设置一个布尔值-
用于平板电脑的ex 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);https://stackoverflow.com/questions/26231752
复制相似问题