• notice
  • Congratulations on the launch of the Sought Tech site

Why do developers write test cases?

As a developer, you may find that the developers around you do not like to write test cases very much, and even some students do not write test cases at all, thinking that writing test cases is a complete waste of time, or test cases are just test things.

In the development process, it is often hula to write the code, and then use interface tools such as Postman or Httpclient to request the interface, see that there is no problem, and then wait for the tester to feedback the problem.

This is probably related to the profession and the environment, some are the company does not have relevant requirements, some focus on agile development (the project and themselves always have an agile), but some students in the group asked about test cases, and I happened to be writing test cases some time ago, so I made some notes and shared them with you here.

The following is your own crude understanding, what is wrong, please point out.

Why write test cases

Test whether the logic function is normal

The test case can know the meaning by listening to the name, which is to test the logic function of the code you wrote. After all, whether the code written so hard can be run or not, there must be a place to verify it, so it can only be tested.

Unit tests and integration tests are sometimes difficult to distinguish, and can be easily distinguished by whether third-party services are called. However, in development, the tools used are actually very similar.

Unit testing: The test of a certain functional unit, in my opinion, is a test of a method, do not leap over many methods, or adjust many layers. Because the logic that calls other methods is a unit test for other methods.

Integration test: The test of integrating other components and calling third-party interfaces, such as integrating MySQL, MQ, cache, RPC components, and so on.

Refactor and sort out logic

For example, there is a reason why the Java Development Manual recommends that the length of a single method should not exceed 80 lines, because once a logic exceeds 80 lines, it means that it is time to split, and whether the internal logic is reasonable needs to be reviewed.

The logic is complex, and it is not friendly to unit tests, such as a large number of if-else, object conversions, etc., through unit tests, you can clearly sort out the functions that can be separated, so that the code structure is clearer.

Helps with Review

When reading code, if there are test cases to refer to, you can read the logic faster. Whether this code is to be reviewed by someone else or reread after a while, test cases are important. (Code comments are also important)

Prevent bugs

If you write a unit test, and when others modify the code, you suddenly find that the previous unit test cannot be executed, then the modification of this code needs to be carefully considered.

What the unit tests should be written as

You can refer to the introduction in the Java Development Manual here.

Test the tool

It can be used directly in SpringBoot and includes the following components:spring-boot-starter-test



Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

order lipitor generic & lt;a href="https://lipiws.top/"& gt;lipitor 20mg oral& lt;/a& gt; atorvastatin 80mg for sale

Acdwto

2024-03-07

Leave a Reply

+