我的测试是:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class TipActivityTests {
@Rule
public ActivityTestRule<TipActivity> mActivityRule = new ActivityTestRule<>(TipActivity.class);
@Test
public void initialValues() {
onView(withId(R.id.tip_label_base_price)).check(matches(ViewMatchers.withText("45"")));
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误'with text: is "45"' doesn't match the selected view.
Expected: with text: is "45":
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "45"' doesn't match the selected view.
Expected: with text: is "45"
Got: "AppCompatTextView{id=2131689669, res-name=tip_label_base_price, visibility=VISIBLE, width=266, height=106, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=141.0, y=96.0, text=$ 45.00, input-type=0, ime-target=false, has-links=false}"
Run Code Online (Sandbox Code Playgroud)
它对我来说没有意义,它不应该打印字段的实际值与比较值?
现在我正在使用mockito或其他东西创建我们项目的单元测试框架.我发现有一个任务app:mockableAndroidJar可以构建一个mockableAndroidJar jar文件.这是为了什么?如何使用它?我还没有找到关于它的介绍.
背景故事:
这很重要,因为我试图比较它与从命令行执行测试的不同之处: ./gradlew :testDebug
您会看到,当从 Android Studio 运行测试包时,我的测试运行正常,但在 gradle 上方使用命令行运行会引发异常。
问题的根源在于,当在命令行上运行时,gradle 找不到依赖模块 B 的资源文件。抛出异常的行: setParametersFromResource(context, R.raw.coursera_mobile_android, VALUE_NAMESPACE);
android android-testing gradlew android-studio android-gradle-plugin
我正在尝试为其中一个静态方法编写单元测试用例.
protected static ScheduleCheck createScheduleCheck(Bundle extra){
long assetId = extra.getLong(ScheduleCheckRecommendationService.EXTRA_ASSET_ID);
int hour = extra.getInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_HOUR);
int minute = extra.getInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_MINUTE);
int ampm = extra.getInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_AMPM);
long landmarkId = extra.getLong(ScheduleCheckRecommendationService.EXTRA_LANDMARK_ID);
final ArrayList<Integer> weekday =
extra.getIntegerArrayList(ScheduleCheckRecommendationService.EXTRA_WEEK_DAY_LIST);
final Set<Weekday> weekdaySet = convertWeedays(weekday);
// Adjust hour.
hour = hour + 12 * ampm;
ScheduleCheck scheduleCheck = new ScheduleCheck();
scheduleCheck.setAssetId(assetId);
scheduleCheck.setTimeToFireInDisplayTimezone((int) FinderUtils.getTimeInSeconds(hour, minute));
scheduleCheck.setLandmarkId(landmarkId);
scheduleCheck.setWeekdaysToFireInDisplayTimezone(weekdaySet);
return scheduleCheck;
}
Run Code Online (Sandbox Code Playgroud)
单元测试案例:
public class ScheduleCheckRecommendationReceiverTest {
private static final long ASSET_ID = 1L;
private static final int HOUR = 10;
private …Run Code Online (Sandbox Code Playgroud) 我正在使用 Espresso 来实现我的应用程序的自动测试框架。但是在我设计的一些测试用例中,我发现我的测试总是失败,根本原因不在我的测试代码中的功能实现代码。根本原因是在android输入法模式下,有时是中文输入模式,而我的输入文本是英文,那么输入值就会失败。所以我想知道如何将当前的typeText输入法模式从中文切换到英文,或者如何在不手动配置的情况下确保输入法模式为英文?我相信这是一个重要的要求,因为当我们的应用程序支持多种语言时,我们需要此功能在测试期间自动切换到所需的语言。以下是我的代码,如果默认输入模式是英文就没有问题。
onView(withId(R.id.entryWordInput))
.perform(typeText(entryWord), closeSoftKeyboard());
onView(withId(R.id.OkButton))
.perform(click());
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我有多个测试作为 Android Jenkins 构建的一部分运行,包括单元测试和功能测试。我能够在 Jenkins 上成功发布测试结果,但我想查看单元测试和功能测试的单独结果图。Jenkins JUnit 发布者只为多个 XML 文件显示一个图表。有什么帮助吗?
我有一个list_view,我想测试一下.
list_view项目布局是RelativeLayout,适配器是ItemAdapter.
此代码工作正常(项目contftining Daft Punk可见):
@Test
public void listViewTest() {
Espresso.onView(withText("Daft Punk")).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
这两种变体都失败了:
(滚动开始,到达项目然后失败,错误就在这个问题的正确位置.)
@Test
public void listViewTest() {
Espresso.onData(artistWithName("Imagine Dragons")).perform(scrollTo(), click());
}
@Test
public void listViewTest() {
Espresso.onData(artistWithName("Imagine Dragons")).
.inAdapterView(withId(R.id.list_view))
.perform(scrollTo(), click());
}
Run Code Online (Sandbox Code Playgroud)
artistWithName定义:
public static Matcher<Object> artistWithName(String expectedName) {
Checks.checkNotNull(expectedName);
return artistWithName(equalTo(expectedName));
}
public static Matcher<Object> artistWithName(final Matcher<String> itemMatcher) {
Checks.checkNotNull(itemMatcher);
return new BoundedMatcher<Object, ArtistItem>(ArtistItem.class) {
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("ArtistTime with name: ");
itemMatcher.describeTo(description);
}
@Override
protected boolean matchesSafely(ArtistItem …Run Code Online (Sandbox Code Playgroud) 执行我们的一些仪器测试需要很长时间。因此,当我使用gradle connectedAndroidTest.
为什么我不用@Ignore注释这些测试?因为我想稍后使用这里adb shell描述的方式运行它们。
像这样:
运行除特定类中的测试之外的所有测试: adb shell am instrument -w -e notClass com.android.foo.FooTest com.android.foo/android.support.test.runner.AndroidJUnitRunner
如果我将这些测试标记为忽略并编译它们,则根本无法执行它们。
是否可以修改connectedAndroidTest或其他一些任务来达到我的需要?
我使用AndroidViewModelwith LiveData将Intent发送到IntentService并从EventBus接收事件。我需要意图和EventBus的应用程序上下文。
用本地测试测试AndroidViewModel类的最佳方法是什么?我可以从Robolectrics RuntimeEnvironment.application开始,但是似乎没有ShadowOf()用于AndroidViewModel来检查是否将正确的Intent发送到了正确的接收者。
也许可以通过Mockito使用我自己的模拟意图将其注入并注入到我的中AndroidViewModel,但这似乎不是很简单。
我的代码如下所示:
class UserViewModel(private val app: Application) : AndroidViewModel(app){
val user = MutableLiveData<String>()
...
private fun startGetUserService() {
val intent = Intent(app, MyIntentService::class.java)
intent.putExtra(...)
app.startService(intent)
}
@Subscribe
fun handleSuccess(event: UserCallback.Success) {
user.value = event.user
}
}
Run Code Online (Sandbox Code Playgroud)
肺动脉测试:
@RunWith(RobolectricTestRunner.class)
public class Test {
@Test
public void testUser() {
UserViewModel model = new UserViewModel(RuntimeEnvironment.application)
// how do I test that startGetUserService() is sending
// the Intent to MyIntentService and check the …Run Code Online (Sandbox Code Playgroud) 我们有一个核心的android应用程序模块(称为app)和一个动态功能模块(称为replay)。即使这是DFM,我们也会在安装时将其包括在内。在我们的DFM的AndroidManifest.xml中:
<dist:module
dist:instant="false"
dist:title="@string/title_replay">
<dist:delivery>
<dist:install-time />
</dist:delivery>
<dist:fusing dist:include="true" />
</dist:module>
Run Code Online (Sandbox Code Playgroud)
我们可以使用CL中的DFM构建apk: $ ./gradlew app:assembleDebug
现在在测试时间...我们的replay功能模块中没有任何仪器测试,只有app模块中。这样我们就可以从命令行运行检测测试了:$ ./gradlew app:connectedDebugAndroidTest
但是,我们最近开始尝试在Android Studio中运行检测测试。我们再次只想在app模块上运行测试:
现在的问题是,我们收到一堆奇怪的“ Android资源链接失败”错误:
What went wrong:
Execution failed for task ':replay:processDebugAndroidTestResources'.
/Users/user/.gradle/caches/transforms-2/files-2.1/48609a786af4d1714850acbdd03ace31/jetified-beacon-ui-1.0.3/
AndroidManifest.xml:15:9-19:54: AAPT: error: resource string/hs_beacon_empty (aka com.example.feature.replay.test:string/hs_beacon_empty) not found.
Run Code Online (Sandbox Code Playgroud)
我们通过几个第三方依赖项(facebook,leakcanary,beacon等)获取这些文件。它们似乎都是将值添加到现有xml文件(strings.xml,AndroidManifest.xml)的库。
看起来是replay构建过程的一部分,AAPT试图在我们的replay模块中查找实际上在其他库中的资源。但是为什么AAPT寻找我们在replay功能模块:com.example.android.feature.replay.test:string/hs_beacon_empty?
我对AAPT如何合并动态模块的资源没有完全的了解,而对于仪表测试apk来说,我什至没有了解。
我不明白为什么通过CL可以正常工作,所以我查看了Android Studio构建日志的顶部,并在运行工具测试时看到了这一点。
Executing tasks: [:replay:assembleDebug, :replay:assembleDebugAndroidTest, :app:assembleDebug, :app:assembleDebugAndroidTest]
Run Code Online (Sandbox Code Playgroud)
因此,现在更基本的问题是,为什么在运行针对应用模块的测试时,Android Studio为何尝试汇编重播和重播测试代码?
FWIW我正在运行Android Studio和AGP 3.5 …
android-testing ×10
android ×7
mockito ×2
gradle ×1
gradlew ×1
junit ×1
junit-runner ×1
junit4 ×1
listview ×1
robolectric ×1
ui-testing ×1
unit-testing ×1