Javascipt Test Research

Source: Internet
Author: User

Javascipt Test Survey 1 JsUnit1.1 Introduction

Jsunit is the open Source Unit testing framework for JavaScript. It is inspired by JUnit and is written entirely in JavaScript. This is a previously popular JavaScript unit testing tool that can be combined with Maven and ant to do some continuous integration work.

1.2 Use steps to explore

(1) Download Jsunit, then unzip the file. : Http://sourceforge.net/projects/jsunit/?source=navbar

(2) Use the browser to open the testrunner.html file in the extracted directory.

(3) To prepare the test case, you can do the experiment directly using the page of a test case in the tests folder in the download directory.

(4) Run the test case and fill in the test suite or test file path into the input box. Click to run, we can see the result after running.

Example of writing 1.3 Jsunit test Cases

(1) A simple test case is written

<title>a unit test for DRW. Systemundertest class</title>

<script type= ' text/javascript ' src= '. /jsunit/app/jsunitcore.js ' ></script>

<link rel= "stylesheet" type= "Text/css" href= ". /css/jsunitstyle.css ">

<script type= ' text/javascript ' src= '. /app/system_under_test.js ' ></script>

<script type= ' Text/javascript ' >

function SetUp () {

Perform fixture set up

}

function TearDown () {

Clean up

}

function testonething () {

Instantiating a systemundertest, a class in the DRW namespace

var sut = new DRW. Systemundertest ();

var thing = sut.onething ();

Assertequals (1, thing);

}

function testanotherthing () {

var sut = new DRW. Systemundertest ();

var thing = sut.anotherthing ();

Assertnotequals (1, thing);

}

</script>

<body/>

The above is an example of the writing of a test case, which we can use as an HTML file. We need to introduce Jsunitcore.js and jsunitstyle.css two files in the head of the HTML file, a jsunit core framework. We also need to add the JS script file system_under_test.js that we need to test.

You can see the same setup and teardown as JUnit, where the two functions are the preparation and final processing of the test case, executed before and after each test case.

The two functions of testonething () and testanotherthing () are test cases, which complete what needs to be tested.

As with JUnit, a number of assert functions are also defined in Jsunit. Functions like assert** are their assert functions, and we use these functions to determine that the result is the same as expected.

(2) Jsunit test set writing

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<title>jsunit Test suite</title>

<link rel= "stylesheet" type= "Text/css" href= ". /css/jsunitstyle.css ">

<script type= "Text/javascript" src= ". /app/jsunitcore.js "></script>

<script type= "Text/javascript" >

function Coresuite () {

var result = new Jsunittestsuite ();

Result.addtestpage ("tests/jsunitassertiontests.html");

Result.addtestpage ("tests/jsunitframeworkutilitytests.html");

Result.addtestpage ("tests/jsunitonloadtests.html");

Result.addtestpage ("tests/jsunitrestoredhtmldivtests.html");

Result.addtestpage ("tests/jsunitsetupteardowntests.html");

Result.addtestpage ("tests/jsunittestmanagertests.html");

Result.addtestpage ("tests/jsunitparamstests.html");

Result.addtestpage ("tests/jsunittestsetuppages.html");

Result.addtestpage ("tests/jsunittestsuitetests.html");

Result.addtestpage ("tests/jsunitutilitytests.html");

Result.addtestpage ("tests/testpagetest.html");

Result.addtestpage ("tests/uimanagertest.html");

return result;

}

function Librariessuite () {

var result = new Jsunittestsuite ();

Result.addtestpage ("tests/jsunitmocktimeouttest.html");

Result.addtestpage ("tests/jsunitajaxtest.html");

return result;

}

Function Suite () {

var newsuite = new Jsunittestsuite ();

Newsuite.addtestsuite (Coresuite ());

Newsuite.addtestsuite (Librariessuite ());

return newsuite;

}

</script>

<body>

<p>this page contains a suite of tests for testing jsunit.</p>

</body>

The code above is the writing of the test set, and the concept of the test set is consistent with JUnit. To define a test set, you need to create a function called Suite that returns an Jsunittestsuite object. The Jsunittestsuite object is then built by adding test case pages or other test set objects. In the example above, the Jsunittestsuite object returned by Coresuite and Librariessuite two functions was added to a Jsunittestsuite object in the suite function and returned to the object.

2 Jasmine2.1 Introduction

Jasmine is a behavior-driven JavaScript testing framework that does not depend on any other JavaScript framework. It has a clean and clear syntax, so you can write the test code very simply. For JavaScript-based development, it is a good test framework choice.

2.2 Use steps to explore

(1) Download Jasmine

: https://github.com/pivotal/jasmine/releases

Introduction:http://jasmine.github.io/2.0/introduction.html

(2) Directory parsing

Unzip the downloaded zip file and click Open Folder. See the following directory, where Lib is the source code for Jasmine. SRC is an example of the measured JS file, spec for the written test case code, specrunner.html to join

(3) Running test cases

After extracting the file, you can see that there is a file called: specrunner.html, you can run the relevant test case by clicking Open with Browser.

After opening in the browser, we can see a result page, which will show the result information of the run.

2.3 Test Case Writing

(1) A simple test case

Describe ("When song had been Paused", function () {

Beforeeach (function () {

Player.play (song);

Player.pause ();

});

It ("should indicate that's song is currently paused", function () {

Expect (player.isplaying). Tobefalsy ();

Demonstrates use of ' no ' with a custom matcher

Expect (player). Not.tobeplaying (song);

});

It ("should is possible to resume", function () {

Player.resume ();

Expect (player.isplaying). Tobetruthy ();

Expect (Player.currentlyplayingsong) toequal (song);

});

});

Describe is the global function of Jasmine, and as a Test Suite start, it usually has 2 parameters: string and method. string as the name and title of the specific Suite. The method is to include code that implements the set of test cases.

Specs is defined by calling it's global function. Like describe, it also has 2 parameters, strings, and methods. Each Spec contains one or more expectations to test the code that needs to be tested. Every it is a test case.

Each expectation in the Jasmine is an assertion, which can be either true or false. When all expectations in each Spec are true, pass the test. If any one of the expectation is false, the test is not passed. And the content of the method is the test body.

3 JSLint3.1 Introduction

JSLint is a JavaScript grammar checker and validation tool that scans JavaScript source code to find problems. JSLint is not an open source software, it is written in pure JavaScript. JSLint defines a set of professional grammar rules and recommendations.

If JSLint finds a problem, JSLint displays a message describing the problem and points out the approximate location of the error in the source code. Some coding style conventions may lead to unforeseen behavior or errors, and JSLint can mark structural problems in addition to these unreasonable conventions. Although JSLint cannot guarantee that the logic must be correct, it does help to find errors that are likely to cause the browser's JavaScript engine to throw errors.

The core principle of JSLint code quality detection is the set of rules that users set. JSLint the rule set provided by default contains a poorly developed style that WEB developers have accumulated over the years, and we can choose to build a specific set of rules based on the needs of our projects. JSLint will scan the JavaScript script according to it and give the corresponding problem description information.

Preliminary study on the usage of 3.2 jslint

(1) directly using http://www.jslint.com/URL to detect. In the input box, your JavaScript script. Then click on the JSLint button and we'll see what JSLint said about our mistakes.

This page also provides some other rules, if you do not select these rules, follow the default rules of JSLint

Javascipt Test Research

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.