- The inclusion of
_it_
seems unnecessary in the context of these isolated tests, UNLESS additional clarity is desired, in which case this should include the function name under test, which seems pretty common.
it
is often used in JS tests/specs in the sense:
describe( 'disableBlockEditorForNavigationPostType', () => {
// Some suites use "it"
it( 'should only disable block editor for navigation post types', () => {})
// Some suites use "test"
test( 'it should only disable block editor for navigation post types', () => {})
// For suites using "test", removing "it" is common
test( 'should only disable block editor for navigation post types', () => {})
})
Converted to PHPUnit, the third example above would look like:
class Tests_Editor_DisableBlockEditorForNavigationPostType extends WP_UnitTestCase {
public function test_should_only_disable_block_editor_for_navigation_post_types() {
}
}
Each of these migrated tests have the same test method name,
test_it_correctly_handles_different_post_types()
, which seems awkward compared with others in the project. Maybe it's just me, but an alternative might be to simplify it totest_handle_different_post_types()
.
My issue with handle
- and admittedly, I've used it myself in the past, is that it doesn't describe the expected behaviour.
i.e. test_handle_different_post_types()
vs test_should_only_disable_block_editor_for_navigation_post_types()
.
By using test_should_
and test_should_not_
prefixes where possible, that means:
- Raw output:
Tests_Editor_DisableBlockEditorForNavigationPostType::test_should_only_disable_block_editor_for_navigation_post_types
Tests_Editor_DisableBlockEditorForNavigationPostType::test_should_not_disable_block_editor_for_missing_post_types
--testdox
output:
_Editor_DisableBlockEditorForNavigationPostType
โ Should only disable block editor for navigation post types
โ Should not disable block editor for missing post types
Theoretically, _should_
may not be necessary, but it gives a clear indication of the expected result before the reader even knows the remaining context, and we'll often internally say "Okay, this function should only / should not do something", so this saves the reader time.
Thanks, @costdev! I originally had a much longer comment, too (including sample
--testdox
output!), and stripped it down quite a bit, because it was getting really pedantic, a sin that I struggle to avoid daily ๐ The gist is a good alternative for discussion.The non-unique function names, and use of "it" and "correctly", were what I had the most trouble grappling with. The JS test context you provide makes sense, but rubs up against what I've seen in existing PHPUnit tests in the project, hence the raised flag (...in the context of someone who's only been on the core project for ~8 months, so grain of salt and all).
I do like the examples you provided, which are much more precise as to the intent and what's happening with the tests, and amend my suggestions along those lines. What do you and @anton-vlasenko think about these?
For
editor/disableBlockEditorForNavigationPostType.php
:test_should_only_disable_block_editor_for_navigation_post_types()
test_should_not_disable_block_editor_for_non_navigation_post_types()
editor/disableContentEditorForNavigationPostType.php
:test_should_only_disable_content_editor_for_navigation_post_types()
test_should_not_disable_content_editor_for_non_navigation_post_types()
editor/enableContentEditorForNavigationPostType.php()
:test_should_only_enable_content_editor_for_navigation_post_types()
test_should_not_enable_content_editor_for_non_navigation_post_types()
I swapped "missing" for "non-navigation" since the former implies something else if you were to say "a missing post type".
And for
kses/wpFilterGlobalStylesPost.php
:test_should_not_remove_safe_global_style_rules()
test_should_only_remove_unsafe_global_style_rules()
<-- Does it make sense for us to add an opposing test to validate that the function does filter out unsafe rules?