1. 程式人生 > >Android相容性測試工具Spoon

Android相容性測試工具Spoon

摘自: http://www.yeetrack.com/?p=877

簡介

android眾多的版本和機型給android app測試工作帶來不小的挑戰。Spoon通過將case分散式地執行、將執行結果更友好地展示出來,從而簡化android app的測試工作。

Spoon沒有開發新的測試框架,而是讓已經存在的測試框架更有效。現在的測試框架,一般都是通過測試apk來驅動被測apk,Spoon可以讓這些case在多臺裝置上同時執行。一旦測試結束,Spoon就是生成一份html報表,來展示每臺裝置上的執行結果。

Spoon會檢測到adb devices命令中顯示的全部裝置,並在這些裝置上執行case。所以我們在使用spoon時,可以插入不同的手機、平板、或者不同配置的模擬器。

插入的裝置越多、越雜,那麼最終的測試結果展示的資訊就越多。

截圖

除了單純地跑case,Spoon還可以在case執行中,進行截圖,並在結果中進行展示。在最後的結果中,我們可以看到不同裝置跑一個case時的截圖,來測試app的相容性。

使用Spoon的截圖功能,必須在被測app中引入spoon-clientjar包,在我們的測試程式碼中呼叫screenshot方法進行截圖,這些截圖會被打上標籤。

    Spoon.screenshot(activity, "initial_state");
    //這裡編寫登陸程式碼
    Spoon.screenshot(activity, "after_login");

程式碼裡指定的tag用來命名截圖,以便在測試相容性的時候進行不同裝置間的橫向比較。
我們也可以連貫地瀏覽每臺裝置上的截圖,來檢視執行過程。

下載

<dependency>
    <groupId>com.squareup.spoon</groupId>
    <artifactId>spoon-client</artifactId>
    <version>(insert latest version)</version>
</dependency>

執行

Spoon既可以單獨執行,又可以整合到maven裡,作為maven命令的一部分來執行。

單獨執行,需要被測apk包和測試apk包,執行下面的命令即可:

    java -jar spoon-runner-1.0.0-jar-with-dependencies.jar \
        --apk example-app.apk \
        --test-apk example-tests.apk

執行結果,預設會放在當前目錄的spoon-output資料夾下。當然還有一些其他的引數,具體如下:

    Options:
        --apk               被測apk
        --fail-on-failure   Non-zero exit code on failure
        --output            結果路徑
        --sdk               android sdk路徑
        --test-apk          測試apk
        --title             Execution title
        --class-name        Test class name to run (fully-qualified)
        --method-name       Test method name to run (must also use --class-name)
        --no-animations     Disable animated gif generation
        --size              Only run test methods annotated by testSize (small, medium, large)
        --adb-timeout       Set maximum execution time per test in seconds (10min default)

如果使用maven來執行,有個maven外掛可以用。在pom.xml中加入下面程式碼:

    <plugin>
        <groupId>com.squareup.spoon</groupId>
        <artifactId>spoon-maven-plugin</artifactId>
        <version>(insert latest version)</version>
    </plugin>

這個外掛會自動尋找被測試的apk包。這個apk包通常還會以jar包的形式在dependency中進行宣告。

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>example-app</artifactId>
        <version>${project.version}</version>
        <type>jar</type>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>example-app</artifactId>
        <version>${project.version}</version>
        <type>apk</type>
        <scope>provided</scope>
    </dependency>

配置了上面的外掛,我們可以通過mvn spoon:run來執行。執行結果放在target/spoon-output/資料夾。如果想手動指定test class,可以在命令中新增引數-Dspoon.test.class=fully.qualified.ClassName。如果想手動指定test method,可以在命令中再新增引數-Dspoon.test.method=testAllTheThings