Unit testing all-in-one

Source: Internet
Author: User

Unit testing is the core of Dev testing. It has many advantages, but there are not many projects that truly enjoy its many advantages. I think this is mainly because people still have some misunderstandings about unit testing in our development practices. The two most common misunderstandings are:

"Functional testing and integration testing are not required as long as unit testing is available. developers can completely replace testers ."

"No new bugs can be found in unit tests. The cost is too high and no return is returned ."

 

The key to writing unit tests is to understand the external dependencies of the tested function/API, such as databases, files, registries, and other APIs.

Http://research.microsoft.com/en-us/projects/pex/molestutorial.pdf

Key points:

  • Unit test is designed for the smallest testable unit in an application.
  • The difficulty of unit test is that the program is always closely related to the external environment and how to isolate the testable unit for testing (test in isolation ).
  • Unit Test vs. Integration Test
  • Integration test is not self-contained
  • 3A pattern-arrange, act, assert
  • Two ways to isolate the tested code and external dependencies: (1) refactor the code and introduce an abstraction layer (Interface) between the tested code and external dependencies ). (2) attackers can bypass external dependencies during runtime.
  • Dependency injection-constructor Injection

 

Commonly used unit testing tools beginning mocking with Moq-1, 2, 3 to really understand Moq, you must
Expression, Expression Tree, and predicate are familiar with ===javascript code unit test ====

There are many unit test frameworks for JavaScript code, and qunit (cookbook) and jasmine are mature and popular ones. This is a long group of discussions,
Looking for a better JavaScript unit test tool. Considering
Jquery is now quite popular, and its recommended qunit is more competitive. Qunit supports synchronous and asynchronous test cases. How
Test your JavaScript code with qunit is a good article that introduces the basic knowledge of qunit, especially about asynctest.

However, the Javascript unit test framework alone is not enough. The next step is to combine the execution of these tests with CI and continuous integration systems, such as TFs. To achieve continuous integration testing similar to C # code. Unit
Testing JavaScript as part of TFS build and JavaScript
Unittest in build provides some reference directions.

Chutzapis
Is an open-source tool on codeplex. It supports JavaScript unit testing in Visual Studio (download the vsix installation package
Command Line execution unit test (download) is also supported, with the command line method, we can easily integrate chutzapis into msbuild and CI. Chutzapis
-A JavaScript test runner is a very good entry-level learning article.

Many people naturally have questions about executing JavaScript unit tests using the command line method. How does it start a browser to execute unit test cases? Answer: chutzpha depends on another open-source project phantomjs. In short, phantomjs is a headless
Browser, which is an unbounded browser. Headless testing awakened by the use of chutzapis is actually only used by phantomjs. It can also be used for: Page automation, network monitoring, and screen acquisition, and participate in quick start. Phantomjs seems to be very powerful and has done almost all the work of a standard browser! In fact, phantomjs relies on open-source browser engines.
-WebKit. WebKit is one of the three major browser engines, and the other two are gecko and Trident. Apple's Safari and Google's chrome and andriod both use WebKit. Firefox uses Gecko, while ie uses Trident.

All the friends who write unit tests must understand mock. Unit Tests for JavaScript code are also required, especially for Ajax requests. Mockjax is such
The open-source library for Ajax requests, mockjax, is easy to use and can be used together with the qunit mentioned above. Mock your
Ajax request with mockjax for rapid development is a good article about mockjax.

 

=== Dependency injection ===

Unity-
Developer's Guide to dependency Injection Using unity *****
Chapter 2 introduces factory method pattern-> simple factory pattern-> abstract factory pattern in a simple and easy-to-understand manner, and lists the characteristics of the three pattern types by comparison.

 

Dependency injection consists of register and resolve. Take unity as an example. The Register Code is as follows:

 

var container = new UnityContainer();container.RegisterType<ITenantStore, TenantStore>();

 

To obtain the required object, the Code is as follows:

   

var controller = container.Resolve<ManagementController>();

 

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.