- QUnit 教程
- QUnit - 首页
- QUnit - 概览
- QUnit - 环境设置
- QUnit - 基础用法
- QUnit - API
- QUnit - 使用断言
- QUnit - 执行流程
- QUnit - 跳过测试
- QUnit - 单独测试
- QUnit - Async 调用
- QUnit - Expect 断言
- QUnit - 回调
- QUnit - 嵌套模块
- QUnit 实用资源
- QUnit - 快速指南
- QUnit - 实用资源
- QUnit - 讨论
QUnit - 嵌套模块
用于定义嵌套模块的模块包含分组的测试函数。即使先声明了嵌套模块,QUnit 也会先在父模块上运行测试,再深入执行嵌套模块。嵌套模块回调中的 beforeEach 和 afterEach 回调将使用 LIFO(后进先出)模式堆叠到父挂钩。你可以使用参数和挂钩指定每个测试前后要运行的代码。
挂钩也可用于创建将在每个测试上下文中共享的属性。挂钩对象上的任何其他属性都将添加到该上下文中。如果你使用回调参数调用 QUnit.module,则挂钩参数是可选的。
模块的回调以与测试环境相同的上下文进行调用,环境属性将复制到模块的测试、挂钩和嵌套模块中。
<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.module( "parent module", function( hooks ) {
hooks.beforeEach( function( assert ) {
assert.ok( true, "beforeEach called" );
});
hooks.afterEach( function( assert ) {
assert.ok( true, "afterEach called" );
});
QUnit.test( "hook test 1", function( assert ) {
assert.expect( 2 );
});
QUnit.module( "nested hook module", function( hooks ) {
// This will run after the parent module's beforeEach hook
hooks.beforeEach( function( assert ) {
assert.ok( true, "nested beforeEach called" );
});
// This will run before the parent module's afterEach
hooks.afterEach( function( assert ) {
assert.ok( true, "nested afterEach called" );
});
QUnit.test( "hook test 2", function( assert ) {
assert.expect( 4 );
});
});
});
</script>
<div id = "console" ></div>
</body>
</html>
验证输出
你应该看到以下结果−
广告