How to do a good test? (2) Unit Testing (UT)

1. Introduction to unit testing:

Unit Testing Unit Testing (UT) is a testing method of software testing that aims to verify whether the smallest testable unit (usually a function, method or class) in the software system functions correctly. It breaks a software system into independent units and tests each unit independently to ensure that it works as expected in an isolated environment.

2. Usage scenarios:

Unit testing is suitable for the following situations:

  • For the front-end web page and mobile terminal of the online shopping system, unit testing can be performed on each module, component, function or class to verify the correctness of its functions.
  • Unit testing can be performed frequently during development to quickly identify and fix problems in the code.
  • It helps ensure that individual units can work independently before being integrated into the overall system.

3. Common technologies and tools:

When unit testing an online shopping system, you can use the following common techniques and tools:

  • Testing frameworks: such as JUnit (Java), PyTest (Python), PHPUnit (PHP), etc., provide convenient assertions and test running environments.
  • Mocking and Stubbing tools: such as Mockito (Java), unittest.mock (Python), etc., used to simulate and replace dependent external components or modules.
  • Coverage tools: such as JaCoCo (Java), Coverage.py (Python), etc., used to measure test coverage and help discover untested code parts.

4. Specific implementation methods:

Here's how unit testing is generally implemented:

  • Select unit: Based on the function and structure of the system, select an appropriate unit for testing, such as a function, method, or class.
  • Write test cases: Design test cases based on the function and expected behavior of the unit, including input data, operations to call the unit, and expected results.
  • Set up the test environment: Prepare the test environment, including required test data, simulated dependent components, etc.
  • Execute tests: Use the selected testing framework and tools to execute the written test cases and observe the actual results.
  • Judge the test results: compare the actual results with the expected results, judge whether they are consistent, and record whether the test passed or failed.
  • Fix problems: For test cases that fail, locate and fix problems until the tests pass.
  • Repeat the test: After the code is modified, re-execute the unit test to ensure that the fixed problems do not introduce new problems.
  • Recording and reporting: Record the testing process, test results, and problems found, and generate test reports.

5. Examples of test case design:

The following are unit test case designs for three examples, targeting a certain functional module in the online shopping system.

5.1.Test case 1:

  • Test case name:calculate_total_price_test
  • Test goal: Verify that the function that calculates the total price of the items in the shopping cart is correct.
  • Test prerequisite: There are multiple products in the shopping cart, and the unit price and quantity of each product are known.
  • Test steps:
    1. Call the calculation total price function and pass in the shopping cart product list.
    2. Check that the total price returned is consistent with the expected result.
  • Expected results: The expected function returns the correct value for the total price of the items in the shopping cart.
  • Actual Result: Records the actual total value returned by the function.
  • Judgment of test results: Compare whether the actual total value is consistent with the expected result.
  • Note: Different situations such as empty shopping cart, only one product, multiple products, etc. can be considered.

5.2.Test case 2:

  • Test case name: validate_coupon_code_test
    -Test goal: Verify whether the function that validates the coupon code can correctly determine the validity of the coupon.
  • Test prerequisites: A valid coupon code and an invalid coupon code are known.
  • Test steps:
    1. Call the verify coupon code function and pass in a valid coupon code.
    2. Check whether the returned result is valid.
    3. Call the verify coupon code function and pass in an invalid coupon code.
    4. Check if the returned result is invalid.
  • Expected results: The expected function correctly determines valid and invalid coupon codes.
  • Actual Result: Records the actual result returned by the function.
  • Judgment of test results: Compare the actual results with the expected results.
  • Note: More test cases can be designed based on specific coupon rules.

5.3.Test case 3:

  • Test case name:check_inventory_availability_test
  • Test goal: Verify that the function that checks product inventory availability returns the correct result.
  • Test prerequisites: One in-stock item and one out-of-stock item are known.
  • Test steps:
    1. Call the check inventory availability function and pass in the ID of the item in stock.
    2. Check if the returned result is available in stock.
    3. Call the check inventory availability function and pass in the item ID that is out of stock.
    4. Check if the returned result is that the stock is unavailable.
  • Expected results: The expected function correctly returns the availability of items in stock and out of stock.
  • Actual Result: Records the actual result returned by the function.
  • Judgment of test results: Compare the actual results with the expected results.
  • Note: Different situations such as zero product inventory, sufficient inventory, and insufficient inventory can be considered.

Guess you like

Origin blog.csdn.net/holyvslin/article/details/133309425