gpt4 book ai didi

Android Espresso : Test filtering is not supported for given version of JUnit. 请将 JUnit 版本升级到至少 4.6

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:00 25 4
gpt4 key购买 nike

我真的不敢发布这个,但我在谷歌上没有找到任何相关的东西,所以我想这是一个菜鸟问题。

我想使用 Espresso 来测试我的 Android 应用。

我尝试下载示例项目,创建一个简单的项目并按照 android 开发者网站中的描述实现它,但我无法启动和运行它。

  • 我有构建工具 22.0.1
  • 支持图书馆 22

手动添加静态导入后,我解决了示例测试中的所有编译问题。但是当我运行它时,我得到了这个:

:app:cleanTest UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72200Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42200Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preCompileDebugUnitTestJava
:app:compileDebugUnitTestJava UP-TO-DATE
:app:compileDebugUnitTestSources UP-TO-DATE
:app:mockableAndroidJar UP-TO-DATE
:app:assembleDebugUnitTest UP-TO-DATE
:app:testDebug
:app:testDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 32.847 secs
Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6.

这是我的测试类:

package com.example.federicoponzi.testingexample;

import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import android.app.Activity;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;


/**
* Created by FedericoPonzi on 10/04/2015.
*/

import org.junit.Test;

@RunWith(AndroidJUnit4.class) //copied from a google example
@LargeTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest(){
super(MainActivity.class);
}
Activity mActivity;
@Override
protected void setUp() throws Exception {
super.setUp();

mActivity = getActivity();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());

}
private final String STRING_TO_TYPE = "Hello, Testing";
@Test
public void testChangeText_sameActivity()
{
onView(withId(R.id.edittextview)).perform(typeText(STRING_TO_TYPE), closeSoftKeyboard());
onView(withId(R.id.edittextview)).check(matches(withText(STRING_TO_TYPE)));

}

}

构建.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.example.federicoponzi.testingexample"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

编辑:gradle更新后的新错误:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [com.example.federicoponzi.testingexample.MainActivityTest]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 7.246 secs
No tests found for given includes: [com.example.federicoponzi.testingexample.MainActivityTest]

最佳答案

重要须知:

androidTestCompile-androidTest文件夹-ui测试

testCompile - test 文件夹 - 单元测试

对于单元测试:

添加:

testCompile 'junit:junit:4.12'

收件人:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
testCompile 'junit:junit:4.12' // <-- added
}

对于您的Espresso 测试:

删除 Junit 和注释。

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest(){
super(MainActivity.class);
}
Activity mActivity;

@Override
protected void setUp() throws Exception {
super.setUp();

mActivity = getActivity();
// injectInstrumentation(InstrumentationRegistry.getInstrumentation()); // not sure what this is

}
private final String STRING_TO_TYPE = "Hello, Testing";

public void testChangeText_sameActivity()
{
onView(withId(R.id.edittextview)).perform(typeText(STRING_TO_TYPE), closeSoftKeyboard());
onView(withId(R.id.edittextview)).check(matches(withText(STRING_TO_TYPE)));
}
}

例子:

https://github.com/googlesamples/android-testing/blob/master/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java https://github.com/jaredsburrows/AndroidGradleTemplate

官方文档: https://developer.android.com/tools/testing-support-library/index.htmlhttps://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide

关于Android Espresso : Test filtering is not supported for given version of JUnit. 请将 JUnit 版本升级到至少 4.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29594056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com