自动化测试(五)03-js测试框架AVA——ava中的断言api之pass、fail、false、is、regex、not、throws
ava中的断言:
.pass([message])
.fail([message])
.assert(value, [message])
.truthy(value, [message])
.falsy(value, [message])
.true(value, [message])
.false(value, [message])
.is(value, expected, [message])
.not(value, expected, [message])
.deepEqual(value, expected, [message])
.notDeepEqual(value, expected, [message])
.deepEqual()。
.throws(fn, [expected, [message]])
.throwsAsync(thrower, [expected, [message]])
.notThrows(fn, [message])
.notThrowsAsync(nonThrower, [message])
.regex(contents, regex, [message])
.notRegex(contents, regex, [message])
.snapshot(expected, [message])
.snapshot(expected, [options], [message])
命令
npx ava assertion.test.js
实例
1、初始化项目
npm init -y
npm install ava -D
2、新建测试文件
test/assertion.test.js
import test from 'ava'
test('pass assertion', async t => {
t.pass()
})
test('fail assertion', async t => {
t.is.skip() // fail()
})
test('assert test', async t => {
t.assert(true, 'assert function , assert assertion')
})
// test(',true or ,false')
test('not test', async t => {
t.not(1, 2)
})
test('regex test', async t => {
// notRegex
t.regex('jsx', new RegExp('js'))
})
test('throws fn test', async t => {
t.throws(() => {
throw new TypeError('type error in ava')
}, TypeError)
})
const fn = () => {
throw new TypeError('🦄');
};
test('throws', t => {
const error = t.throws(() => {
fn();
}, TypeError);
console.log(error.message)
t.is(error.message, '🦄');
});
执行命令
npx ava
或
npm run test
或
npx ava assertion.test.js
ava实例-测试效果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zPVDLgHH-1645898021787)(C:\Users\Administrator\Desktop\阶段5 架构与运维\课程资料\1\阶段5:架构与运维\05-3 自动化测试\resource\assets\ava实例.jpg)]
附
package.json
{
"name": "ava-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "ava"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ava": "^2.2.0",
"eslint": "^6.0.1",
"eslint-plugin-ava": "^7.1.0"
}
}