我正在尝试在Espresso仪器测试中设置Dagger,以模拟对外部资源的调用(在这种情况下为RESTful服务).我在Robolectric中为我的单元测试所遵循的模式是扩展我的生产Application类并使用将返回模拟的测试模块覆盖Dagger模块.我试图在这里做同样的事情,但是当我尝试将应用程序转换为我的自定义应用程序时,我在Espresso测试中遇到了ClassCastException.
这是我到目前为止的设置:
生产
在app/src/main/java/com/mypackage/injection下我有:
MyCustomApplication
package com.mypackage.injection;
import android.app.Application;
import java.util.ArrayList;
import java.util.List;
import dagger.ObjectGraph;
public class MyCustomApplication extends Application {
protected ObjectGraph graph;
@Override
public void onCreate() {
super.onCreate();
graph = ObjectGraph.create(getModules().toArray());
}
protected List<Object> getModules() {
List<Object> modules = new ArrayList<Object>();
modules.add(new AndroidModule(this));
modules.add(new RemoteResourcesModule(this));
modules.add(new MyCustomModule());
return modules;
}
public void inject(Object object) {
graph.inject(object);
}
}
Run Code Online (Sandbox Code Playgroud)
我用以下方式使用:
BaseActivity
package com.mypackage.injection.views;
import android.app.Activity;
import android.os.Bundle;
import com.mypackage.injection.MyCustomApplication;
public abstract class MyCustomBaseActivity extends Activity {
@Override
protected void …Run Code Online (Sandbox Code Playgroud) 有没有可靠的方法让Espresso等待WebViews完成加载?
我希望有人有一个没有任何这些缺点的解决方案.我曾希望espresso-web软件包可以提供解决方案,但它似乎没有提供与加载有关的任何内容.
我正在使用浓缩咖啡进行测试,但有时我会尝试从外部存储中获取图像,并且使用棉花糖我需要运行时权限,否则会出现异常崩溃并且测试将失败.
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
// this library uses the newest app compat v22 but the espresso contrib still v21.
// you have to specifically exclude the older versions of the contrib library or
// there will be some conflicts
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.squareup.retrofit:retrofit-mock:1.9.0'
androidTestCompile 'com.squareup.assertj:assertj-android:1.1.0'
androidTestCompile 'com.squareup.spoon:spoon-client:1.2.0'
Run Code Online (Sandbox Code Playgroud)
我该如何管理呢?
我应该为运行时权限编写测试,还是有办法禁用它进行测试?
在测试运行之前,我应该像她在这里说的那样给出权限吗?https://www.youtube.com/watch?list=PLWz5rJ2EKKc-lJo_RGGXL2Psr8vVCTWjM&v=C8lUdPVSzDk
我在动作栏中有一个菜单,我通过以下方式创建:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 98,Menu.NONE,R.string.filter).setIcon(R.drawable.ic_filter_list_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, 99,Menu.NONE,R.string.add).setIcon(R.drawable.ic_add_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Run Code Online (Sandbox Code Playgroud)
和menu_main.xml看起来像:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="@drawable/ic_settings_white_48dp"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
在Espresso中测试时,我想点击"添加"图标(menuId 99).我试过了
@Test
public void testAdd() {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withText(R.string.add)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
但是这会因NoMatchingViewException而失败.(设置项,直接在xml中定义,我可以使用相同的代码单击.)
这是针对targetSdkVersion 23和AppCompatActivity的.工具栏的相关行是:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
if( getSupportActionBar() != null ) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)
和tool_bar.xml看起来像:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/ColorPrimary"
android:elevation="4dp"
tools:ignore="UnusedAttribute">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud) 在我的build.gradle文件中,我有支持库依赖项:
compile "com.android.support:appcompat-v7:22.2.0"
compile "com.android.support:recyclerview-v7:22.2.0"
compile "com.android.support:design:22.2.0"
Run Code Online (Sandbox Code Playgroud)
我也有espresso测试的依赖项:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
Run Code Online (Sandbox Code Playgroud)
此时一切都运行良好,但是当我添加依赖项时,espresso-contrib我得到了InflateException我的RecyclerView
android.view.InflateException: Binary XML file line #33: Error inflating class android.support.v7.widget.RecyclerView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
...
Caused by: java.lang.IllegalStateException: Binary XML file line #33: Unable to find LayoutManager android.support.v7.widget.@2131296518
at android.support.v7.widget.RecyclerView.createLayoutManager(RecyclerView.java:500)
at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:438)
at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:404)
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.@2131296518" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.myapp.debug.test-1/base.apk", …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据我的功能编写espresso函数以匹配第一个espresso找到的元素,即使找到了多个匹配的项目.
例如:我有一个包含商品价格的单元格的列表视图.我希望能够将货币兑换成加元并验证商品价格是否为加元.
我正在使用这个功能:
onView(anyOf(withId(R.id.product_price), withText(endsWith("CAD"))))
.check(matches(
isDisplayed()));
Run Code Online (Sandbox Code Playgroud)
这会抛出AmbiguousViewMatcherException.
在这种情况下,我不关心有多少或几个单元格显示CAD,我只想验证它是否显示.有没有办法让espresso在遇到符合参数的物体时立即通过此测试?
我正在Android Studio 0.5.0用Gradle 1.11.我正在尝试从com.jakewharton.espresso安装Espresso库 :espresso:1.1-r2.由于某些原因,AS在项目同步后无法识别Espresso类.所以每次我尝试导入的时间import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;内androidTest文件的文件夹它标记为无效.
这是我的build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.squareup.dagger:dagger:1.2.1'
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
exclude group: 'com.squareup.dagger'
}
}
Run Code Online (Sandbox Code Playgroud)
外部图书馆:

