Introduction to Unit Testing

Source: Internet
Author: User

Unit testing is a white-box test. It is the first and most important part of all tests.

It is mainly used for testing the public method of a class.

Writing unit tests is an important way to verify the behavior of Class Functions and optimize the design. Therefore, it is the work of programmers.


Unit Test Writing Process

  1. Retrieve a test case from the test list

  2. Define a test method

    Public void test _ {methodname }_{ inputparameter }_{ expectation}

    {

    // Prepare test data

    // Execute the tested method

    // Verify the expected results

    }

  3. Prepare test data

    This mainly includes building parameters of the tested method, simulating the dependent objects called by the tested method, and assigning values to the tested objects through the set attribute.

  4. Method for executing the test

  5. Verify expected results

    Asserted the method to be tested, and asserted the call status of the simulated object.


Here is a specific example:

[Test]
Public void test_read_singlekey_successful ()
{
Memcachedclient = schonerstorage. memcacheclient. cacheclient;


// Prepare test data

String businesskey = "100_1 ";
Byte [] returndata = new byte [] {1, 23, 12, 1 };
Reverse CT. Call (memcachedclient. Get (businesskey). Return (returndata );
Memcachedclient. Replay ();


// Execute the tested method

Byte [] actualdata = schonerstorage. Read (businesskey );


// Verify the expected results
Assert. areequal (returndata, actualdata );
Memcachedclient. verifyallexpectations ();
}



Unit Test naming rules

  1. The project that stores the test class is named {name of the project to be tested}. unittest

  2. The test class maintains the same directory structure or package structure as the project to be tested, and is named {tested class} Test

  3. Test Cases in the test class are named as test _ {tested method }_{ test point }_{ expected test result }.

    {Test point} can be a parameter type, parameter boundary value, and so on.



Unit Test considerations

Isolate the dependencies of test cases (unit test tools such as xunit usually ensure this)

Test cases should cover normal workflows, abnormal workflows, input parameter boundary values, etc.

The granularity of test cases cannot be too large. If a test case has too many vertices to be verified, You can see whether it can be split into smaller cases.


Test-driven development (TDD)

 

TDD is a brand new development model, which forces developers to write unit tests before writing product code. The process is as follows:

  1. Abstract The classes and interfaces involved in a feature and their dependencies in units of a feature.

  2. Write test cases for the class (first, list the test list in the test Class header, and then write the test cases according to a certain priority)

  3. Run the test case. The test case failed.

  4. Compile product code

  5. Run the test case. The test case passes

  6. Repeat 3-5 steps to complete the test case of the entire class.

  7. Refactor product code or test cases to eliminate repeated designs and optimize the code structure

  8. Make sure all test cases pass

  9. Submit code to the product Library

  10. CI server performs integration test on the unit test.

  11. If any test case fails, repair the failed test case first before developing the new product code.


TDD helps developers develop product code from the user's perspective, and keeps the product codeSimple and available.

Introduction to Unit Testing

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.