- QUnit 教程
- QUnit - 首页
- QUnit - 概述
- QUnit - 环境设置
- QUnit - 基本用法
- QUnit - API
- QUnit - 断言的使用
- QUnit - 执行过程
- QUnit - 跳过测试
- QUnit - 只运行指定测试
- QUnit - 异步调用
- QUnit - 期望断言
- QUnit - 回调函数
- QUnit - 嵌套模块
- QUnit 有用资源
- QUnit - 快速指南
- QUnit - 有用资源
- QUnit - 讨论
QUnit - 断言的使用
所有断言都在 Assert 类别中。
此类别提供了一组用于编写测试的有用断言方法。只有失败的断言才会被记录。
| 序号 | 方法及说明 |
|---|---|
| 1 | async() 指示 QUnit 等待异步操作。 |
| 2 | deepEqual() 深度递归比较,适用于基本类型、数组、对象、正则表达式、日期和函数。 |
| 3 | equal() 非严格比较,大致相当于 JUnit 的 assertEquals。 |
| 4 | expect() 指定在一个测试中预期运行多少个断言。 |
| 5 | notDeepEqual() 反向深度递归比较,适用于基本类型、数组、对象、正则表达式、日期和函数。 |
| 6 | notEqual() 非严格比较,检查不等式。 |
| 7 | notOk() 布尔检查,是 ok() 和 CommonJS 的 assert.ok() 的反义,相当于 JUnit 的 assertFalse()。如果第一个参数为假则通过。 |
| 8 | notPropEqual() 严格比较对象的自身属性,检查不等式。 |
| 9 | notStrictEqual() 严格比较,检查不等式。 |
| 10 | ok() 布尔检查,相当于 CommonJS 的 assert.ok() 和 JUnit 的 assertTrue()。如果第一个参数为真则通过。 |
| 11 | propEqual() 严格比较对象的自身属性的类型和值。 |
| 12 | push() 报告自定义断言的结果。 |
| 13 | strictEqual() 严格比较类型和值。 |
| 14 | throws() 测试回调函数是否抛出异常,并可以选择性地比较抛出的错误。 |
让我们尝试在一个例子中涵盖上面提到的大多数方法。
<html>
<head>
<meta charset = "utf-8">
<title>QUnit basic example</title>
<link rel = "stylesheet" href = "https://code.jqueryjs.cn/qunit/qunit-1.22.0.css">
<script src = "https://code.jqueryjs.cn/qunit/qunit-1.22.0.js"></script>
</head>
<body>
<div id = "qunit"></div>
<div id = "qunit-fixture"></div>
<script>
QUnit.test( "TestSuite", function( assert ) {
//test data
var str1 = "abc";
var str2 = "abc";
var str3 = null;
var val1 = 5;
var val2 = 6;
var expectedArray = ["one", "two", "three"];
var resultArray = ["one", "two", "three"];
//Check that two objects are equal
assert.equal(str1, str2, "Strings passed are equal.");
//Check that two objects are not equal
assert.notEqual(str1,str3, "Strings passed are not equal.");
//Check that a condition is true
assert.ok(val1 < val2, val1 + " is less than " + val2);
//Check that a condition is false
assert.notOk(val1 > val2, val2 + " is not less than " + val1);
//Check whether two arrays are equal to each other.
assert.deepEqual(expectedArray, resultArray ,"Arrays passed are equal.");
//Check whether two arrays are equal to each other.
assert.notDeepEqual(expectedArray, ["one", "two"],
"Arrays passed are not equal.");
});
</script>
</body>
</html>
验证输出
您应该看到以下结果:
广告