gpt4 book ai didi

android - 从 IDE 运行测试有效,但不能从命令行运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:17 24 4
gpt4 key购买 nike

我编写过单元测试、仪器测试和 Espresso 测试。我还使用 Android Test Orchestrator 运行它们以获得清晰的应用程序状态(对于 Espresso 测试很重要)。当我从 Android Studio 运行这些测试时,一切正常。但是当我尝试使用命令行时,我收到了我无法真正理解的错误。

当我尝试时:

./gradlew connectedAndroidTest or connectedDebugAndroidTest

我收到:

Instrumentation run failed due to 'java.lang.IllegalStateException'
com.android.builder.testing.ConnectedDevice > No tests found.[SM-J106H -
6.0.1] FAILED
No tests found. This usually means that your test classes are not in the
form that your test runner expects (e.g. don't inherit from TestCase or lack
@Test annotations).

当然,我所有的测试都带有@Test 注释。

当我尝试

adb shell am instrument -w my.package/android.test.InstrumentationTestRunner

我收到了

INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: 
ComponentInfo{mypackage/myCustomRunner}
INSTRUMENTATION_STATUS_CODE: -1

我使用 CustomTestRunner,但错误保持不变。

当我尝试

 adb shell 'CLASSPATH=$(pm path android.support.test.services) app_process / 
\
android.support.test.services.shellexecutor.ShellMain am instrument -w -e \
targetInstrumentation
mypackage/myTestRunner \
android.support.test.orchestrator/.AndroidTestOrchestrator'

那么输出等于:

Time: 0

OK (0 tests)

有人可以向我解释我做错了什么吗?我真的不明白为什么命令行没有任何作用,但在 Android Studio 中一切都运行良好。

/编辑

我的自定义运行器:

public final class CustomTestRunner extends AndroidJUnitRunner {

private static final String TAG = "CustomTestRunner";

@Override
public void onStart() {

try {
TestListener.getInstance().testRunStarted();
} catch (Exception e) {
e.printStackTrace();
}

runOnMainSync(new Runnable() {
@Override
public void run() {
Context app = CustomTestRunner.this.getTargetContext().getApplicationContext();

CustomTestRunner.this.disableAnimations(app);
}
});

ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(new ActivityLifecycleCallback() {
@Override public void onActivityLifecycleChanged(Activity activity, Stage stage) {
if (stage == Stage.PRE_ON_CREATE) {
activity.getWindow().addFlags(FLAG_DISMISS_KEYGUARD | FLAG_TURN_SCREEN_ON | FLAG_KEEP_SCREEN_ON);
}
}
});

RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(Scheduler scheduler) throws Exception {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
});

RxJavaPlugins.setComputationSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(Scheduler scheduler) throws Exception {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
});

RxJavaPlugins.setNewThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(Scheduler scheduler) throws Exception {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
});

super.onStart();
}


@Override
public void finish(int resultCode, Bundle results) {
try {
TestListener.getInstance().testRunFinished();
} catch (Exception e) {
e.printStackTrace();
}

super.finish(resultCode, results);
enableAnimations(getContext());
}

private void disableAnimations(Context context) {
int permStatus = context.checkCallingOrSelfPermission(Manifest.permission.SET_ANIMATION_SCALE);
if (permStatus == PackageManager.PERMISSION_GRANTED) {
setSystemAnimationsScale(0.0f);
}
}

private void enableAnimations(Context context) {
int permStatus = context.checkCallingOrSelfPermission(Manifest.permission.SET_ANIMATION_SCALE);
if (permStatus == PackageManager.PERMISSION_GRANTED) {
setSystemAnimationsScale(1.0f);
}
}

private void setSystemAnimationsScale(float animationScale) {
try {
Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub");
Method asInterface = windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class);
Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class);
Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager");
Method setAnimationScales = windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class);
Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales");

IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window");
Object windowManagerObj = asInterface.invoke(null, windowManagerBinder);
float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj);
for (int i = 0; i < currentScales.length; i++) {
currentScales[i] = animationScale;
}
setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales});
Log.d(TAG, "Changed permissions of animations");
} catch (Exception e) {
Log.e(TAG, "Could not change animation scale to " + animationScale + " :'(");
}
}
}

那是我的一个 Espresso 测试类(可见 RecyclerView 列表中的一个项目的 DetailView)

@RunWith(AndroidJUnit4.class)
public class DetailActivityTest {

private IdlingResource mInitialInformationIdlingResource;

@Before
public void setUp() throws UiObjectNotFoundException, InterruptedException {
SetupHelper.setUp();

File tempRealmFile = new File(InstrumentationRegistry.getTargetContext().getFilesDir(), PRODUCT_REALM_DB_FILE_NAME);

if(tempRealmFile.length() <= 8192 && CustomAssertion.doesViewExist(R.id.countries)) {

onView(withId(R.id.countries))
.check(matches(isDisplayed()));
onData(anything()).inAdapterView(withId(R.id.countries)).atPosition(3).perform(click());

mInitialInformationIdlingResource = new InitialInformationIdlingResource();
IdlingRegistry.getInstance().register(mInitialInformationIdlingResource);

Espresso.onView(withText("OK"))
.check(matches(isDisplayed()))
.perform(click());
}
}

@Test
public void ensureDetailViewWorks() throws UiObjectNotFoundException {
SetupHelper.checkForDialogs();

onView(withId(R.id.show_filter_results)).perform(scrollTo());

onView(withId(R.id.show_filter_results))
.check(matches(isDisplayed())).perform(scrollTo(), click());

onView(withId(R.id.resultList)).perform(RecyclerViewActions.actionOnItemAtPosition(1, click()));
onView(withId(R.id.main_container)).check(matches(isDisplayed()));
onView(withId(R.id.detail_item_icon)).check(matches(isDisplayed()));
}

我在 build.gradle 中的构建类型

buildTypes {

debug {
debuggable true
minifyEnabled false
versionNameSuffix "-debug"
manifestPlaceholders = [HOCKEYAPP_APP_ID: ""]
testCoverageEnabled true
}

release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
versionNameSuffix "-release"
manifestPlaceholders = [HOCKEYAPP_APP_ID: ""]
}
}

最佳答案

查看您的设备/模拟器上的日志。甚至在测试开始之前,应用程序/测试代码中的某些内容就崩溃了。 “未找到测试。这通常意味着您的测试类不是您的测试运行程序所期望的形式。”对你完全没有帮助 =)

关于android - 从 IDE 运行测试有效,但不能从命令行运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647307/

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