Package Naming Strategies for Go Unit Testing
In Go, there are multiple strategies for naming test packages. Understanding their pros and cons aids in selecting the most appropriate strategy for your project.
Strategy Analysis
Strategy 1:
This strategy locates the test code in the same package as the code being tested. It allows access to non-exported identifiers, enabling unit tests to examine internal variables, functions, and methods. However, appending *_test to test packages can create naming inconsistencies.
Strategy 2:
This strategy places the test code in a separate package. It ensures that the test code only accesses exported identifiers, enabing black-box testing. However, it limits access to non-exported identifiers, which can be a disadvantage for unit tests requiring such access.
Strategy 3:
This strategy is a variant of Strategy 2, allowing test code to access non-exported identifiers by importing myfunc using the '.' notation. It combines the benefits of both strategies, but can introduce namespace collisions if multiple packages use the '.' notation.
Which Strategy to Choose?
The choice depends on the testing approach:
The above is the detailed content of What's the Best Go Package Naming Strategy for Unit Testing?. For more information, please follow other related articles on the PHP Chinese website!