JavaScript Automated Unit Testing

Source: Internet
Author: User

#JavaScript Automated Unit Testing

-Why automated testing?

-what projects are suitable for automated testing?

-Test Runner Karma

-Test Framework Jasmine

# # # Why do you test automation?


1. Correctness: Verify the correctness of the code

2. Automation: Write once, run multiple times

3. Explanatory: Reading test cases, sometimes clearer than document descriptions

4. Drive development: Rapid feedback to improve development efficiency

5. Ensure refactoring: The test case is backed up, you can boldly refactor the continuous integration


# # # What is the right project for automated testing?

Revenue from automation = number of iterations * (full manual execution cost-maintenance cost)-First automation cost

Trade-offs from three aspects


1, the demand change is not frequent

The stability of the test script determines the maintenance cost of the automated test. If the software requirements change too frequently, testers need to update the test cases and related test scripts according to the needs of the changes, and the maintenance of the script itself is a process of code development, need to modify, debug, and, if necessary, modify the framework of the automated test, if the cost is not lower than the test cost of using its savings , then automated testing is a failure.


Some of the modules in the project are relatively stable, and some of the modules require a lot of variability. We can automatically test the relatively stable modules, while the larger changes are still tested by hand.


2, the project period is longer

Because of the determination of automation test requirements, the design of the automated test framework, the writing and debugging of test scripts, it takes quite a long time to complete. The process itself is a test software development process that takes a long time to complete. If the project's cycle is short and there is not enough time to support such a process, then automated testing becomes a joke.


3. Automated test scripts can be reused

The reuse of automated test scripts is to be considered whether there is a significant difference between the tested projects, whether the test tools selected are adapted to the differences, and whether testers have the ability to develop automated test frameworks that adapt to this discrepancy.



# # # Test Runner

-KARMA: Set the framework, environment, source files, test files, etc. required for the test, so that you can perform the test easily when you have finished configuring them.


# # # Test Framework

-Qunit: The framework was first created for the unit test of jquery, and was later independent from the jquery itself, but it was still out of the shadow of jquery

-Jasmine:behavior-drive Development (BDD)-style test framework, which is popular in the industry, features a comprehensive, self-asssert, mock function

-The works of the Mocha:node community, the great God TJ, can be used on the node and the browser side, with strong flexibility.

-Intern: Look at the official introduction The test framework is extremely versatile and seems to encompass all of the industry's testing-related features


# # # Assertion Library

-Chai: Should be the current set of popular assertion libraries, support TDD (assert), BDD (expect, should) two styles of assertion library

-Another open source contribution from Should.js:TJ

-Another assertion library in the Expect.js:BDD style, based on Should.js, is the mini version of the BDD library

-Assert (node comes with core module): Assertion module that can be used in node


# # # Test Runner Karma


Karma is Testacular's new name, in 2012 Google Open source testacular,2013 year testacular renamed Karma. Karma is a very mysterious name, indicating the fate of Buddhism, karma, this name is even more people to guess!

Karma is a node. JS-based JavaScript test Execution Process management tool (test Runner). This tool can be used to test all major web browsers or to work with other code editors. A powerful feature of this testing tool is that it can monitor changes to the (Watch) file and then execute it on its own, displaying the test results by Console.log.



Resources:

-Karma Official Website: https://karma-runner.github.io/1.0/index.html


-Karma-runner Installation steps: https://karma-runner.github.io/0.12/intro/installation.html


-Karma config file:http://karma-runner.github.io/0.8/config/configuration-file.html


-Karma files:http://karma-runner.github.io/0.8/config/files.html


-Karma and Jasmin Automated Unit tests: http://blog.fens.me/nodejs-karma-jasmine/


-Angularjs's unittest:https://docs.angularjs.org/guide/unit-testing

# # # Test Framework Jasmine


# # # # Jasmine's basic syntax

1. TDD (Test Driven development) test-driven development

2. BDD (Behavior Driven development) behavior-driven development can effectively improve the design and identify the way forward for the team in the evolution of the system

-Jasmine has four core concepts: grouping (Suites), use case (Specs), expectation (expectations), matching (matchers).

-Suites can be understood as a set of test cases, created using the global Jasmin function describe. The describe function accepts two parameters, a string and a function.

-Specs can be understood as a test case, using the global Jasmin function it creates. As with describe, accept two parameters, a string and a function, which is the test code to execute, and the string is the name of the test case.

-Expectations is created by the expect function. Accept a parameter. In conjunction with Matcher, set the expected value of the test.

-The Matcher implements a comparison between "expected" and "actual", and if the result is true, passes the test, and vice versa, fails. Every matcher can perform a negative judgment by not.

```

Expect (a). ToBe (true);//expected variable A is true

Expect (a). Toequal (true);//expected variable a equals true

Expect (a). Tomatch (/reg/);//expected variable A to match a reg regular expression or a string

Expect (A.foo). tobedefined ();//expected A.foo defined

Expect (A.foo). tobeundefined ();//expected A.foo undefined

Expect (a). Tobenull ();//expected variable A is null

Expect (A.ismale). Tobetruthy ();//Expect A.ismale to be true

Expect (A.ismale). Tobefalsy ();//expect A.ismale to be false

Expect (true). Toequal (true);//expected True equals True

Expect (a). Tobelessthan (b);//Expect A to be less than B

Expect (a). Tobegreaterthan (b);//expected a greater than B

Expect (a). Tothrowerror (/reg/);//Expect a method to throw an exception

Expect (a). Tothrow ();//Expect a method to throw an exception

Expect (a). Tocontain (b);//expect a (array or object) to contain B

```

-To make it easier to assemble and disassemble in complex test cases, Jasmine provides four functions:

```

Beforeeach (function)//execute the Beforeeach function before each test case (it) execution;

Aftereach (function)//execute the Aftereach function once every test case (it) is completed;

Beforeall (function)//execute the Beforeall function once before all test cases are executed;

Afterall (function)//execute the Afterall function once all test case execution is complete;

```


-Example: http://ued.fanxing.com/javascriptdan-yuan-ce-shi-kuang-jia-jasmine/


Resources:

-Jasmine Official website: http://jasine.github.io/

-Jasmine github:https://github.com/jasmine/jasmine/


This article from "front-end" blog, declined reprint!

JavaScript Automated Unit Testing

Related Article

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.