如何编写简单的测试用例以测试android中的Login Api?

时间:2019-07-11 11:17:39

标签: android unit-testing junit

我一直在尝试为APIS编写测试用例,到目前为止,我只写了unit test cases个功能电子邮件验证,仅用于示例。

我实际想要的内容如下-

我想为登录API 编写一个Android测试用例,以接受emailpassword.

我已经看了很多教程,但是每个链接都说要包含很多依赖,例如expresso,mockito等。

因此,没有什么更简单的过程可以将其直接集成到我的预构建应用程序中。

1 个答案:

答案 0 :(得分:1)

要为Login API(而不是注册API)创建测试用例,只需考虑API返回成功或失败的可能性。用户将提供正确的电子邮件和密码,或者提供不正确的电子邮件和密码,您的api将相应地响应。

要进行测试,您可以潜在地创建以下4个测试用例:

  • 有效电子邮件,有效密码
  • 错误的电子邮件,正确的密码(用于与该密码相关联的用户)
  • 正确的电子邮件,错误的密码
  • 错误的电子邮件,错误的密码。

好像您没有在测试注册API一样,而是要按照问题所述专门测试登录api,因此无需测试用户是否根据业务逻辑输入了有效的电子邮件或密码数据。 / p>

作为一个简单的示例,您可以执行以下操作:

@Test
public void testExampleWithCorrectValues() {
    String validEmail = "email";
    String validPassword = "password";

    boolean responseOfExecutingYourApiWithCorrectValues = how you get a response from the api

    Assert.assertEquals(true, responseOfExecutingYourApiWithCorrectValues);

}

@Test
public void testExampleWithIncorrectEmail() {
    String invalidEmail = "email1";
    String validPassword = "password";

    boolean responseOfExecutingYourApiWithIncorrectValues = how you get a response from the api

    Assert.assertEquals(false, responseOfExecutingYourApiWithIncorrectValues);

}


@Test
public void testExampleWithIncorrectPassword() {
    String validEmail = "email";
    String invalidPassword = "password1";

    boolean responseOfExecutingYourApiWithCorrectValues = how you get a response from the api

    Assert.assertEquals(false, responseOfExecutingYourApiWithCorrectValues);

}

@Test
public void testExampleWithIncorrectValues() {
    String invalidEmail = "email1";
    String invalidPassword = "password1";

    boolean responseOfExecutingYourApiWithIncorrectValues = how you get a response from the api
    Assert.assertEquals(false, responseOfExecutingYourApiWithIncorrectValues);
}

但是,您必须注意,您的测试随后将达到您要运行的实际API(进行服务调用),这是一种不好的做法,这就是为什么人们告诉您使用Mockito和其他框架进行模拟的原因您的api调用或响应