【问题标题】:Unit Testing (Android) - Common scenarios to test单元测试 (Android) - 常见的测试场景
【发布时间】:2016-10-21 14:18:49
【问题描述】:

我在意图上看到了不同的 UT 示例,但找不到为我的应用集成 UT 的实用方法。

请考虑这个简单的活动,它可以读取 JSON 提要并发布数据。

public class MainActivity extends Activity {

   String dataArray;

   /** Called when the activity is first created. */
   public void onCreate() {
        //Get json data from the server
        getAPIdata();
   }

    /** get data json from the server */
    public getAPIdata(Server Response){
        //Parse the json output
        //Validate 
        //populate data array
        return dataArray;
    }


    /** use populated data array and display */
    public setListView(dataArray){
        //use the data array and set a list view to display the results

    }


    /** post data to create a record */
    public postDataToServer(Parameters){
        //validate input data
        //send a post request to the server
        //if success, print a success message on the screen
        //else print the error
    }


}

这是我主要在我的 Android 应用程序中使用的结构。我没有太多的业务逻辑。

让我再总结一下。

  1. 检索 JSON 并显示结果

  2. 表单输入验证

  3. 将数据发布到 API 以创建/编辑记录

所以现在您可以看到这个应用程序主要处理 CRUD 操作。我的问题是你们是:

如何在此应用程序中使用 UT? (或者这里不需要UT,因为我没有太多的业务逻辑?)

我们将不胜感激任何实用的建议。

提前致谢!

【问题讨论】:

  • Step1:关注点分离。活动不应该管理下载 json 数据。在适当的地方将逻辑拆分为不同的类
  • 实际上,正如你所说,我将它放在一个单独的类中。没有发布完整的代码,因为它会很长

标签: java android unit-testing tdd automated-tests


【解决方案1】:

考虑到您必须阅读 JSON 提要并在此活动中显示结果(或其他内容),我将编写集成测试来测试您的业务逻辑、服务器通信、JSON 解析器等,并编写一个仪器测试来直接测试您的 Activity。

您可以在Android documentation 中查看一些使用 Espresso 框架测试您的 UI 的示例,也可以使用 JUnit 框架编写您的集成测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2011-03-16
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多