我想声明我正在测试的我的Acitivty在执行某些操作时已完成.不幸的是到目前为止,我只是通过在测试结束时添加一些睡眠来断言它.有没有更好的办法 ?
import android.content.Context;
import android.os.Build;
import android.support.test.rule.ActivityTestRule;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertTrue;
@SuppressWarnings("unchecked")
@RunWith(JUnit4.class)
@LargeTest
public class MyActivityTest {
Context context;
@Rule
public ActivityTestRule<MyActivity> activityRule
= new ActivityTestRule(MyActivity.class, true, false);
@Before
public void setup() {
super.setup();
// ...
}
@Test
public void finishAfterSomethingIsPerformed() throws Exception {
activityRule.launchActivity(MyActivity.createIntent(context));
doSomeTesting();
activityRule.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
fireEventThatResultsInTheActivityToFinishItself();
}
});
Thread.sleep(2000); // this is needed :(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) …Run Code Online (Sandbox Code Playgroud) testing android android-activity android-testing android-espresso
我在androidTest文件夹中创建了一个虚拟活动,并在androidTest文件夹中的AndroidManifest文件中声明了该活动.
我的基本目的是通过使用framelayout容器将其放入虚拟活动来测试可重用片段.
AndroidManife文件夹里面的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.droid.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="18"
tools:overrideLibrary="android.support.test.uiautomator.v18" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.droid" />
<application>
<uses-library android:name="android.test.runner" />
<activity
android:name="com.droid.DummyActivityForTest"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
我的测试类TestWidgets.java
public class TestWidgets extends ActivityInstrumentationTestCase2<DummyActivityForTest> {
private AppCompatActivity mActivity;
public TestWidgets() {
super(DummyActivityForTest.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
@Test
public void testAddSpecializationClick() {
onView(withId(R.id.widgets_rv)).perform(
RecyclerViewActions.actionOnItemAtPosition(4, click()));
Assert.fail("Not Implemented");
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的测试类时,它抛出异常, …
android unit-testing android-espresso android-instrumentation
我正在尝试运行一个取决于上下文的本地单元测试,并遵循本指南:https://developer.android.com/training/testing/unit-testing/local-unit-tests#kotlin 我设置像我这样的项目(点击此链接:https://developer.android.com/training/testing/set-up-project):
的build.gradle(APP)
android {
compileSdkVersion 28
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 76
versionName "2.6.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
testOptions {
unitTests.returnDefaultValues = true
unitTests.all {
// All the usual Gradle options.
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen { false }
showStandardStreams = true
}
}
unitTests.includeAndroidResources = true
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion", { …Run Code Online (Sandbox Code Playgroud) android unit-testing android-espresso android-instrumentation
android-espresso ×10
android ×9
testing ×4
unit-testing ×2
dagger ×1
gradle ×1
java ×1
webview ×1