鸿蒙L0设备测试用例开发指导

上一篇 / 下一篇  2024-07-04 11:35:56

  1 框架简介
  1.1 L0框架简介
  L0设备使用的测试框架是hctest,其代码位于test/xts/tools/lite/hctest目录下。此框架支持使用C语言编写测试用例,是在开源测试框架unity的基础上进行增强和适配。
  2 用例编写
  2.1 目录规范
  2.1.1 目录规划
  L0的测试用例存放在各子系统部件模块的根目录下,例如utils/native/lite/test。
  2.1.2 目录结构
  部件根目录
      ├── test
      │   ├── kv_store_hal
      │   │   ├── BUILD.gn
      │   │   ├── Test.tmpl
      │   │   └── src
      │   │       └── kvstore_func_test.c
      │   └── kv_store_posix
      │       ├── BUILD.gn
      │       ├── Test.json
      │       └── src
      │           └── KvStoreTest.cpp
  备注:hal归档L0自动化用例,posix归档L1自动化用例
  2.2 用例编写
  2.2.1 引用测试框架
  #include “hctest.h”
  2.2.2 定义测试信息
  使用宏定义LITE_TEST_SUIT定义子系统,模块,测试套件名称
  /**
   * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
   * @param        : subsystem name is utils
   * @param        : module name is kvStore
   * @param        : test suit name is KvStoreFuncTestSuite
   */
  LITE_TEST_SUIT(utils, kvStore, KvStoreFuncTestSuite);
  2.2.3 定义Setup和TearDown
  1、命名方式:测试套件名称+Setup,测试套件名称+TearDown;2、Setup和TearDown函数必须存在,函数体可以为空
  /**
   * @tc.setup     : setup for all testcases
   * @return       : setup result, TRUE is success, FALSE is fail
   */
  static BOOL KvStoreFuncTestSuiteSetUp(void)
  {
     return TRUE;
  }
  /**
   * @tc.teardown  : teardown for all testcases
   * @return       : teardown result, TRUE is success, FALSE is fail
   */
  static BOOL KvStoreFuncTestSuiteTearDown(void)
  {
     return TRUE;
  }
  2.2.4 编写测试用例
  包括三个参数:测试套件名称,测试用例名称,用例级别
  /**
   * @tc.number    : SUB_UTILS_KV_STORE_0100
   * @tc.name      : UtilsSetValue parameter legal test
   * @tc.desc      : [C- SOFTWARE -0200]
   */
  LITE_TEST_CASE(KvStoreFuncTestSuite, KvStoreSetValue001, Function | MediumTest | Level1)
  {
     char key[] = "rw.sys.version";
     char value[] = "Hello world !";
     int ret = UtilsSetValue(key, value);
     TEST_ASSERT_EQUAL_INT(0, ret);
     ret = UtilsDeleteValue(key);
     TEST_ASSERT_EQUAL_INT(0, ret);
  };
  2.2.5 注册测试套件
  RUN_TEST_SUITE(KvStoreFuncTestSuite);
  2.2.6 完整示例说明
  /*
   * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
     *
   * http://www.apache.org/licenses/LICENSE-2.0
      *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  #include "ohos_types.h"
  #include <securec.h>
  #include "hctest.h"
  #include "utils_config.h"
  /**
   * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
   * @param        : subsystem name is utils
   * @param        : module name is kvStore
   * @param        : test suit name is KvStoreFuncTestSuite
   */
  LITE_TEST_SUIT(utils, kvStore, KvStoreFuncTestSuite);
  /**
   * @tc.setup     : setup for all testcases
   * @return       : setup result, TRUE is success, FALSE is fail
   */
  static BOOL KvStoreFuncTestSuiteSetUp(void)
  {
     return TRUE;
  }
  /**
   * @tc.teardown  : teardown for all testcases
   * @return       : teardown result, TRUE is success, FALSE is fail
   */
  static BOOL KvStoreFuncTestSuiteTearDown(void)
  {
     return TRUE;
  }
  /**
   * @tc.number    : SUB_UTILS_KV_STORE_0100
   * @tc.name      : UtilsSetValue parameter legal test
   * @tc.desc      : [C- SOFTWARE -0200]
   */
  LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue001, Function | MediumTest | Level1)
  {
     char key[] = "rw.sys.version";
     char value[] = "Hello world !";
     int ret = UtilsSetValue(key, value);
     TEST_ASSERT_EQUAL_INT(0, ret);
     ret = UtilsDeleteValue(key);
     TEST_ASSERT_EQUAL_INT(0, ret);
  };
  /**
   * @tc.number    : SUB_UTILS_KV_STORE_0200
   * @tc.name      : UtilsSetValue parameter legal test using key with underline
   * @tc.desc      : [C- SOFTWARE -0200]
   */
  LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue002, Function | MediumTest | Level1)
  {
     char key[] = "rw.sys.version_100";
     char value[] = "Hello world !";
     int ret = UtilsSetValue(key, value);
     TEST_ASSERT_EQUAL_INT(0, ret);
     ret = UtilsDeleteValue(key);
     TEST_ASSERT_EQUAL_INT(0, ret);
  };
  RUN_TEST_SUITE(KvStoreFuncTestSuite);
  2.3 编译文件
  在每个测试套件目录下新建BUILD.gn文件,用于指定构建目标的名称,依赖的头文件,依赖的库文件等。
  # Copyright (c) 2020-2021 Huawei Device Co., Ltd.
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  import("//test/xts/tools/lite/build/suite_lite.gni")
  hctest_suite("KvStoreTest") {
    suite_name = "acts"
    sources = [ "src/kvstore_func_test.c" ]
    include_dirs = [
      "src",
      "//utils/native/lite/include",
      "//base/iot_hardware/peripheral/interfaces/kits",
    ]
    cflags = [ "-Wno-error" ]
  }
  2.4 配置文件
  在每个测试套件目录下新建Test.tmpl文件,内容如下:
  {
      "description": "Config for $module test cases",
      "environment": [
          {
              "type": "device",
              "label": "wifiiot"
          }
      ],
      "kits": [
          {
              "type": "DeployKit",
              "timeout": "20000",
              "burn_file": "$subsystem/$module.bin"
          }
      ],
      "driver": {
          "type": "CTestLite"
      }
  }
  2.5 配置编译目标
  进入test/developertest/src/core/build目录,在build_lite_testcases.sh文件中按如下格式添加构建目标路径:
  BUILD_TARGETS="${BUILD_TARGETS},//utils/native/lite/test/kv_store_hal:KvStoreTest"
  备注:必须是完成的构建目标路径
  3 用例编译
  3.1 编译用例
  1、编译所有用例
  ./test/developertest/src/core/build/build_lite_testcases.sh product=wifiiot_hispark_pegasus kernel=liteos_m
  2、编译指定目标的用例
  ./test/developertest/src/core/build/build_lite_testcases.sh product=wifiiot_hispark_pegasus kernel=liteos_m target=//utils/native/lite/test/kv_store_hal:KvStoreTest
  3.2 输出目录
  测试用例输出路径为out/hispark_pegasus/wifiiot_hispark_pegasus/test,其目录结构如下:
  test
    ├── module_info.json
    ├── test_component.json
    └── unittest
        └── utils
            ├── KvStoreTest.bin
            └── KvStoreTest.json

TAG: 测试用例

 

评分:0

我来说两句

Open Toolbar