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 126fcfe4a40 Enforce test naming and refine assertion standards (#39713)
126fcfe4a40 is described below
commit 126fcfe4a4080b478271b34fcde36afc76492478
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Sep 3 18:02:18 2026 +0800
Enforce test naming and refine assertion standards (#39713)
---
CODE_OF_CONDUCT.md | 8 ++++----
docs/community/content/involved/conduct/code.cn.md | 8 ++++----
docs/community/content/involved/conduct/code.en.md | 8 ++++----
src/resources/checkstyle.xml | 4 ++--
4 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
index cea10de6ef4..23bbb391066 100644
--- a/CODE_OF_CONDUCT.md
+++ b/CODE_OF_CONDUCT.md
@@ -91,7 +91,6 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- Correctness testing: Get expected results through correct inputs.
- Reasonable design: Combined with production code design, design
high-quality unit tests.
- 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.
- Tests that modify static state must restore the original state after each
test.
- Obtain SPI implementations through the project loader by default. If the
class under test implements `TypedSPI` or `DatabaseTypedSPI`, instantiate it
through `TypedSPILoader` or `DatabaseTypedSPILoader`, not with `new`.
@@ -104,9 +103,10 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- Data assertion standards should follow:
- Boolean type assertions should use `assertTrue` and `assertFalse`;
- Null value assertions should use `assertNull` and `assertNotNull`;
- - Other types should use `assertThat(actual, is(expected))` instead of
`assertEquals`;
- - Use `assertThat(..., isA(...))` instead of `instanceOf`;
- - Do not use `assertSame` / `assertNotSame`; use instead of
`assertThat(actual, is(expected))` or `assertThat(actual, not(expected))`;
+ - Non-boolean, non-null value equality assertions must use
`assertThat(actual, is(expected))`;
+ - Type assertions must use `assertThat(actual, isA(ExpectedType.class))`;
+ - Reference identity assertions must use `assertThat(actual,
sameInstance(expected))`;
+ - Reference non-identity assertions must use `assertThat(actual,
not(sameInstance(expected)))`;
- Use Hamcrest matchers like `is()`, `not()` for precise and readable
assertions.
- The actual values in test cases should be named actual XXX, and expected
values should be named expected XXX.
- Test classes and methods marked with `@Test` do not need JAVADOC.
diff --git a/docs/community/content/involved/conduct/code.cn.md
b/docs/community/content/involved/conduct/code.cn.md
index 301e2dd73dc..d4231e0a082 100644
--- a/docs/community/content/involved/conduct/code.cn.md
+++ b/docs/community/content/involved/conduct/code.cn.md
@@ -95,7 +95,6 @@ chapter = true
- 正确性测试(Correct):通过正确的输入,得到预期结果。
- 合理性设计(Design):与生产代码设计相结合,设计高质量的单元测试。
- 容错性测试(Error):通过非法数据、异常流程等错误的输入,得到预期结果。
- - 使用 `assert` 前缀命名所有的测试用例。
- 单元测试必须通过公共 API 验证行为,禁止通过反射调用私有成员。 若测试必须通过反射访问字段,应使用
`Plugins.getMemberAccessor()`,且反射仅限于 `Field` 访问。
- 测试修改静态状态时,必须在每个测试结束后恢复其原始状态。
- 默认通过项目加载器获取 SPI 实现。 如果被测类实现了 `TypedSPI` 或 `DatabaseTypedSPI`,应通过
`TypedSPILoader` 或 `DatabaseTypedSPILoader` 实例化,禁止使用 `new`。
@@ -109,9 +108,10 @@ chapter = true
- 数据断言规范应遵循:
- 布尔类型断言应使用 `assertTrue` 和 `assertFalse`;
- 空值断言应使用 `assertNull` 和 `assertNotNull`;
- - 其他类型断言应使用 `assertThat(actual, is(expected))` 代替 `assertEquals`;
- - 类型断言使用 `assertThat(..., isA(...))` 代替 `instanceOf`;
- - 禁用 `assertSame` / `assertNotSame`,使用 `assertThat(actual, is(expected))`
或 `assertThat(actual, not(expected))`;
+ - 非布尔值、非空值的相等断言必须使用 `assertThat(actual, is(expected))`;
+ - 类型断言必须使用 `assertThat(actual, isA(ExpectedType.class))`;
+ - 引用同一性断言必须使用 `assertThat(actual, sameInstance(expected))`;
+ - 引用非同一性断言必须使用 `assertThat(actual, not(sameInstance(expected)))`;
- 使用 Hamcrest 匹配器(如 `is()`、`not()`)来进行精确且可读性高的断言。
- 测试用例的真实值应名为为 actual XXX,期望值应命名为 expected XXX。
- 测试类和 `@Test` 标注的方法无需 JAVADOC。
diff --git a/docs/community/content/involved/conduct/code.en.md
b/docs/community/content/involved/conduct/code.en.md
index 7b71f6a6653..7c3f95f5c72 100644
--- a/docs/community/content/involved/conduct/code.en.md
+++ b/docs/community/content/involved/conduct/code.en.md
@@ -95,7 +95,6 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- Correctness testing: Get expected results through correct inputs.
- Reasonable design: Combined with production code design, design
high-quality unit tests.
- 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.
- Tests that modify static state must restore the original state after each
test.
- Obtain SPI implementations through the project loader by default. If the
class under test implements `TypedSPI` or `DatabaseTypedSPI`, instantiate it
through `TypedSPILoader` or `DatabaseTypedSPILoader`, not with `new`.
@@ -109,9 +108,10 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- Data assertion standards should follow:
- Boolean type assertions should use `assertTrue` and `assertFalse`;
- Null value assertions should use `assertNull` and `assertNotNull`;
- - Other types should use `assertThat(actual, is(expected))` instead of
`assertEquals`;
- - Use `assertThat(..., isA(...))` instead of `instanceOf`;
- - Do not use `assertSame` / `assertNotSame`; use instead of
`assertThat(actual, is(expected))` or `assertThat(actual, not(expected))`;
+ - Non-boolean, non-null value equality assertions must use
`assertThat(actual, is(expected))`;
+ - Type assertions must use `assertThat(actual, isA(ExpectedType.class))`;
+ - Reference identity assertions must use `assertThat(actual,
sameInstance(expected))`;
+ - Reference non-identity assertions must use `assertThat(actual,
not(sameInstance(expected)))`;
- Use Hamcrest matchers like `is()`, `not()` for precise and readable
assertions.
- The actual values in test cases should be named actual XXX, and expected
values should be named expected XXX.
- Test classes and methods marked with `@Test` do not need JAVADOC.
diff --git a/src/resources/checkstyle.xml b/src/resources/checkstyle.xml
index cb6f272670f..91dfc356323 100644
--- a/src/resources/checkstyle.xml
+++ b/src/resources/checkstyle.xml
@@ -124,8 +124,8 @@
</module>
<module name="MatchXpath">
<property name="query"
-
value="//METHOD_DEF[./MODIFIERS/ANNOTATION[(./IDENT[@text='Test'] or
./DOT/IDENT[@text='Test'] or ./IDENT[@text='ParameterizedTest'] or
./DOT/IDENT[@text='ParameterizedTest'])] and not(./IDENT[starts-with(@text,
'assert')])]" />
- <message key="matchxpath.match" value="Method names for @Test and
@ParameterizedTest must start with 'assert'." />
+
value="//METHOD_DEF[./MODIFIERS/ANNOTATION[(./IDENT[@text='Test'] or
./DOT/IDENT[@text='Test'] or ./IDENT[@text='ParameterizedTest'] or
./DOT/IDENT[@text='ParameterizedTest'] or ./IDENT[@text='RepeatedTest'] or
./DOT/IDENT[@text='RepeatedTest'] or ./IDENT[@text='TestFactory'] or
./DOT/IDENT[@text='TestFactory'] or ./IDENT[@text='TestTemplate'] or
./DOT/IDENT[@text='TestTemplate'])] and not(./IDENT[starts-with(@text,
'assert')])]" />
+ <message key="matchxpath.match" value="Test method names must
start with 'assert'." />
</module>
<module name="IllegalType" />
<module name="MissingSwitchDefault" />