Unit Test combat-JUnit test

Source: Internet
Author: User

First, the addition function to test

1. Instantiate the unit (method): Class name Instance name =new class name ([parameter])

2. Call the unit under test, compare the expected value and output value (actual value);

In the absence of the JUnit test tool, we will do the following test code writing process:

Ii. using JUnit for unit testing

JUnit is a unit test tool for Java-language-specific code, and is a Java-based test framework that currently has two versions: the two most commonly used test methods in Junit3,junit4.junit are:

Assertequals (expect,actual); the comparison of data for non-array type;
Assertarrayequals (expect,actual); the comparison of data used for array type;

PS: The choice of these two functions is determined by the output data type of the unit being measured.

1.Junit 3 Features

A. The test class is to inherit the TestCase class:
Import Junit.framework.TestCase
public class Testcalc extends TestCase
B. The method in the test class is determined by the method name and the method of the test case must start with a lowercase test;

Such as:

2.Junit 4

Junit 3 has great optimizations, no Junit3 constraints, which are convenient to use and for the Calc test are as follows:

3. For unit tests in Java, the type of method being tested is generally or can be converted to the following four types:

A. The method under test has a return value, and the return value is predictable. (According to the function, the specific input must correspond to the specific output); Assertequals (expected value, input value);

The above tests of the addition function belong to this class;

B. The method under test has a return value, but the return value is not fixed, is random, but the return value from the mathematical point of view, the return value must have its characteristics;

Cases:

C. The measured method has no return value, at which point it can be converted to the output type (SYSTEM.OUT.PRINTLN ());

Example: A class like dog has no return type, and usually we can convert it to a type with an output statement;


D. The test method itself throws an exception, at which point the testing emphasis is on the detection of abnormal information.

Cases:

4.Parameter parameterization

In general, in the field of automated testing, for the same unit of testing, often there are multiple test cases, at the code level, the implementation of multiple test cases, characterized by: the same logic, the data is different. In this case, it is necessary to use the parameterized operation, to some extent to achieve the separation of code and data, JUnit itself provides a parameterized way.

Cases:

To test by parameterization:

5.feeder Tools

Add the feeder plugin, create a new data folder, and right-click the new file named Isnumber.csv. (Take the above isnumber as an example)

Unit Test combat-JUnit 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.