What is the purpose of unit testing _ unit testing

What is unit testing

The factory tests each component before assembling a TV set. This is the unit test.

Unit testing (module testing) is a small piece of code written by the developer to verify that a small, well-defined function of the code under test is correct. In general, a unit test is used to determine the behavior of a particular function under a particular condition (or scene). For example, you might put a large value into an ordered list and then confirm that the value appears at the end of the list. Or, you might remove characters that match a pattern from the string and then confirm that the string does not actually contain those characters.

Unit testing is done by the programmer himself, and ultimately the programmer himself. It can be said that it is the responsibility of the programmer to write functional code and to be responsible for writing unit tests for their own code. Unit testing is performed to prove that the code behaves in the same way we expected.

In fact, we are doing unit tests every day. You wrote a function, in addition to the very simple, always have to perform a look to see if the function is normal, sometimes you have to find a way to output some data, such as pop-up information window or something, this is also unit test, put this unit The test is called a temporary unit test. Software that only performs temporary unit testing, the testing of the code is very incomplete, the code coverage is more than 70% is very difficult, the uncovered code may leave a lot of small errors, these errors will also affect each other, when the BUG is exposed It is difficult to debug when it comes out, greatly improving the cost of later testing and maintenance, and reducing the competitiveness of developers. It can be said that full unit testing is the only way to improve software quality and reduce development costs.

For programmers, if you develop the habit of unit testing your own code, you can not only write high-quality code, but also improve the programming level.

What is the purpose of unit testing _ unit testing

For full unit testing, test code should be written specifically and isolated from the product code. I think the simpler way is to establish a corresponding test project for the product project, establish a corresponding test class for each class, and build a test function for each function (except very simple). Let me first talk about my views on several concepts.

It is generally believed that in the era of structured programming, the unit referred to in unit testing refers to functions. In today's object-oriented era, the unit referred to in unit testing refers to classes. In my practice, class as a test unit, high complexity, poor operability, so still advocate the function as a unit test unit, but you can use a test class to organize all test functions of a class . Unit testing should not place too much emphasis on object orientation because local code is still structured. The unit test has a large workload, and it is the last word that is simple, practical and efficient.

There is a perception that only testing the interface of the class (public function), not testing other functions, from the object-oriented point of view, does make sense, but the purpose of the test is to find the error and eventually debug, so as long as it is included The function with a higher probability of error is tested, and it has nothing to do with whether the function is private. For C++, you can use a simple method to separate the functions to be tested: simple functions such as data read and write functions are written in the header file (inline function), all functions written in the source file must be performed Testing (except constructors and destructors).

Test purpose of unit test

First guarantee the quality of the code.

Second, ensure that the code is maintainable.

This again ensures that the code is extensible.

The code quality of one of the purposes of the code.

The code we write can detect the correct nature of the grammar through the compiler, but it does not guarantee that the code logic is correct. How can we guarantee that the code execution is correct? Ok, let's take a look at the code below.

Java code

Int add(int x, int y){

Return x+y;

}

The function module above. Below is the segment test code

Java code

Void testAdd(){

//We require the program logic to be 1+4=5;

assertEquals(5,add(1,4);

}

What is the purpose of unit testing _ unit testing

After testing, if you modify the logic of int add(int x, int y); if the modification is correct, the test code will always see green. If you are logically wrong. Sorry, your test will let you rewrite that piece of logic. Until you are correct.

There is a special case. If my test code is written like this, then I guarantee the correctness of the logic code, but I can't see the green I am looking for. What is the reason?

Java code

Void testAdd(){

//We require the program logic to be 1+4=5;

assertEquals(6,add(1,4);

}

I personally think this problem is a problem with logic code, but the logic problem in your test code, hey, MyGot, as a programmer. It has been a headache for the logic code. I have to worry about the test code. It’s really hard to be a man. I think it is true.

This leads to another question, how can we guarantee the testability of my logic code?

The maintainability of the second code of purpose.

Take the example above. As long as our unit tests are correct, then we can guarantee that no matter how you modify the code, as long as the test code can produce a green bar, then OK, the logic code you modified is correct. Of course, it is not only this, but also, for example, ensuring that this code is modified will not affect other modules.

The purpose of the third code is extensible.

I understand this superficially for this. Can only express the surface of things, hope.

For scalability I feel that the degree of coupling between a code is kept to a minimum. It’s OK. Unit testing has a strong advantage in this regard, and it can force you to write programs with low coupling for program maintainability.

Unit test advantages

1. It is a verification act.

Every function in the program is tested to verify its correctness. It provides a support for future development. Even in the late stages of development, we can easily add features or change the structure of the program without worrying about breaking down important things in the process. And it provides a guarantee for the reconstruction of the code. In this way, we can more freely improve the program.

2. It is a design behavior.

Writing unit tests will allow us to observe and think from the caller. Especially the test-first test, forcing us to program

It is easy to call and testable, forcing us to uncouple the software.

3. It is a behavior of writing documents.

Unit testing is an invaluable document that is the best documentation for showing how a function or class is used. This document is compilable, runnable, and it's up to date, always in sync with the code.

4. It is regressive.

Automated unit testing avoids code regression, and you can run tests anytime, anywhere, after writing.

Category of unit testing

It seems difficult to define a clear scope for unit testing and indicate which features belong to unit tests. But the four questions discussed below basically explain the scope of unit testing and the work to be done in unit testing.

1. Is its behavior consistent with what I expected?

This is the most fundamental purpose of unit testing. We use the unit testing code to prove that what we are doing is what we expect.

2. Has its behavior consistent with what I expected?

Writing unit tests, if you only test a correct path to the code, let it go correctly, it is not really done. Software development is a complex project. When testing whether a piece of code behaves consistently with your expectations, you need to confirm: in any case, this code is consistent with your expectations; for example, the parameters are suspicious, hard disk There is no space left, buffer overflow, and network drop.

3. Can I rely on unit testing?

Code that cannot be relied on is of little use. Since unit testing is used to ensure the correctness of the code, unit testing must also be worth relying on.

4. Does the unit test explain my intentions?

Unit testing can help us fully understand the usage of the code. In terms of performance, unit testing is like a document that can be executed, which shows what you can expect from when you call the code with various conditions.

60 Port USB Charger

60 Port USB Charging

This 60 -port USB charger is charged at the fastest speed to as many as 60 devices. The table charger has 60 ports. The maximum output of a single USB port is 2.4A, and the output power is as high as 300W. Super intelligence: Each port can charging a current of up to 2.4A, and adaptive smart chip detector can provide the best charging current for connected devices. Multiple protection: The USB charger has rush protection, over -current and overvoltage protection functions, which can safely protect your smart devices. Suitable for multiple devices: charging stations are compatible with 99 % of smart devices, such as iPad iPhone, tablet, Bluetooth headset, fan, DVS, for PSP. It is suitable for various occasions, such as hotels, school dormitories, gym and other places.

60 Port USB Charging

60 Port Usb Charger,60 Port Portable Hub,60 Port Usb Smart Charger,High Power Mobile Phone Charger

shenzhen ns-idae technology co.,ltd , https://www.best-charger.com

Posted on