使用Espresso,我尝试使用Home按钮测试将活动发送到后台,然后再次将其放到前台进行一些检查:
@EspressoTest
public void test() {
onSomeView().check(matches(isDisplayed()));
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
Context context = getInstrumentation().getTargetContext();
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
onSomeView().check(matches(isDisplayed()));
}
Run Code Online (Sandbox Code Playgroud)
我不得不使用intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);异常建议的,但除此之外我还测试了,启动它作为Launcher Activity,或使用
FLAG_ACTIVITY_REORDER_TO_FRONT,但视图不可见.即使测试通过.
是否可以为我的Android应用程序编写单元测试,我使用Volley进行网络请求.对于Eg.我想为登录功能编写单元测试,其中我发布了一个带有用户凭据的截击请求,并在响应中检查有效的用户对象.有没有人做过类似的事情?请提供示例或参考.
这是我的登录方法:
public void login() {
try {
JSONObject jsonRequest = new JSONObject();
String emailString = email.getText().toString();
jsonRequest.put("email", emailString);
String passwordString = password.getText().toString();
jsonRequest.put("password", passwordString);
NetworkUtil.postLogin(new Listener<User>() {
@Override
public void onResponse(User response) {
setUser(response);
onUserSuccess();
}
}, new ErrorListener("postLogin") {
@Override
public void onErrorResponse(VolleyError error) {
super.onErrorResponse(error);
onUserError(error);
}
}, jsonRequest);
} catch (Exception e) {
}
Run Code Online (Sandbox Code Playgroud)
我的postLogin方法就像添加一个截击请求:
public static void postLogin(Listener<User> listener, ErrorListener errorListener,
JSONObject jsonRequest) {
VolleySingleton
.getInstance()
.getRequestQueue()
.add(new GsonRequest<User>(getUrl("login"), "user_profile", User.class, jsonRequest, Method.POST,
listener, errorListener)); …Run Code Online (Sandbox Code Playgroud) 嗨,我正在尝试为我的活动编写测试用例.我有几个活动,当我尝试在其他ActivityTest类上运行测试时遇到错误,其中一个没有问题.
android.support.test.espresso.NoActivityResumedException:阶段RESUMED中没有活动.你忘了发动这项活动吗?(test.getActivity()或类似的)?
这是我的课,我的所有测试用例都失败了:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LocatingActivityTest
{
@Rule
public ActivityTestRule<LocatingActivity> mActivityTestRule = new ActivityTestRule<>(LocatingActivity.class);
private LocatingActivity mLocatingActivity;
@Before
public void setup()
{
mLocatingActivity = mActivityTestRule.getActivity();
}
@Test
public void viewsMustBeVisible()
{
onView(withId(R.id.locating_text)).check(matches(isCompletelyDisplayed()));
onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
onView(withId(R.id.locating_cancel_booking)).check(matches(isCompletelyDisplayed()));
onView(withId(R.id.locating_list_view)).check(matches(isDisplayed()));
}
@Test
public void viewsMustBeEnabled()
{
onView(withId(R.id.tvNoDriverFound)).check(matches(not(isCompletelyDisplayed())));
onView(withId(R.id.tvNextSearch)).check(matches(not(isCompletelyDisplayed())));
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这是我的另一个类,它的所有测试用例都通过了:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class BookingActivityTest
{
@Rule
public IntentsTestRule<BookingTaxiActivity> mActivityTestRule = new IntentsTestRule<>(BookingTaxiActivity.class);
private BookingTaxiActivity mBookingTaxiActivity;
@Before
public void setup()
{
mBookingTaxiActivity = mActivityTestRule.getActivity();
}
@Test
public void viewsMustBeVisible()
{ …Run Code Online (Sandbox Code Playgroud) 我正在关注这个关于 Material Components 的代码实验室,在完成它之后,我想我应该练习编写一些仪器测试。该应用程序运行良好。但是当我运行任何测试时,我不断收到此错误:
java.lang.RuntimeException: android.view.InflateException: Binary XML file line #37: Binary
XML file line #37: Error inflating class
com.google.android.material.textfield.TextInputLayout
at androidx.test.runner.MonitoringInstrumentation.runOnMainSync(MonitoringInstrumentation.java:450)
at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:564)
at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:300)
at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:282)
at com.google.codelabs.mdc.kotlin.shrine.LoginFragmentTest.username_and_password_editTexts_visible(LoginFragmentTest.kt:64)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at …Run Code Online (Sandbox Code Playgroud) junit android android-fragments android-espresso android-instrumentation
自从 Android 10 上改进的隐私更改Android 10 隐私更改以来,我注意到 Kotlin 中的屏幕截图失败测试观察程序规则(扩展了 Espresso BasicScreenCaptureProcessor)不再保存失败屏幕截图,因为我使用的是getExternalStoragePublicDirectoryAndroid 10 上已弃用的屏幕截图。
目前实现的概念非常类似于How to take Screenshot at the point where test failed in Espresso?
class TestScreenCaptureProcessor : BasicScreenCaptureProcessor() {
init {
this.mDefaultScreenshotPath = File(
File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"Failure_Screenshots"
).absolutePath
)
}
Run Code Online (Sandbox Code Playgroud)
正如在其他帖子中看到的,我可以使用getInstrumentation().getTargetContext().getApplicationContext().getExternalFilesDir(DIRECTORY_PICTURES)
这会将文件存储在 -/sdcard/Android/data/your.package.name/files/Pictures目录中,但connectedAndroidTestgradle 任务最后会删除该应用程序以及上面列出的文件夹。
我想知道是否有其他人遇到过类似的情况,并考虑过在 Android 10 上存储故障屏幕截图的方法,存储在测试运行完成后不会被删除的位置以及 Espresso Instrumentation 测试可以访问的位置。
我的 UI 测试在各种设备上运行,因此需要一种通用的文件存储方式来适应所有模型。
automation screenshot kotlin android-espresso android-instrumentation
目标
在RunListener使用 Espresso 运行 Android 仪器测试时,在测试失败时自定义执行自定义操作。
tl;博士
InstrumentationInfo.metaData是null,即使ApplicationInfo.metaData有我的信息。为什么?
迄今为止的进展
我可以让我的 RunListener 使用以下 adb 命令:
adb shell am instrument -w -e listener com.myproject.test.runlisteners.CustomRunListener -e class com.myproject.test.ui.HomeActivityTest#testWillFail com.myproject.test/android.support.test.runner.AndroidJUnitRunner
Run Code Online (Sandbox Code Playgroud)
这是在此处的 AndroidJUnitRunner 文档中指定的。
但是,该文档还指出可以在AndroidManifest.xml元数据元素中指定 RunListener 。到目前为止,我一直没有成功地让它发挥作用。
AndroidManifest.xml
我在我的<application>元素中添加了以下内容main/AndroidManifest.xml:
<meta-data
android:name="listener"
android:value="com.myproject.test.runlisteners.CustomRunListener" />
Run Code Online (Sandbox Code Playgroud)
这没有任何影响。通过各种方式,我发现这些代码行(由清单使用AndroidJUnitRunner并RunnerArgs从清单中获取自定义元数据参数)
InstrumentationInfo instrInfo = pm.getInstrumentationInfo(
getComponentName(), PackageManager.GET_META_DATA);
Bundle b = instrInfo.metaData;
Run Code Online (Sandbox Code Playgroud)
...还我一个null捆绑包。
我注意到生成的debug/AndroidManifest.xml没有我的元数据标签,因此,作为实验,我也将它添加到我的androidTest/AndroidManifest.xml文件中。看起来像这样:
<application
android:name=".BaseApplication">
<meta-data
android:name="listener" …Run Code Online (Sandbox Code Playgroud) junit android metadata android-manifest android-instrumentation
在过去的一周里,我一直在寻找有关 Android 端到端测试的有用信息。虽然我找到了 UI Automator,但它并没有引起我的注意。
我正在开发一个应用程序,它的快乐路径包含 6 个活动。我想在主屏幕中创建我的 Booking 对象并将其移动到快乐路径以检查其状态。我想我可以用MockWebServer模拟服务器响应。我看到的所有样本都在测试一项活动。
所以我的问题是我能否在一次测试中测试所有这些活动(快乐路径)?如果是,是否有任何样本?
设置:
我继承的一个较旧的项目有很多遗留的仪器测试,我想对它们施加超时,因为它们中的很多都可以无限期地挂起,这使得很难获得测试报告。我正在将测试更新为 Junit4 样式,但目前它们都在扩展 ActivityInstrumentationTestCase2。
到目前为止尝试过:
在AndroidJUnitRunner的文档中,它说要设置这个标志:
设置将应用于每个测试的超时(以毫秒为单位):-e timeout_msec 5000 ...
...所有参数也可以通过元数据标签在 AndroidManifest 中指定
我已经尝试将 AndroidJUnitRunner 配置添加到应用程序清单和测试清单,但 timeout_msec 元数据项到目前为止没有任何影响。
我知道这个主题有很多问题(和答案),但我已经尝试了在SO和其他网站上找到的所有内容,而且我还没有找到一种方法让JaCoCo包括使用Mockito的Android测试的覆盖范围.
我的问题:我想使用JaCoCo生成单元测试和仪器测试(androidTest)的代码覆盖率.我正在使用Mockito来模拟一些课程.我在GitHub中找到了一个使用JaCoCo的样本并将其作为起点.
https://github.com/rafaeltoledo/unified-code-coverage-android
当我运行该示例中包含的自定义jacocoTestReport任务时,代码覆盖率报告已正确生成,代码覆盖率为100%.该报告包括单元测试和android测试.但是,该示例不使用Mockito(我需要),因此我将以下内容添加到app/build.gradle中
dependencies {
...
androidTestCompile 'org.mockito:mockito-android:2.10.0'
}
Run Code Online (Sandbox Code Playgroud)
我在app/src/main/java/net/rafaeltoledo/coverage/Util.java中添加了一个名为Util的非常简单的Java类
public class Util {
public int anIntMethod() {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
并将以下简单测试添加到app/src/androidTest/java/net/rafaeltoledo/coverage/MainActivityTest.java中的现有android测试
@Test
public void utilMethod() {
Util util = Mockito.mock(Util.class);
Mockito.doReturn(10).when(util).anIntMethod();
assertThat(util.anIntMethod(), is(10));
}
Run Code Online (Sandbox Code Playgroud)
当我再次运行jacocoTestReport时,代码覆盖率降至88%,并且报告实际上显示我的测试未涵盖Util类,即使我显然有一个练习该类的测试.
(我想添加报告的截图,但我没有足够的声誉,所以这里有一个覆盖报告和执行报告的链接,表明两个测试实际上都已执行)
版本信息:Gradle插件:2.3.3 Jacoco:0.7.8.201612092310 Android Studio:2.3.3 Android构建工具:25.0.2
这是Jacoco限制还是我做错了什么?
android mockito jacoco android-gradle-plugin android-instrumentation
我正在尝试使用 Android Studio 为我的MainActivity.java班级使用 Junit4进行仪器测试 。我不知道我的代码是否正确,但这是我的测试类代码:
import android.widget.EditText;
import android.widget.TextView;
import androidx.test.espresso.Espresso;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest{
private EditText edt;
private TextView txt;
@Rule
ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void setup(){
edt = activityTestRule.getActivity().findViewById(R.id.edt);
txt = activityTestRule.getActivity().findViewById(R.id.txt);
}
@Test
public void edt_text_change() {
//i want this test to be passed
assertEquals("147", "147");
}
}
Run Code Online (Sandbox Code Playgroud)
构建.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29 …Run Code Online (Sandbox Code Playgroud) android ×8
junit ×3
automation ×1
jacoco ×1
java ×1
kotlin ×1
metadata ×1
mockito ×1
screenshot ×1
testing ×1
unit-testing ×1