新增 GoogleTest (GTest)

如果你是 Android 平台開發新手,可能會看到完整內容 新增 GTest 二進位檔 (有時也稱為「原生」檔案) 測試),藉此示範相關典型的工作流程。適用對象 GTest 架構的其他相關資訊 C++,請參閱 GTest 專案 網站

本指南使用 Hello World GTest 舉例來說建議您詳細閱讀這些程式碼,進一步瞭解 才能繼續操作

決定來源位置

一般來說,您的團隊已有一定的檢查地點模式 以及要新增測試的位置大多數團隊都擁有單一 Git 存放區,或 也可以與其他團隊共用,但具有專屬的子目錄 元件原始碼

假設元件來源的根位置位於 <component source root>,大多數元件底下都有 srctests 資料夾,而 其他檔案,例如 Android.mk (或細分為其他 .bp) 檔案)。

由於您要新增的是全新的測試,因此您可能需要建立 tests 目錄 (位於元件 src 旁邊),並填入內容。

在某些情況下,您的團隊可能會在 tests 下使用進一步的目錄結構 原因是必須將不同的測試套件封裝至個別二進位檔中。 在此情況下,您需要在 tests 底下建立新的子目錄。

為了便於說明,以下為含有單一標記的元件的典型目錄大綱 tests 資料夾:

\
 <component source root>
  \-- Android.bp (component makefile)
  \-- AndroidTest.xml (test config file)
  \-- src (component source)
  |    \-- foo.cpp
  |    \-- ...
  \-- tests (test source root)
      \-- Android.bp (test makefile)
      \-- src (test source)
          \-- foo_test.cpp
          \-- ...

以下是含有多個測試來源的元件的一般目錄大綱 目錄:

\
 <component source root>
  \-- Android.bp (component makefile)
  \-- AndroidTest.xml (test config file)
  \-- src (component source)
  |    \-- foo.cpp
  |    \-- ...
  \-- tests (test source root)
      \-- Android.bp (test makefile)
      \-- testFoo (sub test source root)
      |   \-- Android.bp (sub test makefile)
      |   \-- src (sub test source)
      |       \-- test_foo.cpp
      |       \-- ...
      \-- testBar
      |   \-- Android.bp
      |   \-- src
      |       \-- test_bar.cpp
      |       \-- ...
      \-- ...

無論結構為何,您都會填入 tests 目錄或 新建立的子目錄,其中包含與 native 中的類似檔案 。以下各節將說明 個別檔案的詳細資料

原始碼

請參閱 Hello World GTest 例如,

該範例的原始碼已加註如下:

#include <gtest/gtest.h>

GTest 的標頭檔案。系統會自動納入 include 檔案依附元件 在 makefile 中使用 BUILD_NATIVE_TEST 解決。

#include <stdio.h>

TEST(HelloWorldTest, PrintHelloWorld) {
    printf("Hello, World!");
}

GTest 是使用 TEST 巨集編寫:第一個參數是測試案例 第二個是測試名稱連同測試二進位檔名稱一起建立 下列階層:

<test binary 1>
| \-- <test case 1>
| |   \-- <test 1>
| |   \-- <test 2>
| |   \-- ...
| \-- <test case 2>
| |   \-- <test 1>
| |   \-- ...
| \-- ...
<test binary 2>
|
...

如要進一步瞭解如何使用 GTest 編寫測試,請參閱 GTest 說明文件

簡易設定檔

每個新的測試模組都必須有設定檔 建構系統,其中包含模組中繼資料、編譯時間依附元件和封裝 操作說明。在多數情況下,Soong 型的 Blueprint 檔案選項為 而負責任的 AI 技術做法 有助於達成這項目標詳情請參閱「簡易測試設定」。

複雜設定檔

如要改用貿易聯盟,請撰寫測試設定 檔案。

測試設定可以指定特殊的裝置設定選項和預設設定 提供測試類別。

在本機建構及測試

最常見的用途是 Atest

如果是需要層級大幅自訂的較複雜案例,請遵循 檢測操作說明