标签: android-instrumentation

Android检测测试未发现任何测试

我是Android仪表单元测试的新手.我有一个看起来像这样的简单测试.这是仪器化测试; 在我真正编写一个实际的仪器测试之前,我只想测试一个简单的测试:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class AddressBookKeepingInstrumentedTest {


    public static final String TEST_STRING = "This is a string";
    public static final long TEST_LONG = 12345678L;

    @Test
    public void test_simple() {
        assertEquals(2,1+1);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到以下错误:

junit.framework.AssertionFailedError: No tests found in com.example.myfirstapp.AddressBookKeepingInstrumentedTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)

Tests ran to completion.
Run Code Online (Sandbox Code Playgroud)

Gradle构建在此之前成功通过.任何想法为什么会这样?

android unit-testing android-instrumentation

10
推荐指数
2
解决办法
5843
查看次数

使用Espresso测试RxJava2并在suscribeOn时获取空指针异常

Android Studio 3.0 Beta2
Run Code Online (Sandbox Code Playgroud)

我正在测试使用RxJava2获取端点列表.该应用程序正常运行时工作正常.但是,当我使用espresso测试时,我尝试使用时会得到一个空指针异常subscribeOn(scheduler).对于调度程序,我使用trampoline()for subscribeOnobserveOn两个注入.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.reactivex.Observable io.reactivex.Observable.subscribeOn(io.reactivex.Scheduler)' on a null object reference
Run Code Online (Sandbox Code Playgroud)

为了测试使用浓缩咖啡的RxJava2,有什么我应该做的,这对于subscribeOnobserveOn

@Singleton
@Component(modules = {
        MockNetworkModule.class,
        MockAndroidModule.class,
        MockExoPlayerModule.class
})
public interface TestBusbyBakingComponent extends BusbyBakingComponent {
    TestRecipeListComponent add(MockRecipeListModule mockRecipeListModule);
}
Run Code Online (Sandbox Code Playgroud)

这是我的课程

public class RecipeListModelImp
        implements RecipeListModelContract {

    private RecipesAPI recipesAPI;
    private RecipeSchedulers recipeSchedulers;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Inject
    public RecipeListModelImp(@NonNull RecipesAPI recipesAPI, @NonNull RecipeSchedulers recipeSchedulers) {
        this.recipesAPI …
Run Code Online (Sandbox Code Playgroud)

android unit-testing nullpointerexception android-espresso android-instrumentation

10
推荐指数
1
解决办法
1238
查看次数

AndroidJUnitRunner和AndroidJunit4有什么区别?

我是Android测试世界的新手,单元测试的样本提到了AndroidJunit4跑步者的使用.我偶然发现了一位名叫的跑步者AndroidJUnitRunner.现在,我看到了这两个类的文档,乍一看,似乎差异在于AndroidJunit4支持JUnit4,特别是在AndroidJUnitRunner支持JUnit3和JUnit4时,但是如果你研究类结构和可用的方法,实际上有一个巨大的区别这两位选手.

例如,AndroidJUnitRunner从以下类扩展:

在此输入图像描述

并且,AndroidJunit4从,延伸,

在此输入图像描述

那么,两位选手之间有什么区别?我最终应该使用哪名选手?

junit android android-instrumentation

10
推荐指数
1
解决办法
3096
查看次数

如何授予android检测测试的权限?

我有一个读取短信的应用程序.该应用程序在调试时工作正常,但在使用android检测测试时,它会抛出以下错误

java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例

@RunWith(AndroidJUnit4.class)
public class SmsFetcherTest {

   @Test
   public void fetchTenSms() throws Exception {
      // Context of the app under test.
      Context appContext = InstrumentationRegistry.getContext();

      //   Fails anyway.
      //   assertTrue(ContextCompat.checkSelfPermission(appContext,
      //     "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED);

      List<Sms> tenSms = new SmsFetcher(appContext)
              .limit(10)
              .get();

      assertEquals(10, tenSms.size());
   }
}
Run Code Online (Sandbox Code Playgroud)

我是仪器化测试的新手.这是正确的方法吗?

或者我错过了什么?

testing permissions android android-studio android-instrumentation

10
推荐指数
2
解决办法
2712
查看次数

等到IdleResource可以看到视图

我正在使用Espresso 2.2编写仪器测试.

流程我想测试:

  1. radioButton点击测试
  2. onClick启动对API的请求
  3. 每次不同的时间后我收到回复
  4. 肯定响应触发在活动中调用的接口方法
  5. onRequestSuccess我在屏幕上创建名为vSetupAmount的其他面板可见

我想在点击radioButton后注册IdleResource,等待vSetupAmount变为可见.但我不能让它发挥作用.请告诉我我做错了什么.

我写过这样的IdleResource:

public class AmountViewIdlingResource implements IdlingResource {

    private CountriesAndRatesActivity activity;
    private ResourceCallback callback;

    private SetupAmountView amountView;

    public AmountViewIdlingResource(CountriesAndRatesActivity activity) {
        this.activity = activity;

        amountView = (SetupAmountView) this.activity.findViewById(R.id.vSetupAmount);
    }

    @Override public String getName() {
        return "Amount View idling resource";
    }

    @Override public boolean isIdleNow() {
        callback.onTransitionToIdle();
        return amountView.getVisibility() == View.VISIBLE;
    }

    @Override public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
        this.callback = resourceCallback;
    }

}
Run Code Online (Sandbox Code Playgroud)

所以我将活动传递给IdleResource,链接视图与变量.我知道IdleResource将不会让测试通过,直到isIdleNow()返回值true.因此,如果视图是View.GONE,那么它将不会更进一步.

它在测试中看起来如何:

    // click of radioButton picked from radioGroup
    onView(withId(rgDeliveries.getChildAt(id).getId())).perform(scrollTo(), …
Run Code Online (Sandbox Code Playgroud)

android android-espresso android-instrumentation

9
推荐指数
1
解决办法
1万
查看次数

拖放浓缩咖啡

是否可以通过Espresso执行拖放操作?我需要向下移动一个视图(直线)以接受我的自动化测试中的某些条件.

android android-espresso android-instrumentation

9
推荐指数
1
解决办法
3663
查看次数

Android工具测试用于库模块覆盖

我继承了一个android项目来设置代码覆盖率.没有为Android做太多事情,而且几乎没有做太多事情,我开始寻找一个有用的教程.令人惊讶的是,前几个教程非常有用,我能够包含jacoco gradle插件并启用代码覆盖.使用jenkins我甚至生成了一份报道报告.到目前为止一切都很好.

然而,在看到报告时,我闻到了一些可疑的东西.测试与覆盖率似乎太小了.进一步的调查揭示了罪魁祸首.

测试本身更多地是作为功能而不是单元编写的.那没关系.但是,项目库的模块中没有测试.相反,库测试是在gui模块中编写的(因为这是使用库的地方).

因此,即使测试涵盖了大多数库功能,也仅为gui模块的内容生成覆盖.

项目

- 桂模块

----桂源

----所有的测试

- 图书馆模块

----图书馆来源

不,我一直在寻找一个有效的解决方案.不幸的是,我能够找到的是如何将单元和集成.exec测试覆盖结果组合成一个报告(或其他基于单元测试的解决方案 - 其中没有一个适用于仪器测试).

我需要的是基于Gui模块测试从库模块生成源代码.

因为我在这里的黑暗中磕磕绊绊,甚至是那样的东西,远远可能吗?

code-coverage jacoco android-gradle-plugin android-instrumentation

9
推荐指数
1
解决办法
1302
查看次数

如何使用 Gradle 命令在 Android 中运行单个测试类?

在我的 Android 应用程序中,我有几个测试类。如果我运行以下命令,./gradlew connectedAndroidTest它将运行 android test 文件夹中的所有测试用例并为所有测试类生成测试报告,但我需要测试特定的测试类并为我尝试使用以下命令的测试类生成报告:

./gradlew test --tests com.login.user.UserLoginTest
Run Code Online (Sandbox Code Playgroud)

上面的命令抛出以下异常

FAILURE: Build failed with an exception.

* What went wrong:
Problem configuring task :app:test from command line.
> Unknown command-line option '--tests'.

* Try:
Run gradlew help --task :app:test to get task usage details. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at …
Run Code Online (Sandbox Code Playgroud)

android unit-testing android-gradle-plugin android-instrumentation android-junit

9
推荐指数
1
解决办法
5851
查看次数

使用命令提示符使用参数运行Instrumented Test

我有一个InstrumentedTest

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext(String groupName) {
Context appContext = InstrumentationRegistry.getTargetContext();
        UiDevice device=UiDevice.getInstance(getInstrumentation());
...
...
  }
}
Run Code Online (Sandbox Code Playgroud)

我想用adb shell命令执行它.但我要传递groupName方法的参数值useAppContext(String groupName)

我正在使用命令

adb shell am instrument -w -r   -e debug false -e class 'com.<package_name>.ExampleInstrumentedTest' com.<package_name>.test/android.support.test.runner.AndroidJUnitRunner  
Run Code Online (Sandbox Code Playgroud)

但是如何将方法参数作为参数传递给在命令提示符下运行的命令?

android android-testing android-instrumentation

9
推荐指数
1
解决办法
1079
查看次数

收到“应用程序中未找到撰写视图。您的活动已恢复吗?”

我有一个Composable有一个TextButtonText将显示P当前方向是否为纵向,L否则。单击Button会将方向更改为横向,(因此之后,应该将文本从 更改PL

这是可组合的

@Composable
fun MyApp() {
    val currentOrientation = LocalConfiguration.current.orientation
    val orientation = if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            "P"
        } else {
            "L"
        }
    val activity = LocalContext.current as Activity
    Column {
        Text(text = orientation)
        Button(onClick = {
            // change orientation to landscape
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }) {
            Text(text = "DO IT")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我测试的方法

@get:Rule
val composeRule = createComposeRule() …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-instrumentation android-jetpack-compose

9
推荐指数
1
解决办法
4198
查看次数