android 命令行 test,android – 从IDE运行测试但无法从命令行运...

我写过单元测试,仪器测试和浓缩咖啡测试.我还使用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 /

\n android.support.test.services.shellexecutor.ShellMain am instrument -w -e \n targetInstrumentation

mypackage/myTestRunner \n android.support.test.orchestrator/.AndroidTestOrchestrator'

然后输出等于:

Time: 0

OK (0 tests)

有人可以向我解释我做错了什么吗?我无法理解为什么在命令行中没有任何作用,但在Android Studio中一切正常.

/编辑

我的CustomRunner:

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() {

@Override

public Scheduler apply(Scheduler scheduler) throws Exception {

return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);

}

});

RxJavaPlugins.setComputationSchedulerHandler(new Function() {

@Override

public Scheduler apply(Scheduler scheduler) throws Exception {

return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);

}

});

RxJavaPlugins.setNewThreadSchedulerHandler(new Function() {

@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: ""]

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值