At present, JS Unit Testing Framework has a wealth of choices, such as Buster.js, Testswarm, Jstestdriver and so on. Jasmine, as a popular JavaScript testing tool, presents the concept of BDD (Behavior Driven development) behavior-driven development, which is only about 66K, and is feature-rich, which makes it easy to write clear and concise test cases for projects. A good test framework choice for JavaScript-based projects.
Installation:
Download Jasmine Latest Version Https://github.com/pivotal/jasmine/blob/master/dist/jasmine-standalone-2.0.3.zip
After decompression, "Specrunner.html" is a demo, you can double-click open to see the test results page
Folder spec and SRC are demo of the test JS directory and source code JS directory
Hello World:
Let's follow the demo structure and build a minimal test suite to get a feel for this easy-to-use unit test.
1. Set up the test results page first, test.html
<!DOCTYPE HTML><HTML><Head><Linkrel= "stylesheet"type= "Text/css"href= "/js/jasmine-2.0.3/jasmine.css"><Scripttype= "Text/javascript"src= "/js/jasmine-2.0.3/jasmine.js"></Script><Scripttype= "Text/javascript"src= "/js/jasmine-2.0.3/jasmine-html.js"></Script><Scripttype= "Text/javascript"src= "/js/jasmine-2.0.3/boot.js"></Script></Head><Body><H1>Jasmine Test</H1><Scripttype= "Text/javascript"src= "Src.js"></Script><Scripttype= "Text/javascript"src= "Test.js"></Script></Body></HTML>
Jasmine.css is the appearance control of the test result display
Jasmine.js is Jasmine.js's test run class library.
Jasmine-html.js is the class library that the test results display
Boot.js is the JS that starts and initializes the test framework,
Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the Loaded environment and all of a project ' s specs. This file should is loaded after ' jasmine.js ', but before any project source files or spec files is loaded. Thus This file can also is used to customize Jasmine for a project.
Many of the online tutorials are based on the version before 2.0, will write a section of JS to start Jasmine, now just load boot.js on OK (but should be in the jasmine.js behind, other JS source code before load)
These three files will be found in the unpacked Lib.
Src.js is the JS code we want to test today, is not very simple ^_^
function Hello (hellowho) { return "Hello" + hellowho;}
Test.js is our test case.
function () { var name; It (function() { = "World"; var exp = "Hello world"; Expect (exp). Toequal (Hello (input)); });
Describe blocks and it blocks have two parameters, one is a string, can be written casually, and the other is a function. In general, a describe is used to describe a suite that contains multiple it, each of which is used to test a function.
Of course, describe can also be nested describe.
In this example, we can see that the actual statement used to verify is "expect ()." Toequal (); ", expect () is the desired result, and toequal () is its sub-method, which indicates whether it is equal to its expectations.
Expect (3). Toequal (1+2) is a unit test.
OK, one of the simplest unit tests is written, so visit test.html.
This is passed the test!!!
If Test.js changed to var exp = "Hello world! ";, then the test results page fell down on the tragic fail
The error is "expected ' hello World ' to equal ' Hello World '.", the second understands that the Hello function returns the string and the expected inconsistency.
This article is intended to stimulate, jasmine in addition to toequal to compare the return value, there are other matching test, asynchronous test method, the function is very powerful.
China has an old saying: learn a tool, you can knock down a piece of code every day.
Jasmine of JavaScript Unit testing