nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

[Bug]: Excessive underscores in test function names

Open dturner opened this issue 3 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Is there a StackOverflow question about this issue?

  • [X] I have searched StackOverflow

What happened?

For example, in https://github.com/android/nowinandroid/blob/607c24e7f7399942e278af663ea4ad350e5bbc3a/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstUserDataRepositoryTest.kt

Check all codebase and use maximum of 2 underscores per test in the format: given_when_then

Relevant logcat output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

dturner avatar Oct 20 '22 11:10 dturner

I can take this

Kenny50 avatar Nov 01 '22 12:11 Kenny50

@dturner I am struggle with some decision

  1. ForYouViewModel Original test naming is expect_condition, like stateIsLoadingWhenFollowedTopicsAreLoading, I don't understand the reason, but I prefer using direct statement like whenFollowedTopicsAreLoading_thenStateIsLoading
  2. ListToMapMigration, IntToStringMigration I never heard naming convention in test start with capitalize, I would like to change ListToMapMigration_should_migrate_bookmarks into listToMapMigration_shouldMigrateBookmarks
  3. NewsResourceDaoTest newsResourceDao_filters_items_by_topic_ids_or_author_ids_by_descending_publish_date test with long condition expression would looks like newsResourceDao_filtersItemsByTopicIdsOrAuthorIdsByDescendingPublishDate , but this change doesn't make the name more readable
  4. NewsResourceTypeConverterTest Not sure the object in test naming, the original version test_room_news_resource_type_converter_for_video, although testRoomNewsResourceTypeConverter_toVideo will be more straightforward, however the change from for to to means different object in sentence

any advice?

Kenny50 avatar Nov 05 '22 07:11 Kenny50

I think that looks fine @Kenny50

chanjungkim avatar Mar 22 '23 16:03 chanjungkim

This could be a lint 🤓 and pretty easy to write too. But I'm not sure if this would fit in such a "app architecture showcase" repository. An other simpler solution is to simply use Kotlin's backticks for method names and use spaces.

@Test
fun `given X when Y then Y`()

@dturner let us know what's the best approach here.

SimonMarquis avatar May 08 '23 07:05 SimonMarquis

Although, there already is a DesignSystemDetector custom lint, so, this might not be a bad idea to add one for test method conventions after all?

SimonMarquis avatar May 10 '23 11:05 SimonMarquis

I seem to remember there was some issue which prevented us using backticks for test names.

@JoseAlcerreca Any thoughts?

dturner avatar May 10 '23 13:05 dturner

Backticks will not work on Android instrumented tests, but they work fine on regular unit tests.

SimonMarquis avatar May 10 '23 19:05 SimonMarquis