【问题标题】:TDD of android applications安卓应用的TDD
【发布时间】:2012-08-31 12:04:02
【问题描述】:

我需要一些与 Android 应用程序的 TDD 相关的建议和指导

我正在开发一个复杂的 android 应用程序(用于网络游戏的自动机器人播放器)。现在我有一个试点,在继续开发之前,我想为它做完整的 TDD。

此应用程序的主要目的是作为与 http 服务器通信的后台服务运行 - 它会发送带有 json 内容的 http 请求

它有两种运行模式:

- service mode - it will run in background- read some data configuration from database and communicate with the server 
- GUI mode - communication with server on demand + configuration for running in service mode.

GUI 层被设计为 MVP,因此在一个模块(MVP 单元)中完成的所有业务都与视图分离,因此,所有应用程序逻辑都是“非 android 依赖”。

我有几个大层需要对其进行单元测试:

- domain data access : 
      database storage (ormlite)
      android specific storage - shared prefs.., 
      files
- server communication - http client (spring android rest template)
                         http content conversion (gson)

- background services - android services
- GUI - activities
- app business - android independent - algorithms, computations, ...

总之,需要 TDD - 对每一层单独进行测试,还需要对完整流程进行集成测试。

包括测试:

  - database DAO tests
  - http client requests 
  - GSON conversion TDDs
  - business logic - simple tests for methods
  - unit testsfor running and scheduled background services
  - activity unit testing
  - test suites (service + DAO + json conversion + http requests)

我的第一个问题是哪种 TDD 框架最适合我的需求?我应该使用模拟吗?

请给我一些指示。谢谢

【问题讨论】:

    标签: android unit-testing tdd


    【解决方案1】:

    首先,用于自动测试:junit


    对于流畅的断言,您可以查看fest-assertions。他们提供了很多例子,here。但是请在String 上享受这个:

     assertThat("Frodo").startsWith("Fro").endsWith("do").hasSize(5);
    

    不仅和谐,还能提高您的工作效率。


    由于您想编写单元测试,因此您必须编写 mock 对象。当然,不用任何框架也可以,不过是非常tedious

    就个人而言,我是Mockito 的忠实粉丝。它的api很棒而且非常流畅。此外,您可以使用BDDMockito 静态方法以BDD 的方式编写测试。看:

    //given  
    given(seller.askForBread()).willReturn(new Bread());
    //when
    Goods goods = shop.buyBread();
    //then
    assertThat(goods, containBread());
    

    有时您会依赖无法模拟的类,例如 final class,如 BluetoothAdapter。在这些情况下(并且仅在这些情况下),您可以使用PowerMock

    看看这些东西:

    还有一本书:Android Application Testing Guide

    【讨论】:

      【解决方案2】:

      由于您提到了集成测试,您可能还对 RoboGherk 作为 Android 的自动化验收测试框架感兴趣,最近是 announced by my friend at LeanDog

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-19
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多