In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
The basic usage of Go unit testing is believed to be at a loss for many inexperienced people. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. introduction of the basic rules of unit testing
Unit testing of Go is easier to implement because the Go language provides us with a framework for unit testing. The framework for unit testing needs to follow the following rules.
Rule 1. The go file of the unit test code must end with _ test.go, and the Go language test tool will only recognize files that conform to this rule.
Rule 2. The function name of the unit test must begin with Test and is exportable. Note: the function name is preferably the name of the method function to be tested by Test+
Rule 3. The signature of the test function must accept a pointer to the testing.T type as an argument, and the test function cannot return any value
Rule 1:
Example: the unit test file is not named at the end of _ test.go.
$go test / / Note: when there are only main.go and maintest.go in the ut directory, the following prompt will be displayed
? _ / Users/XXXX/ut [no test files] / / execution results show that no test files have been found.
Rule 2:
Example: the test function does not start with Test
Go test will prompt the alarm: testing: warning: no tests to run
How to run a unit test
The unit test code is as follows:
Main.go + main_test.go
$go test-v-cover
Introduction:
/ /-v, showing the details of the unit test
/ /-cover displays the coverage of the test code
/ / UT test results displayed by go test and time spent
Third, table-driven test method:
If a function has many logical branches, it will lead to a large number of test cases for testing the function, but most of these test cases are similar. In this case, the table-driven method (Table Driven) can play a role.
Here is an example of how to use a table-driven method:
$go test table_test.go table.go-v
Painting cymbals
Table-driven testing typically takes three steps:
1. Define the corresponding data structure for test result verification.
two。 Construct several test scenarios corresponding to the test function.
3. Call the function under test in a loop and verify it.
Note:
The above introduction is only the most basic testing knowledge of unit testing, the actual engineering unit testing is much more complex, much more complex. In particular, the functional dependencies in a module are interfaced with other modules or third-party libraries. In this case, when we do unit testing, we need functions such as stub and mock.
After reading the above, have you mastered the basic usage of Go unit testing? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.