Javascript的UT测试框架-Jasmine简介

Jasmine是什么?(What)

Jasmine是一个测试JavaScript代码的行为驱动开发(BDD)框架。

他不依赖于任何javaScript框架,不要求有DOM。他有一个干净,简洁的语法,让你很容易编写测试程序。

Jasmine怎么用?(HOW)

下载包:

在如下路径下载Jasmine的包https://github.com/jasmine/jasmine/releases/download/v2.3.4/jasmine-standalone-2.3.4.zip

 

语法

describe函数

一个测试组(suite)开始于Jasmine 的“ describe”函数。

Describe函数如下,他有两个参数。

第一个参数:测试组的描述信息

第二个参数:组测组的具体实现函数代码块(具体的干活)

例如:

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

it函数

具体的一个测试规格(Spec)必须开始于一个it中。

 itDescribe一样也有两个参数,

第一个参数:对于这个规格测试的描述信息

第二个参数:这个规格测试的具体实现

 

一个Spec包含一个或多个相当于断言的预期。如

上面的例子expect(true).toBe(true); expect函数对于True的预期是True。

一个Spec的所有的预期都为True的情况下,It为True,所有的It为True的情况下一个Describe为True

 

expect函数

一个预期是建立在一个expect函数上的。expect函数是一个于预期值进行匹配的匹配连。

如:

expect(true).toBe(true);

expect(false).not.toBe(true); 

禁用测试组

Suites and specs can be disabled with the xdescribe and xit

Demo

https://github.com/jasmine/jasmine/releases/download/v2.3.4/jasmine-standalone-2.3.4.zip

下载的文件目录如下:

 

双击打开SpecRunner.html文件,将显示所有的UT结果。

Notepad打开SpecRunner.html文件,你将会看到如下内容:

l  其中你的测试目标JS和你的测试代码分别写在Src目录和Spec目录下的JS文件里面。

 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner v2.3.4</title>
  <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.3.4/jasmine_favicon.png">
  <link rel="stylesheet" href="lib/jasmine-2.3.4/jasmine.css">

  <script src="lib/jasmine-2.3.4/jasmine.js"></script>
  <script src="lib/jasmine-2.3.4/jasmine-html.js"></script>
  <script src="lib/jasmine-2.3.4/boot.js"></script>

  <!-- include source files here... -->
  <script src="src/Player.js"></script>
  <script src="src/Song.js"></script> 

  <!-- include spec files here... -->
  <script src="spec/SpecHelper.js"></script>
  <script src="spec/PlayerSpec.js"></script> 

</head>
<body>
</body>
</html>

参考资料

http://jasmine.github.io/edge/introduction.html

http://jasmine.github.io/

posted @ 2015-07-24 09:52  flyzfj  阅读(1871)  评论(0)    收藏  举报