Android 本地测试与仪器测试 [英] Android Local Tests vs Instrumented Test

查看:54
本文介绍了Android 本地测试与仪器测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查我的 API 在单元测试中是否可用,以确保它以 200 响应.

I'm trying to check if my API is available within a unit test, to be sure that it responds with 200.

现在,我的问题是我不确定何时使用本地测试以及何时必须使用Android Instrumentation Tests.我知道我必须使用 Instrumented Tests 进行 UI 测试,但如何测试端点?

Now, my problem is that I'm not really sure when to use Local Test and when I have to use Android Instrumentation Tests. I know that I have to use Instrumented Tests for UI testing but how to test the endpoint?

我使用 Retrofit2 进行通信.并尝试使用本地测试以两种方式测试 Endpoint.

I use Retrofit2 for communication. And tried to test Endpoint in 2 ways with Local Tests.

示例 1(同步,不起作用)

Example 1 (synchronous, does not work)

public class EndpointTest {

    EndpointApi api;
    SimpleInjection simpleInjection;

    @Before
    public void setUp() {
        simpleInjection = new SimpleInjection();
        api = simpleInjection.getEndpointApi();
    }

    @Test
    public void endpoint_1_isAvailable() {
        Call<ApiResponse> rootCall = api.getRoot();
        try {
            int reponseCode = rootCall.execute().code();
            Assert.assertEquals(200, reponseCode);

        } catch (IOException e) {
            Assert.fail();
        }
    }         
}

示例 2(异步,确实有效)

Example 2 (asynchronous, does work)

public class EndpointTest {

    EndpointApi api;
    SimpleInjection simpleInjection;

    @Before
    public void setUp() {
        simpleInjection = new SimpleInjection();
        api = simpleInjection.getEndpointApi();
    }

    @Test
    public void endpoint_2_isAvailable() {
    Call<ApiResponse> rootCall = api.getRoot();

        rootCall.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                Assert.assertEquals(200, response.code());
            }

            @Override
            public void onFailure(Call<ApiResponse> call, Throwable t) {
                Assert.fail();
            }
        });
    }         
}

我是否必须将 Android Instrumentation Test 用于异步模式?

Do I have to use Android Instrumentation Test for asynchronous mode?

推荐答案

决定是在开发机器上的本地 JVM 上运行测试还是在 Android 设备/模拟器上运行测试,并不取决于您的代码是同步的还是同步的不是.通常您只想在本地运行单元测试,因为它要快得多并且允许您使用 TDD.您的测试执行网络请求,因此它们不是单元测试,因为它们依赖于服务器 - 它们是集成测试.最好在 Android 设备上运行集成测试以获得更好的反馈.

The decision of whether to run your tests on a local JVM on your development machine or on an Android device/emulator is not based on whether your code is synchronous or not. Usually you would only want to run unit tests locally, as it's a lot faster and allows you to use TDD. Your tests do network requests, so they're not unit tests per say, since they have the dependency on the server - they are integration tests. It's preferable to run integration tests on an Android device to get better feedback.

这篇关于Android 本地测试与仪器测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