我正在为a编写测试ContentProvider,insert我正在通知更改getContext().getContentResolver().notifyChange(mUri, null);
我的测试类扩展ProviderTestCase2.我创建了以下模拟ContentObserver类:
private class ContentObserverMock extends ContentObserver {
public boolean changed = false;
public ContentObserverMock(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
@Override
public void onChange(boolean selfChange) {
changed = true;
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试用例:
public void testInsertNotifyContentChanges() {
ContentResolver resolver = mContext.getContentResolver();
ContentObserverMock co = new ContentObserverMock(null);
resolver.registerContentObserver(CONTENT_URI, true, co);
ContentValues values = new ContentValues();
values.put(COLUMN_TAG_ID, 1);
values.put(COLUMN_TAG_CONTENT, "TEST");
resolver.insert(CONTENT_URI, values);
assertTrue(co.changed); …Run Code Online (Sandbox Code Playgroud) 我正在尝试从特定视图访问按钮.相同的视图显示6次.这是我正在使用的代码.
public void testTimeConfig(){
onData(withDesc("description")).onChildView(withId(R.id.positive)).perform(click());
}
private static Matcher<Object> withDesc(String desc) {
return allOf(is(instanceOf(String.class)), is(desc));
}
Run Code Online (Sandbox Code Playgroud)
当我跑步时,我收到一个错误:
在视图上执行'加载适配器数据'时出错'可从类中分配:class android.widget.AdapterView'.
这是访问子视图的最佳方式吗?如果是这样,怎么样?
这是我现在尝试使用的代码.
Run Code Online (Sandbox Code Playgroud)onView(allOf((withContentDescription("description")), hasSibling(withContentDescription("SettingsLayout")), hasSibling(withId(R.id.positive)))).perform(click());
和
Run Code Online (Sandbox Code Playgroud)onView(allOf((withContentDescription("description")), hasSibling(withId(R.id.positive)))).perform(click());
出现此错误:
Run Code Online (Sandbox Code Playgroud)No views in hierarchy found matching
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/setAGoalTitle"
android:layout_alignParentLeft="true"
class="com.xxx"
android:layout_marginTop="30dp"
android:id="@+id/timeGoalWidget"
app:goalLabel="@string/time_min_upper"
app:icon="@drawable/ic_icn_summary_time"
app:step="300"
app:valueFormat="time"
android:gravity="center_horizontal"
android:layout_marginBottom="60dp"
android:contentDescription="setAGoalTimeConfigurator"/>
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/timeGoalWidget"
class="com.xxx"
android:id="@+id/distanceGoalWidget"
app:goalLabel="@string/distance_upper"
app:icon="@drawable/ic_icn_summary_track"
app:step="0.25"
app:valueFormat="decimal_two"
android:gravity="center_horizontal"
android:contentDescription="setAGoalDistanceConfigurator"/>
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timeGoalWidget"
android:layout_alignLeft="@id/timeGoalWidget"
class="com.xxx"
android:id="@+id/paceGoalWidget"
app:goalLabel="@string/pace_upper"
app:icon="@drawable/ic_icn_summary_pace"
app:valueFormat="time"
app:step="10"
android:gravity="center_horizontal"
android:layout_marginBottom="60dp"
android:contentDescription="setAGoalPaceConfigurator"/>
<view
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) 对于我的仪器测试我正在使用Robotium.大多数情况下,除了离线情况,我能够测试一切.
只要我禁用数据(使用adb,模拟器中的F8快捷键等等),测试就会断开连接.它继续在设备/模拟器中,但没有报告结果.
所以,我有一个想法,只将应用程序放在离线模式而不是整个设备.问题是我不知道怎么样......
使用iptablesApi我需要root我的设备.我已经读过,Mobiwol应用程序使用某种VPN来限制应用程序的互联网访问,而无需生根设备.
问题Mobiwol应用程序 如何阻止每个应用程序的互联网连接?或者还有其他方法可以离线测试apks吗?
编辑12/30/2014
我忘了说我可以脱机运行测试,但是当设备处于脱机状态时我必须开始测试.目前,我将测试分为OFFLINE和ONLINE.运行ONLINEs后,我执行着名的adb kill-server和adb start-server.之后我执行OFFLINEs.
我们有一个库项目,多个应用程序依赖于它.单元测试在库项目中.我们能够从Android Studio中的依赖项目运行测试,但是
./gradlew :[DependentProject]:connectedAndroidTest
Run Code Online (Sandbox Code Playgroud)
总是返回"没有找到测试,无事可做".
通过观察,我发现在Android Studio中,它似乎只执行gradle任务:
:[DependentProject]:assembleDebug, :[DependentProject]assembleDebugTest
Run Code Online (Sandbox Code Playgroud)
然后使用adb安装目标并测试apk,adb shell是运行测试的工具.
由于connectedAndroidTest依赖于这两个任务,我安装了它生成的目标和测试apks,并手动调用了instrument命令,测试开始了.
adb shell am instrument -w com.package.test/android.test.InstrumentationTestRunner
Run Code Online (Sandbox Code Playgroud)
然后问题来了,connectedAndroidTest在哪里寻找测试,以及为什么adb仪器可以找不到测试?如何解决这个问题?
android gradle android-testing android-studio android-gradle-plugin
我知道这对所有Android开发者来说都是一个乏味的话题.但究竟什么是Android测试的正确方法?
这是我可以想象的.
70%单元测试(JUnit测试所有业务逻辑,网络层,数据库层等......)
20%集成测试(也许针对模拟服务器进行测试?主要测试API结果?)
10%的UI测试(模拟除UI交互之外的任何其他内容,很可能是Mockito + Espresso)
这是其他人都在追随的还是另一种模式?
提前致谢!
我想做什么和问题
我将Android Studio和Android Gradle Plugin更新为3.0.0,将Gradle Wrapper更新为4.2.1,并希望通过IDE在设备上构建和部署我的Android Gradle项目.
当我尝试将我的:app模块部署到连接的设备时,我收到错误:
错误:配置项目':integration-test'时出现问题.变体'debug'没有类型'INSTANT_RUN_MERGED_MANIFESTS'的输出
项目详情(简化)
该项目包括:
settings.gradle
include :library
include :app
include :integration-test
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.integration_test">
<!-- from https://stackoverflow.com/questions/45631959/how-to-place-android-instrumentation-test-files-outside-of-project-directory -->
<!-- Specify runner and target application package -->
<instrumentation
android:name="android.support.test.runner.AndroidJUnitRunner"
android:functionalTest="false"
android:handleProfiling="false"
android:label="Tests for com.domain.pro.client"
android:targetPackage="com.domain.pro.client"/>
<application>
<uses-library android:name="android.test.runner" />
</application>
Run Code Online (Sandbox Code Playgroud)
上次它的工作原理是: - Build Tools 2.2.3,Gradle 3.4.1和Android Studio 2.3.3
题
有没有人使用com.android.test插件(使用AndroidManifest文件)与Android Gradle …
android android-testing gradle-android-test-plugi android-studio-3.0 android-gradle-3.0
So I'm using the Android Management API to manage and handle deployment for an app to a kiosk device I am working on.
I've created an organisation, created a policy, and ensured the app is limited to managed google play only, and assigned the organisation to the app.
I've enrolled some devices onto the policy, and when the app is moved to prod (currently this is fine as there are only a handful of test devices on that policy), it …
android google-play android-testing android-enterprise android-management-api
最近我展示了一个关于 androidX 测试的谷歌 IO 视频,其中引用了“一次编写,到处运行”。这让我很高兴了解 androidX 测试库。
我发现经过很长时间谷歌为开/关设备的单元测试和仪器测试提出了很好的单一库。但是我发现在开/关设备上运行相同的测试有些困难。
基本上在 Android 中,我们创建了两个源根test/java,androidTest/java分别存储单元测试和仪器测试。单元测试在 JVM 上运行,Instrumentation 在设备/模拟上运行。
然后我为test/java目录中的片段之一编写了测试。
@RunWith(AndroidJUnit4::class)
class MyFragmenTest {
lateinit var scenario: FragmentScenario<MyFragment>
@Before
fun setUp() {
scenario = launchFragmentInContainer<MyFragment>()
}
@Test
fun `sample test`() {
scenario.onFragment {
// something
}
// some assertion
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我使用小的绿色运行图标执行这个测试时,它在没有模拟器的 JVM 中运行这个测试,这很棒。但是要在设备上运行相同的测试,我必须移动此代码androidTest/java源根目录。
基本上我得到了相同的测试可以在任何地方运行,当我们使用 androidX 测试库时,您不必依赖不同的工具和库来完成相同的工作。
我试过的。
之后,在 google 上搜索我发现我们必须sharedTest/java使用下面的 gradle 行创建源根目录,以便它可以在设备上或设备外运行。
android {
...
sourceSets {
androidTest {
java.srcDirs += "src/sharedTest/java"
}
test …Run Code Online (Sandbox Code Playgroud) android android-testing android-jetpack androidx androidx-test
执行单元测试时出现以下错误
java.lang.AssertionError: Activity never becomes requested state "[CREATED, STARTED, RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")
at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:272)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:238)
at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$3$ActivityScenarioRule(ActivityScenarioRule.java:98)
at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda$3.get(Unknown Source:4)
at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
Run Code Online (Sandbox Code Playgroud)
我的测试代码
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {
private val intent = Intent(ApplicationProvider.getApplicationContext(), AudioPlayerActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(HomePageArouterConstants.KEY_ID,"1")
}
@get:Rule
val activityRule:ActivityScenarioRule<AudioPlayerActivity> = activityScenarioRule(intent)
@Test
fun testVote(){
// Assert.assertEquals(1,1)
val scenario = activityRule.scenario
scenario.moveToState(Lifecycle.State.RESUMED)
onView(withId(R.id.linear_layout_like)).perform(click())
}
}
Run Code Online (Sandbox Code Playgroud)
谁有这个问题的想法?
我在我的MainActivityTest班级中配置了如下权限规则
@Rule
public GrantPermissionRule permissionRule =
GrantPermissionRule.grant(RECORD_AUDIO, WRITE_EXTERNAL_STORAGE);
Run Code Online (Sandbox Code Playgroud)
当我运行以下命令以使用 api 27 在模拟器上执行测试时
./gradlew connectedCheck
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下错误
com.example.myapplication.MainActivityTest > testLaunch_main_activity[Pixel_XL_API_27(AVD) - 8.1.0] FAILED
androidx.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,权限在应用程序信息设置中显示为已授予,但在 api 版本 27(或更低版本)的模拟器上运行测试时仍然要求权限
有人可以确认这是否是某些 android 插件中的错误,或者我在这里遗漏了什么。
android android-fragments android-testing android-permissions androidx
android ×10
android-testing ×10
androidx ×2
google-play ×1
gradle ×1
hamcrest ×1
unit-testing ×1