提交 ac501aa0 编写于 作者: C Catherine_MK

houjing8@huawei.com

Signed-off-by: NCatherine_MK <houjing8@huawei.com>
上级 eb2f7c2a
# Assertion
- Assertion,HJSUnit测试框架提供的断言能力,通过expect(XXX).assertXX的断言方式检查结果。
\ No newline at end of file
package com.example.assertion;
import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
public class MainAbility extends AceAbility {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
}
@Override
public void onStop() {
super.onStop();
}
}
package com.example.assertion;
import ohos.aafwk.ability.AbilityPackage;
public class MyApplication extends AbilityPackage {
@Override
public void onInitialize() {
super.onInitialize();
}
}
export default {
onCreate() {
console.info('AceApplication onCreate');
},
onDestroy() {
console.info('AceApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 40px;
color: #000000;
opacity: 0.9;
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
@media screen and (device-type: wearable) {
.title {
font-size: 28px;
color: #FFFFFF;
}
}
@media screen and (device-type: tv) {
.container {
background-image: url("../../common/images/Wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.title {
font-size: 100px;
color: #FFFFFF;
}
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
}
}
package com.example.assertion;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleOhosTest {
@Test
public void testBundleName() {
final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
assertEquals("com.example.assertion", actualBundleName);
}
}
\ No newline at end of file
export default {
onCreate() {
console.info('TestApplication onCreate');
},
onDestroy() {
console.info('TestApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 100px;
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
import file from '@system.file'
import app from '@system.app'
import device from '@system.device'
import router from '@system.router'
import {Core, Constant, ExpectExtend, ReportExtend, InstrumentLog} from 'deccjsunit/index'
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
console.info('onShow finish')
const core = Core.getInstance()
const expectExtend = new ExpectExtend({
'id': 'extend'
})
const reportExtend = new ReportExtend(file)
const instrumentLog = new InstrumentLog({
'id': 'report',
'version': '1.0.3'
})
core.addService('expect', expectExtend)
core.addService('report', reportExtend)
core.addService('report', instrumentLog)
core.init()
core.subscribeEvent('spec', instrumentLog)
core.subscribeEvent('suite', instrumentLog)
core.subscribeEvent('task', instrumentLog)
const configService = core.getDefaultService('config')
configService.setConfig(this)
require('../../../test/List.test')
core.execute()
},
onReady() {
},
}
\ No newline at end of file
import app from '@system.app'
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
describe('appInfoTest', function () {
it('app_info_test_001', 0, function () {
var info = app.getInfo()
expect(info.versionName).assertEqual('1.0')
expect(info.versionCode).assertEqual('3')
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertClose', function () {
it('assertCloseSuccess001', 0, function () {
let a = 100;
let b = 0.1
expect(a).assertClose(99, b)
})
it('assertCloseFail002', 0, function () {
let a = 100;
let b = 0.1
expect(a).assertClose(1, b)
})
it('assertCloseFail003', 0, function () {
let a = 100;
let b = 0.1
expect(a).assertClose(null, b)
})
it('assertCloseFail004', 0, function () {
expect(null).assertClose(null, 0)
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertContain', function () {
it('assertContainSuccessOnString', 0, function () {
let a = 'abc';
let b = 'b'
expect(a).assertContain(b)
})
it('assertContainSuccessOnArray', 0, function () {
let a = ['a', 'b', 'c'];
let b = 'b'
expect(a).assertContain(b)
})
it('assertContainFailOnString', 0, function () {
let a = 'abc';
let b = 'd'
expect(a).assertContain(b)
})
it('assertContainFailOnArray', 0, function () {
let a = ['a', 'b', 'c'];
let b = 'd'
expect(a).assertContain(b)
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertFail', function () {
it('assertFail', 0, function () {
let a = null
expect(a).assertFail()
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertFalse', function () {
it('assertFalseSuccess', 0, function () {
let d = false
expect(d).assertFalse()
})
it('assertFalseFail', 0, function () {
let d = true
expect(d).assertFalse()
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertInstanceOf', function () {
it('assertInstanceOfSuccessOnNumber', 0, function () {
let a = 12;
expect(a).assertInstanceOf('Number')
})
it('assertInstanceOfFailOnNumber', 0, function () {
let a = "abc";
expect(a).assertInstanceOf('String');
expect(a).assertInstanceOf('Number');
})
})
import {describe, it, expect} from 'deccjsunit/index'
describe('assertLarger', function () {
it('assertLargerSuccess', 0, function () {
let a = 12;
let b = 10;
expect(a).assertLarger(b)
})
it('assertLargerFail', 0, function () {
let a = 12;
let b = 13;
expect(a).assertLarger(b);
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertLess', function () {
it('assertLessSuccess', 0, function () {
let a = 12;
let b = 15;
expect(a).assertLess(b)
})
it('assertLessFail', 0, function () {
let a = 12;
let b = 10;
expect(a).assertLess(b);
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertNull', function () {
it('assertNullSuccess', 0, function () {
let d
expect(d).assertNull()
})
it('assertNullFail', 0, function () {
let d = 100
expect(d).assertNull()
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertThrowError', function () {
it('assertThrowErrorFail001', 0, function () {
let a = 12;
expect(a).assertThrowError()
})
it('assertThrowErrorSuccess', 0, function () {
let a = test
function test() {
throw Error("exception")
}
expect(a).assertThrowError("exception");
})
it('assertThrowErrorFail002', 0, function () {
let a = test
function test() {
throw Error("exception")
}
expect(a).assertThrowError("test");
})
})
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('assertUndefined', function () {
it('assertUndefinedSuccess', 0, function () {
let a = undefined
expect(a).assertUndefined()
})
it('assertUndefinedFail', 0, function () {
let a = 100
expect(a).assertUndefined()
})
})
\ No newline at end of file
# Async
- 异步,HJSUnit测试框架针对测试用例提供的异步能力。测试框架识别异步用例的关键字为async,用法如下:在回调函数function前加“async”关键字,调用异步接口时需加上“await”前缀;回调函数中添加“done”参数和用例结尾的加上“done()”,保证可以在执行该异步用例时强制获取到用例执行结果。
\ No newline at end of file
package com.example.async;
import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
public class MainAbility extends AceAbility {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
}
@Override
public void onStop() {
super.onStop();
}
}
package com.example.async;
import ohos.aafwk.ability.AbilityPackage;
public class MyApplication extends AbilityPackage {
@Override
public void onInitialize() {
super.onInitialize();
}
}
export default {
onCreate() {
console.info('AceApplication onCreate');
},
onDestroy() {
console.info('AceApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 40px;
color: #000000;
opacity: 0.9;
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
@media screen and (device-type: wearable) {
.title {
font-size: 28px;
color: #FFFFFF;
}
}
@media screen and (device-type: tv) {
.container {
background-image: url("../../common/images/Wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.title {
font-size: 100px;
color: #FFFFFF;
}
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
}
}
package com.example.async;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleOhosTest {
@Test
public void testBundleName() {
final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
assertEquals("com.example.async", actualBundleName);
}
}
\ No newline at end of file
export default {
onCreate() {
console.info('TestApplication onCreate');
},
onDestroy() {
console.info('TestApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 100px;
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
import file from '@system.file'
import app from '@system.app'
import device from '@system.device'
import router from '@system.router'
import {Core, Constant, ExpectExtend, ReportExtend, InstrumentLog} from 'deccjsunit/index'
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
console.info('onShow finish')
const core = Core.getInstance()
const expectExtend = new ExpectExtend({
'id': 'extend'
})
const reportExtend = new ReportExtend(file)
const instrumentLog = new InstrumentLog({
'id': 'report',
'version': '1.0.3'
})
core.addService('expect', expectExtend)
core.addService('report', reportExtend)
core.addService('report', instrumentLog)
core.init()
core.subscribeEvent('spec', instrumentLog)
core.subscribeEvent('suite', instrumentLog)
core.subscribeEvent('task', instrumentLog)
const configService = core.getDefaultService('config')
configService.setConfig(this)
require('../../../test/List.test')
core.execute()
},
onReady() {
},
}
\ No newline at end of file
import {describe, it} from 'deccjsunit/index'
describe('callback', function () {
it('callbackDemo', 0, async function (done) {
await a()
})
})
function a(done){
setTimeout(function(){
console.log("run a after 1000ms")
},1000)
done()
}
\ No newline at end of file
require('./Callback.test.js')
require('./Promise.test.js')
\ No newline at end of file
import {describe, it} from 'deccjsunit/index'
describe('promise', function () {
it('promiseDemo', 0, async function (done) {
await new Promise(
function (resolve, reject) {
// 一段耗时的异步操作
resolve('成功') // 数据处理完成
// reject('失败') // 数据处理出错
}).then(
(res) => {console.log(res)}, // 成功
(err) => {console.log(err)} // 失败
)
done()
})
})
\ No newline at end of file
# DataDriver
- DataDriver,HJSUnit测试框架提供的数据驱动能力。通过配置data.json文件,实现针对指定测试套、测试用例的参数传递,亦或是根据指定测试套、测试用例的执行次数做压力测试。
\ No newline at end of file
package com.example.datadriver;
import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
public class MainAbility extends AceAbility {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
}
@Override
public void onStop() {
super.onStop();
}
}
package com.example.datadriver;
import ohos.aafwk.ability.AbilityPackage;
public class MyApplication extends AbilityPackage {
@Override
public void onInitialize() {
super.onInitialize();
}
}
export default {
onCreate() {
console.info('AceApplication onCreate');
},
onDestroy() {
console.info('AceApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 40px;
color: #000000;
opacity: 0.9;
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
@media screen and (device-type: wearable) {
.title {
font-size: 28px;
color: #FFFFFF;
}
}
@media screen and (device-type: tv) {
.container {
background-image: url("../../common/images/Wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.title {
font-size: 100px;
color: #FFFFFF;
}
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
}
}
package com.example.datadriver;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleOhosTest {
@Test
public void testBundleName() {
final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
assertEquals("com.example.datadriver", actualBundleName);
}
}
\ No newline at end of file
export default {
onCreate() {
console.info('TestApplication onCreate');
},
onDestroy() {
console.info('TestApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 100px;
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
// @ts-nocheck
import file from '@system.file'
import app from '@system.app'
import device from '@system.device'
import router from '@system.router'
import {Core, Constant, DataDriver, ExpectExtend, ReportExtend, InstrumentLog} from 'deccjsunit/index'
import data from '../../../test/data.json'
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
console.info('onShow finish')
const core = Core.getInstance()
const dataDriver = new DataDriver({data:data})
const expectExtend = new ExpectExtend({
'id': 'extend'
})
const reportExtend = new ReportExtend(file)
const instrumentLog = new InstrumentLog({
'id': 'report',
'version': '1.0.3'
})
core.addService('dataDriver',dataDriver)
core.addService('expect', expectExtend)
core.addService('report', reportExtend)
core.addService('report', instrumentLog)
core.init()
core.subscribeEvent('spec', instrumentLog)
core.subscribeEvent('suite', instrumentLog)
core.subscribeEvent('task', instrumentLog)
const configService = core.getDefaultService('config')
configService.setConfig(this)
require('../../../test/List.test')
core.execute()
},
onReady() {
},
}
\ No newline at end of file
import {describe, it, expect} from 'deccjsunit/index'
describe('testSuite01', function () {
it('testSpec01', function (data) {
console.info('testSpec01......' + JSON.stringify(data))
})
it('testSpec02', function (data) {
console.info('testSpec02......' + JSON.stringify(data))
})
})
\ No newline at end of file
require('./DataDriver.test.js')
\ No newline at end of file
{
"suites": [{
"describe":["testSuite01"],
"params":{
"suiteParam01": "suiteParam"
}
},{
"describe":["testSuite01","testSpec02"],
"params":{
"suiteParam": "suiteParam"
}
},{
"describe":"testSuite01",
"stress": 8,
"items":[{
"it":"testSpec01",
"stress": 1,
"params":[{
"name":"tom"
}]
},{
"it":"testSpec02",
"stress": 2
}]
}]
}
\ No newline at end of file
# App Samples
- [Introduction](#section_HJSUnit_001)
- [Usage](#section_HJSUnit_002)
- [Limitations](#section_HJSUnit_003)
## Introduction<a name="section_HJSUnit_001"></a>
We provide a series of app samples to help you quickly get familiar with the test capabilities and how to use of the HJSUnit. Each app sample is an independent project in DevEco Studio. You can import sample code from a project to a customized project to learn how to use APIs by browsing code, building the project, and installing and running the app.
## Usage<a name="section_HJSUnit_002"></a>
1. Development tool: Devco Studio 2.2.0.200 or later
2. Procedure: Create a blank JS template project in Devco Studio, import the sample code in the ohosTest directory of the sample project to the ohosTest directory of the user-defined project, and compile, build, run, and debug the project.
3. After HAP installation and execution, you can view the execution effect of the sample on the device and then conduct debugging.
## Limitations<a name="section_HJSUnit_003"></a>
Before installing and running the sample, check the **deviceType** field in the **config.json** file to obtain the device types supported by the sample. You can modify this field to enable the sample to run on your target device. \(The **config.json** file is generally stored in the **entry/src/main** directory, which may be different depending on the samples.\)
\ No newline at end of file
# 应用示例
- [概要简介](#section_HJSUnit_001)
- [使用说明](#section_HJSUnit_002)
- [约束与限制](#section_HJSUnit_003)
## 概要简介<a name="section_HJSUnit_001"></a>
为帮助测试人员快速熟悉HJSUnit所提供的测试能力和如何使用,我们提供了一系列的应用示例,即Sample。每一个应用示例都是一个独立的DevEco Studio工程项目,测试人员可以将工程中的代码导入到自定义工程中,通过浏览代码、编译工程、安装和运行应用示例来了解应用示例中涉及测试能力的使用方法。
## 使用说明<a name="section_HJSUnit_002"></a>
1. 开发工具:2.2.0.200版本及以上的Deveco Studio
2. 使用步骤:①在Deveco Studio中新建空白JS模板项目,将示例工程ohosTest目录下的示例代码导入到自定义工程的ohosTest目录下,进行编译构建及运行调试。
3. 安装运行后,即可在设备上查看应用示例运行效果,以及进行相关调试。
## 约束与限制<a name="section_HJSUnit_003"></a>
安装运行应用示例之前,请先通过config.json文件中的"deviceType"字段来确认该应用示例支持的设备类型,可尝试通过修改该字段使其可以在相应类型的设备上运行(config.json文件一般在代码的entry/src/main路径下,不同的Sample可能会有不同)。
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册