White-Box vs. Black-Box Testing
When selecting a package naming strategy for testing in Go, you must consider whether you prefer white-box or black-box testing.
White-box testing involves testing the internal implementation of a function or method, while black-box testing treats the function or method as a black box and focuses on testing the external behavior.
Package Naming Strategies
The three package naming strategies you mentioned differ based on whether the test code is in the same package as the code under test:
Strategy 1: Test code and code under test are in the same package (e.g., package myfunc for both).
Strategy 2: Test code and code under test are in different packages (e.g., package myfunc for the code under test and package myfunc_test for the test code).
Strategy 3: Variant of Strategy 2, imported using dot notation (e.g., import . "myfunc").
Pros and Cons
Strategy 1 (White-box Testing):
Strategy 2 (Black-box Testing):
Strategy 3 (Black-box Testing with Dot Import):
Recommendation
Consider your testing needs and the granularity required for your tests before choosing a strategy. For black-box testing, Strategy 2 or 3 is recommended. For white-box testing, Strategy 1 is recommended. You can also combine strategies within a project for different testing requirements.
The above is the detailed content of How Should I Name Go Test Packages for White-Box vs. Black-Box Testing?. For more information, please follow other related articles on the PHP Chinese website!