Jasmine 编写 JavaScript 测试用例
1,下载Jasmine 包,主要包括如下三个文件:
1>,jasmine-html.js,jasmine.css,jasmine.js
2>,编写测试用例,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | describe( "Examples of Jasmine suite" , function () { //List 1 describe( "This is an exmaple suite" , function () { it( "contains spec with an expectation" , function () { expect( true ).toBe( true ); expect( false ).toBe( false ); expect( false ).not.toBe( true ); }); }); //List 2 describe( "Test suite is a function." , function () { var gVar; it( "Spec is a function." , function () { gVar = true ; expect(gVar).toBe( true ); }); it( "Another spec is a function." , function () { gVar = false ; expect(gVar).toBe( false ); }); }); //List 3 describe( "This is an exmaple suite" , function () { it( "contains spec with an expectation" , function () { var num = 10; expect(num).toEqual(10); }); }); //List 4 describe( "The 'toBe' matcher compares with ===" , function () { it( "and has a positive case " , function () { expect( true ).toBe( true ); }); it( "and can have a negative case" , function () { expect( false ).not.toBe( true ); }); }); //List 5 describe( "Included matchers:" , function () { it( "The 'toBe' Matcher" , function () { var a = 3.6; var b = a; expect(a).toBe(b); expect(a).not.toBe( null ); }); describe( "The 'toEqual' matcher" , function () { it( "works for simple literals and variables" , function () { var a = "varA" ; expect(a).toEqual( "varA" ); }); it( "Work for objects" , function () { var obj = { a: 1, b: 4 }; var obj2 = { a: 1, b: 4 }; expect(obj).toEqual(obj2); }); }); it( "The 'toBeDefined' matcher " , function () { var obj = { defined: 'defined' }; expect(obj.defined).toBeDefined(); expect(obj.undefined).not.toBeDefined(); }); }); //List 6 describe( "An example of setup and teardown" , function () { var gVar; beforeEach( function () { gVar = 3.6; gVar += 1; }); afterEach( function () { gVar = 0; }); it( "after setup, gVar has new value." , function () { expect(gVar).toEqual(4.6); }); it( "A spec contains 2 expectations." , function () { gVar = 0; expect(gVar).toEqual(0); expect( true ).toEqual( true ); }); }); //List 7 describe( "A spec" , function () { var gVar; beforeEach( function () { gVar = 3.6; gVar += 1; }); afterEach( function () { gVar = 0; }); it( "after setup, gVar has new value." , function () { expect(gVar).toEqual(4.6); }); it( "A spec contains 2 expectations." , function () { gVar = 0; expect(gVar).toEqual(0); expect( true ).toEqual( true ); }); describe( "nested describe" , function () { var tempVar; beforeEach( function () { tempVar = 4.6; }); it( "gVar is global scope, tempVar is this describe scope." , function () { expect(gVar).toEqual(tempVar); }); }); }); //List 8 xdescribe( "An example of xdescribe." , function () { var gVar; beforeEach( function () { gVar = 3.6; gVar += 1; }); xit( " and xit" , function () { expect(gVar).toEqual(4.6); }); }); }); |
3>,编写一个List.html文件作为调用Jasmine 和测试用例的入口文件,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | < html > < head > < title >Jasmine Spec Example</ title > < link rel="shortcut icon" type="image/png" href="lib/jasmine-1.3.1/jasmine_favicon.png"> < link rel="stylesheet" type="text/css" href="lib/jasmine-1.3.1/jasmine.css"> < script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></ script > < script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></ script > < script type="text/javascript" src="spec/Lists.js"></ script > < script type="text/javascript"> var jasmineEnv = jasmine.getEnv(); var htmlReporter = new jasmine.HtmlReporter(); jasmineEnv.addReporter(htmlReporter); jasmineEnv.specFilter = function(spec) { return htmlReporter.specFilter(spec); }; window.onload = function() { jasmineEnv.execute(); }; </ script > </ head > < body > </ body > </ html > |
4>,在浏览器打开List.html文件,即可用查看 Lists.js 文件编写的测试用例的所有的执行结果报告,如果所测试用例执行错误,则会在界面上提示是哪个测试用例执行报错,如下图:
Demo 下载 下载密码:a363
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 2025成都.NET开发者Connect圆满结束