HarmonyOS 鸿蒙Next UI测试 怎么在feature模块写单体

HarmonyOS 鸿蒙Next UI测试 怎么在feature模块写单体 目前写Ui单体 测试套不能单独运行,必须全部运行。搞不懂到底要怎么写才规范,请给出一个好的方案,有一个demo提供参考是最好的

2 回复

您好!可以通过自定义测试用例运行任务,使用过滤条件筛选待运行的测试用例,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/ide-instrument-test-V13#section925320179314

示例代码如下:

在用例编写时,通过配置it的第二个入参,为每个用例添加过滤参数。此参数用于为测试用例添加标注,不添加则参数默认为0表示未被标注

import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Level } from '@ohos/hypium'

export default function DemoTest() {
  describe('DemoTest', () => {
    // Defines a test suite. Two parameters are supported: test suite name and test suite function.
    beforeAll(() => {
      // Presets an action, which is performed only once before all test cases of the test suite start.
      // This API supports only one parameter: preset action function.
    })
    beforeEach(() => {
      // Presets an action, which is performed before each unit test case starts.
      // The number of execution times is the same as the number of test cases defined by **it**.
      // This API supports only one parameter: preset action function.
    })
    afterEach(() => {
      // Presets a clear action, which is performed after each unit test case ends.
      // The number of execution times is the same as the number of test cases defined by **it**.
      // This API supports only one parameter: clear action function.
    })
    afterAll(() => {
      // Presets a clear action, which is performed after all test cases of the test suite end.
      // This API supports only one parameter: clear action function.
    })
    it('assertEqual', Level.LEVEL0, () => {
      // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
      let a = 'test'
      // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
      expect(a).assertEqual('test')
    })
    it('assertEqual1', Level.LEVEL1, () => {
      let a = 'test2'
      expect(a).assertEqual('test1')
    })
  })
}

可以通过自定义测试任务,添加testarg,选择Level为0或者1,过滤运行assertEqual和assertEqual1任务

更多关于HarmonyOS 鸿蒙Next UI测试 怎么在feature模块写单体的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中进行Next UI测试时,如果你需要在feature模块编写单体测试(Unit Test),通常你会利用HarmonyOS提供的测试框架来完成。以下是一个简要的步骤说明:

  1. 创建测试类:在你的feature模块中,创建一个新的测试类。这个类通常放在src/test/java目录下,并且命名上一般会与被测试的类相关联。

  2. 引入测试依赖:确保你的build.gradle文件中已经包含了测试相关的依赖。HarmonyOS通常使用JUnit或其他兼容的测试框架。

  3. 编写测试方法:在测试类中,使用注解(如@Test)标记你的测试方法。这些方法将包含具体的测试逻辑,用于验证feature模块中的代码是否按预期工作。

  4. 运行测试:通过IDE或命令行工具运行测试,查看测试结果。HarmonyOS的开发工具通常支持直接运行和查看单元测试的结果。

  5. 处理测试失败:如果测试失败,根据错误信息调整你的代码或测试逻辑,直到所有测试都通过。

请注意,具体的实现细节可能会因项目结构和配置的不同而有所差异。如果以上步骤未能解决你的问题,可能是由于特定项目配置或代码问题导致的。此时,你可以参考HarmonyOS的官方文档或联系官网客服获取进一步帮助。官网地址是:https://www.itying.com/category-93-b0.html

回到顶部