You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/08/16 08:45:30 UTC
(shardingsphere) branch master updated: Refine unit testing standards (#39483)
This is an automated email from the ASF dual-hosted git repository.
terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 228e98b3274 Refine unit testing standards (#39483)
228e98b3274 is described below
commit 228e98b3274695c14a2c07f76459c76943403099
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Aug 16 16:45:24 2026 +0800
Refine unit testing standards (#39483)
---
CODE_OF_CONDUCT.md | 2 ++
docs/community/content/involved/conduct/code.cn.md | 2 ++
docs/community/content/involved/conduct/code.en.md | 2 ++
3 files changed, 6 insertions(+)
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
index 19c19e99f6e..5e83cb13da5 100644
--- a/CODE_OF_CONDUCT.md
+++ b/CODE_OF_CONDUCT.md
@@ -97,6 +97,7 @@ The following code of conduct is based on full compliance with the [Apache Softw
- Error tolerance testing: Get expected results through incorrect inputs such as illegal data, exception flows, etc.
- Use `assert` prefix for all test method names.
- Unit tests must exercise behavior through public APIs only. Reflection-based invocation of private members is forbidden. If tests must access fields via reflection, use `Plugins.getMemberAccessor()` and limit reflection to `Field` access only.
+- Every unit-test class must directly test a corresponding production class and be named `<ProductionClassName>Test`, using the exact simple name of the production class.
- When a production method is covered by only one test case, name that test method `assert<MethodName>` without extra suffixes, and prefer isolating one public production method per dedicated test method; when practical, keep test method ordering aligned with the corresponding production methods.
- For parameterized tests, provide display names via parameters and use `"{0}"` as the display-name template.
- Each test case needs precise assertions, try not to use `not`, `containsString` assertions.
@@ -116,6 +117,7 @@ The following code of conduct is based on full compliance with the [Apache Softw
- When unit tests contain objects that are not easy to construct, for example: objects with more than two levels of nesting and unrelated to testing, `mock` should be used.
- For mocking static methods or constructors, consider using `AutoMockExtension` and `StaticMockSettings` provided by the testing framework for automatic resource release; if using Mockito's `mockStatic` and `mockConstruction` methods, must be paired with `try-with-resource` or closed in cleanup methods to avoid leaks.
- When verifying only one call, there's no need to use `times(1)` parameter, the single-parameter method of `verify` is sufficient.
+- Do not stub methods or verify interactions that do not affect the behavior or result being tested. Omit stubbing when Mockito's default return value is sufficient.
- For deep chained interactions, use Mockito’s `RETURNS_DEEP_STUBS` instead of layering intermediate mocks.
- Test data should use standardized prefixes (e.g., `foo_`/`bar_`) to clearly identify their test purpose
- Use `PropertiesBuilder` simplify `Properties` building.
diff --git a/docs/community/content/involved/conduct/code.cn.md b/docs/community/content/involved/conduct/code.cn.md
index aec8dbe246d..b8b1181c490 100644
--- a/docs/community/content/involved/conduct/code.cn.md
+++ b/docs/community/content/involved/conduct/code.cn.md
@@ -101,6 +101,7 @@ chapter = true
- 容错性测试(Error):通过非法数据、异常流程等错误的输入,得到预期结果。
- 使用 `assert` 前缀命名所有的测试用例。
- 单元测试必须通过公共 API 验证行为,禁止通过反射调用私有成员。 若测试必须通过反射访问字段,应使用 `Plugins.getMemberAccessor()`,且反射仅限于 `Field` 访问。
+ - 每个单元测试类必须直接测试一个对应的生产类,测试类必须使用生产类的准确简单类名并命名为 `<ProductionClassName>Test`。
- 当某个生产方法只由一个测试用例覆盖时,测试方法命名为 `assert<MethodName>`,无额外后缀。
- 每个公有方法使用一个独立的测试方法,测试方法顺序在可行时与生产方法保持一致。
- 参数化测试需通过参数提供显示名,并使用 `"{0}"` 作为展示名模板。
@@ -121,6 +122,7 @@ chapter = true
- 单元测试包含不容易构建的对象时,例如:超过两层嵌套并且和测试无关的对象,应使用 `mock`。
- 模拟静态方法或构造器,应优先考虑使用测试框架提供的 `AutoMockExtension` 和 `StaticMockSettings` 自动释放资源;若使用 Mockito `mockStatic` 和 `mockConstruction` 方法,必须搭配 `try-with-resource` 或在清理方法中关闭,避免泄漏。
- 校验仅有一次调用时,无需使用 `times(1)` 参数,使用 `verify` 的单参数方法即可。
+ - 不得对与当前测试所验证的行为或结果无关的方法进行 stub,也不得 verify 此类交互。当 Mockito 的默认返回值足以满足测试需要时,应省略 stub。
- 深度链式交互使用 Mockito 的 `RETURNS_DEEP_STUBS`,不要层层手动 mock。
- 测试数据应使用标准化前缀(如 `foo_`/`bar_`)明确标识其测试用途。
- 使用 `PropertiesBuilder` 简化 `Properties` 构造。
diff --git a/docs/community/content/involved/conduct/code.en.md b/docs/community/content/involved/conduct/code.en.md
index 271413801e3..820677dee8d 100644
--- a/docs/community/content/involved/conduct/code.en.md
+++ b/docs/community/content/involved/conduct/code.en.md
@@ -101,6 +101,7 @@ The following code of conduct is based on full compliance with the [Apache Softw
- Error tolerance testing: Get expected results through incorrect inputs such as illegal data, exception flows, etc.
- Use `assert` prefix for all test method names.
- Unit tests must exercise behavior through public APIs only. Reflection-based invocation of private members is forbidden. If tests must access fields via reflection, use `Plugins.getMemberAccessor()` and limit reflection to `Field` access only.
+- Every unit-test class must directly test a corresponding production class and be named `<ProductionClassName>Test`, using the exact simple name of the production class.
- Except for simple `getter /setter` methods, unit tests need full coverage.
- When a production method is covered by only one test case, name that test method `assert<MethodName>` without extra suffixes, and prefer isolating one public production method per dedicated test method; when practical, keep test method ordering aligned with the corresponding production methods.
- For parameterized tests, provide display names via parameters and use `"{0}"` as the display-name template.
@@ -121,6 +122,7 @@ The following code of conduct is based on full compliance with the [Apache Softw
- When unit tests contain objects that are not easy to construct, for example: objects with more than two levels of nesting and unrelated to testing, `mock` should be used.
- For mocking static methods or constructors, consider using `AutoMockExtension` and `StaticMockSettings` provided by the testing framework for automatic resource release; if using Mockito's `mockStatic` and `mockConstruction` methods, must be paired with `try-with-resource` or closed in cleanup methods to avoid leaks.
- When verifying only one call, there's no need to use `times(1)` parameter, the single-parameter method of `verify` is sufficient.
+- Do not stub methods or verify interactions that do not affect the behavior or result being tested. Omit stubbing when Mockito's default return value is sufficient.
- For deep chained interactions, use Mockito’s `RETURNS_DEEP_STUBS` instead of layering intermediate mocks.
- Test data should use standardized prefixes (e.g., `foo_`/`bar_`) to clearly identify their test purpose
- Use `PropertiesBuilder` simplify `Properties` building.