export default function abilityTest() {
describe('ActsAbilityTest', () => {
// 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('assertContain', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
let a = 'abc';
let b = 'b';
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertContain(b);
expect(a).assertEqual(a);
})
})
}
```
3. **断言**
**断言功能列表:**
1. **引入uitest框架**
```
import {Driver,ON,Component,Uiwindow,MatchPattern} from '@ohos.UiTest'
```
2. **引入ability**
```
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
```
3. **添加启动应用准备**
```
beforeAll(async (done: Function) => {
// 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.
let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
try {
await abilityDelegator.startAbility({
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
});
} catch (exception) {
console.info(TAG, `StartAbility_001 exception = ${JSON.stringify(exception)}`);
expect().assertFail();
}
done();
})
```
4. **添加测试用例**
```
it('StartAbility_001', 0, async (done: Function) => {
console.info(TAG, 'StartAbility_001 begin');
let driver = Driver.create();
await driver.delayMs(100);
await driver.assertComponentExist(ON.text('Hello World'));
// find component by text
let button: Component = await driver.findComponent(ON.text('Hello World').enabled(true))
// click component
await button.click()
// get and assert component text
let content: string = await button.getText()
expect(content).assertEqual('Hello World')
done();
})
```
TestFinished-ResultCode: 0
TestFinished-ResultMsg: your test finished!!!
user test finished.
```
7. **覆盖率结果**
![覆盖率](file://D:\gitee\workdoc\sharedoc\%E8%B7%9F%E7%9D%80%E5%B0%8F%E7%99%BD%E4%B8%80%E8%B5%B7%E5%AD%A6%E9%B8%BF%E8%92%99%5B%E5%9B%9B%E5%8D%81%E4%B8%83%5D%20OH4.0%E7%9A%84%E5%BA%94%E7%94%A8UT%E5%BC%80%E5%8F%91%E5%8E%9F%E7%90%86\image-20240119173305008.png?lastModify=1706752882)
8. **为啥没覆盖到,可以点击具体部分查看**
![覆盖差缺](file://D:\gitee\workdoc\sharedoc\%E8%B7%9F%E7%9D%80%E5%B0%8F%E7%99%BD%E4%B8%80%E8%B5%B7%E5%AD%A6%E9%B8%BF%E8%92%99%5B%E5%9B%9B%E5%8D%81%E4%B8%83%5D%20OH4.0%E7%9A%84%E5%BA%94%E7%94%A8UT%E5%BC%80%E5%8F%91%E5%8E%9F%E7%90%86\image-20240119173432851.png?lastModify=1706752882)
**红色部分就是没覆盖部分,其实就是生命周期没覆盖,只测试了启动(也就是前台Foreground,没有后台Background,也没有load失败)所以生命周期的测试不完整。**