Initial Knowledge Unit Test

Source: Internet
Author: User

I. DEFINITION of UNIT tests

Unit Testing is the testing of correctness tests for program modules (the smallest unit of software design).

There is also the definition of a unit test: a unit test is a piece of code written by a programmer that executes another section of code and determines whether the behavior of that code is consistent with the programmer's expectations.

Ii. Some principles of unit testing

1, the test code must do a few things:

Prepare the various conditions required for testing (create all required objects, allocate necessary resources, etc.)

Call the method you want to test

Verify that the behavior and expectations of the method being tested are consistent

Clean up all resources after completion

2. Unit Test content:

6 a few aspects: Right -bicep

Right --whether the result is correct;

B --are all the boundary conditions correct?

I --Can you check the reverse link?

C -Can you cross-check the results with other means?

E --Can you force error conditions to occur?

P --Do you meet performance requirements?

Detailed explanation

R ight : The result is correct

If the code works correctly, how do you know it is correct?  

1. Use more specific design documents

2. Real Environment data

3. ????  

B: Boundary conditions

At least as many special or unexpected situations as possible, the test program will work properly, such as:

  • completely forged or inconsistent input data, such as " (*@q!&#?± file.
  • Malformed data, such as an incorrectly formatted e-mail address
  • Null or incomplete value
  • A value that is a far cry from the expected reasonable value, such as age 10000
  • If the requirement is a duplicate value that is not allowed to occur list, but passing in a list with duplicate values
  • The requirement is an orderly list, but passing in an unordered list
  • the order of processing is wrong or inconsistent with the expected order. Try printing If you are not logged in to the system.  

of boundary conditions -correct

Whether the conformance (consistency) value is the same as expected

Whether the ordering (sequential) value should be ordered or unordered

Range (interval) value is in a reasonable range

Whether the Reference (dependency) code references resources outside the control of the code itself

Existence (existence) value exists (whether non-null, nonzero, in the collection, and so on)

Cardinality (cardinality) exactly enough value

Time (relative or absolute) does everything happen in order? Is it at the right time? Is it just in time?  

I: Check for reverse correlation

often some of the results can be used to verify that they are correct using inverse logic, such as: the function for calculating a*b, the test method is as follows:

Publicvoid Usinginverse () {

Double x = mymath.ab (bis);

Assert.areequal<double> (X,4*4);

C: Cross check with other means

There can be multiple algorithms for calculating a result, and the same algorithm can use a stable version to verify the newly improved version , such as:

Publicvoid usingstd () {

Double number = 23214.01;

double result1 =mymath. Old Method (number1);

double result2 =mymath. New Method (number1);

Assert.areequal<double> (RESULT1,RESULT2);

E: Forcing an error condition to occur

Real operating environment a variety of unexpected things can happen, such as power outages, broken nets and so on. Simulating these situations in a test can be accomplished using mock objects.  

P: Performance characteristics

such as the function of data collection, it is normal to collect on 10 websites, then how fast does it take to collect it on 1000 websites or more websites? Do you want to write a unit test?

3, the good test has the quality, collectively a-trip:

Automation ( Automatic )

Thorough ( Thorough )

Repeatable ( Repeatable )

Independent ( Independent )

Professional ( Professional )

Detailed explanation

Thorough thoroughly.

To test all situations that may be problematic, one extreme is that each line of code, the branch that the code can reach, each possible exception, and so on, can be used as the test object. The other extreme is that you only test the most likely situations ---boundary conditions, incomplete and deformed data, and so on. However, these are based on the project requirements of the decision-making problem . these are summed up as "code coverage".

Repeatable REPEATABLE

tests should be independent of all other tests and must be independent of the surrounding environment. There is only one goal, that is, the test should be able to run again and again in any order and produce the same results. If the results are different, there is a BUG.

Independent, Independent.

when writing tests, be sure to test only one thing at a time, but it doesn't mean a only one assert can be used in a testmethod, but a test function should focus on a function in the product code, or a set of functions that combine and provide an attribute together.

Professional professional.

the test code must be written in the same style as the product code. This means that you need to extract common and repetitive code and put them into a functional class that can be reused, as well as the code for unit tests ------Maintenance Encapsulation, dry principle, and reduced coupling.

The significance of unit testing


There are two pictures of the comparison, the first is "test immediately" the picture, that no unit test, the code to knock, after a line to do debugging or run to see if it meets the expected value, the second picture means "unit test", write a function or method immediately test, Fine granularity can see the direct effect of each unit, although the start time is much more, but in the late integration testing, functional and performance testing phase can effectively guarantee the code quality, reduce testing time. So, unit testing is what makes our work easier and makes our designs better, greatly reducing the time we spend on debugging.

The core of unit testing : The use of simple and effective techniques is to make the code more perfect .

PS : personal feelings: To do unit testing, first of all, you have to agree with unit testing, believe that unit testing can bring you more benefits, willing to do unit testing, always behind the urge to drive everyone to do unit testing, is not much meaning of one thing.


Initial Knowledge Unit Test

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.