在Android开发人员中,我已经看到testPreconditions()方法应该在所有测试之前启动.但是在我的app测试中,它的表现就像一个普通的测试.它不会在所有测试之前运行.有什么不对 ?
以下是来自android开发人员的testPreconditions()的描述:"前置条件测试在执行其他测试之前检查初始应用程序条件.它类似于setUp(),但开销较少,因为它只运行一次."
我有一个显示许多项目的警告对话框.
private String[] choices; // populated externally
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setItems(choices, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
_callback.onChoiceSelected(which);
dialog.dismiss();
}
});
_dialog = builder.create();
Run Code Online (Sandbox Code Playgroud)
在不同的情况下(例如,如果您是管理员/客人),您将拥有不同数量的"选择".在我的espresso测试中,我基本上试图测试存在的项目数量.
public void test_ViewsSetupCorrectly() {
onView(withId(R.id.settings)).perform(click());
onView(ViewMatchers.withChild(withClassName(Matchers.equalToIgnoringCase("TextView")))).check(matches(isDisplayed()));
/*// What I would ideally like ... but doesn't work as well :( :
onView(withClassName(equalToIgnoringWhiteSpace("DialogTitle"))).check(matches(hasProperty("child-count",
is(new Integer(1))
)));*/
}
Run Code Online (Sandbox Code Playgroud)
这些基本上抛出一个异常,如下:
我认为onData调用仅用于执行操作?我不是必须采取行动.谁能帮我实际测试一下?
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: has child: with class name: eqIgnoringCase("TextView")
If the target view is not part of …Run Code Online (Sandbox Code Playgroud) 我有一个函数,我想测试哪个在okHttp回调中运行.我正在尝试使用Robolectrics测试它,但回调从未执行过.我认为这是因为测试在请求后继续运行而不等待okHttp返回.到目前为止,我已经尝试过:
ShadowLooper.pauseMainLooper();
Robolectric.flushBackgroundScheduler();
ShadowLooper.unPauseMainLooper();
Run Code Online (Sandbox Code Playgroud)
但那没用.有什么建议?
编辑:
这是我的代码示例:
ApiClient.sendSomeDataToServer(data, callback);
Run Code Online (Sandbox Code Playgroud)
其中ApiClient是包含okHttp客户端的帮助程序类.sendSomeDataToServer API调用看起来像这样:
public static void sendSomeDataToServer(MyObject data, Callback callback){
final Request request = new Request.Builder()
.url(API_SOME_URL)
.post(RequestBody.create(JSON, myObject.getAsJson().toString()))
.build();
sHttpClient.newCall(request).enqueue(callback);
}
Run Code Online (Sandbox Code Playgroud)
其中sHttpClient是初始化的OkHttpClient.
我可以通过Thread.sleep(5000)在我的测试代码中强制并提供自定义回调来测试上面的执行.我试图测试的代码是在回调中.有什么建议我可以测试吗?我真的不想更改主代码以适应测试框架 - 应该反过来.
我使用Espresso来测试很多活动和它们之间的过渡.我想为这些活动编写不同的测试,并将Activity Intents链接到相应的测试用例.
1.活动A(testActivityA运行)2.ActivityA启动ActivityB(testActivity B)应该被调用
是否有可能使用浓缩咖啡或任何其他测试框架?
android automated-tests robotium android-testing android-espresso
我正在尝试将测试从Java转换为Kotlin。
简单的单元测试已成功翻译,如下所示:
class BindingUtilsTest {
@Test @Throws(Exception::class)
fun testConvertBooleanToVisibility_visible() {
assertEquals(BindingUtils.convertBooleanToVisibility(true), View.VISIBLE)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行androidTest时,它失败并显示以下消息:“未找到测试”,并且
测试运行已开始
测试已完成。空的测试套件。
在使用Java时,代码可以完美运行。相关代码:
build.gradle部分:
apply plugin: "com.android.application"
apply plugin: "com.neenbedankt.android-apt"
// for tests
apply plugin: 'kotlin-android'
// defaultConfig
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
sourceSets {
test.java.srcDirs += 'src/test/kotlin' // tests are there
androidTest.java.srcDirs += 'src/androidTest/kotlin' // and there
}
// unit tests
testApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
testCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
// android tests
androidTestApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
androidTestCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
Run Code Online (Sandbox Code Playgroud)
简单测试:
@RunWith(AndroidJUnit4::class) class MainDrawerActivityTest {
private …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的项目运行Instrumental测试.但它们不能在版本低于5的设备(仿真器)上运行(API 21).
我一直试图解决这个问题,但仍然面临着这个问题.我得到以下例外.
02-15 10:46:08.965 1127-1143/? E/AndroidRuntime: FATAL EXCEPTION: Instr: android.support.test.runner.AndroidJUnitRunner
java.lang.ExceptionInInitializerError
at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:81)
at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:524)
at android.support.test.runner.AndroidJUnitRunner.createTestRequestBuilder(AndroidJUnitRunner.java:379)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:352)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Caused by: java.lang.NoClassDefFoundError: org.junit.runner.manipulation.Filter$1
at org.junit.runner.manipulation.Filter.<clinit>(Filter.java:21)
at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:81)
at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:524)
at android.support.test.runner.AndroidJUnitRunner.createTestRequestBuilder(AndroidJUnitRunner.java:379)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:352)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过类似问题的所有解决方案.
这是我的项目结构的样子.
并且测试的gradle依赖性
/************* TEST STUFF ***************/
androidTestCompile 'junit:junit:4.12'
androidTestCompile fileTree(include: ['robotium-solo-5.5.2.jar'], dir: 'libs')
//mockito dependencies
androidTestCompile 'org.mockito:mockito-core:2.7.6'
androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
androidTestCompile files('libs/dexmaker-1.0.jar')
// Set this dependency to build and run Espresso tests
androidTestCompile('com.android.support.test.espresso:espresso-core:+') {
exclude group: 'com.android.support', module: …Run Code Online (Sandbox Code Playgroud) 我试图测试应用程序的发布版本.所以我build.gradle在我的应用程序中添加了以下配置.但这并没有产生任何影响.测试始终在调试版本上运行
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.****.****"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0 Beta"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
testBuildType "release"
signingConfigs {
release {
keyAlias '******'
keyPassword '*****'
storeFile file('path to keystore')
storePassword '*****'
}
}
buildTypes {
release {
minifyEnabled true
debuggable true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
multiDexEnabled true
}
}
}
Run Code Online (Sandbox Code Playgroud)
当在其他SO线程中搜索答案时,我发现testBuildType "release"将在发布版本上运行测试,但它不起作用
我有一个用科特林写的片段。我使用导入布局视图
import kotlinx.android.synthetic.main.my_fragment_layout.
Run Code Online (Sandbox Code Playgroud)
在我的一种方法中,我设置了TextView的文本:
fun setViews() {
myTextView.text = "Hello"
// In Java I would have used:
// (getView().findViewById(R.id.myTextView)).setText("Hello");
}
Run Code Online (Sandbox Code Playgroud)
在普通的JVM单元测试中,我想使用Mockito测试此方法。例如,如果上述方法是用Java编写的,则可以执行以下操作:
public void setViewsTest() {
// Mock dependencies
View view = Mockito.mock(View.class);
TextView myTextView = Mockito.mock(TextView.class);
when(fragment.getView()).thenReturn(view);
when(view.findViewById(R.id. myTextView)).thenReturn(myTextView);
// Call method
fragment.setViews();
// Verify the test
verify(myTextView).setText("Hello");
}
Run Code Online (Sandbox Code Playgroud)
使用Kotlin的kotlinx.android.synthetic视图时如何做类似的实现?
因此,我正在Android Studio 3.1.4中进行代码覆盖率的测试。
如果我在Java类上运行,它将在装订线中很好地显示突出显示的条。但是,如果它是Kotlin文件,则仅在类名称旁边显示彩色条。。它也不会在项目视图中显示百分比。
有什么解决办法吗?
我不仅仅使用Jacoco内置的intelliJ代码覆盖率工具。
在最后一阵子,我试图更改一个多模块Android应用程序以使用新的com.android.dynamic-feature插件。这项更改的一部分是将我的仪器测试移出了基本模块,并让他们测试了多个功能。该com.android.test 插件似乎使我能够做到这一点。
我必须注意到,虽然从androidTest源集(使用app:assembleDebugAndroidTest)构建时运行正常的同一测试会在从com.android.test插件(使用app-tests:assemble)构建时导致奇怪的错误。特别是,当测试启动Activity时,AndroidX AppCompat存在问题:
Caused by: java.lang.IllegalStateException: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat.
at androidx.appcompat.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:756)
at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:196)
at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:260)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at biz.laenger.android.foo.app.presentation.main.MainActivity.onCreate(MainActivity.kt:28)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
... 9 more
Run Code Online (Sandbox Code Playgroud)
我创建了这个最小的示例项目,可以在其中复制问题(自述文件中的步骤)。
从主题出现实际问题时,我发现了上述错误。但是,正确设置了主题,并且在“常规”测试测试和手动启动应用程序时可以很好地进行“活动创建”。该应用程序和测试在API 28设备上运行。
在配置:app-tests模块时,我按照说明进行操作并最终得到以下配置:
Caused by: java.lang.IllegalStateException: This app has been …Run Code Online (Sandbox Code Playgroud) android-testing ×10
android ×9
kotlin ×3
java ×2
junit ×2
unit-testing ×2
androidx ×1
dagger-2 ×1
hamcrest ×1
mockito ×1
robolectric ×1
robotium ×1
testing ×1
wiremock ×1