Example renaming of some tests to the Given When Then style#1
Example renaming of some tests to the Given When Then style#1mario-paniccia wants to merge 4 commits intomasterfrom
Conversation
|
Thanks for the naming proposal @mario-paniccia. Looks good, couple of comments/suggestions from me: It might be worth adding an example for unit tests, some of which test multiple public/package-private methods in the one test class. In those cases, the challenge can be easily seeing which method is actually being tested, as the entry-point can be obsured if there is a lot of test code. Some tests bake the method name being tested into the start of the test name, which I think is useful. The downside of this is the test names wouldn't be automatically updated if the method name gets refactored, so there's a risk of name rot, so maybe there's a better way? On the subject of name rot, it may be better to make the titles less-specific in some areas, as they are less likely to go out of sync with any changes in behaviour. E.g.: Could become: I'd argue this approach still retains the majority of the value of the descriptive test name, and the reader can look to the assertion to see the specific error being returned. |
|
Definitely a big improvement on what we currently have. I would agree with @chrisbellingham-hmcts point about keeping the names more general. If written well by encapsulating stages of the test logic then the full scenario should described within the test method. |
Hi @chrisbellingham-hmcts, thanks for your valuable comments.
|
|
Yeh I agree with Mario with regards to less specific titles. Surely if output of the test is changing, the dev should just update the name? |
But some of the scenarios are quite complex so the test names would become too long and themselves difficult to read. I think along with fixing test names we need to be encouraging better readability in the test implementation. Similar to a regular method, you don't specify the internal logic in the method name, you just specify it enough to make the context of its execution clear. |
@tonymort I think that is an indication that the test is too complex and should be broken down into simpler tests. Something that should be tackled separately though |
|
Agree that devs and reviewers should make the effort to review test names when things change but I think it's inevitable there will be occassions where this is missed. Incorrect documentation is worse than no documentation, so going more specific may have more downside than upside. It's maybe a question of what do we want to know when reading a test name? For me I want an idea of:
|
Yeah agree, we have many examples of that in test suite. |
simplified some test names by omitting the given clause when not needed
fdbaecf to
8e9d6f2
Compare
8e9d6f2 to
710a922
Compare
4ccb9d5 to
64c8966
Compare
|
I've tackled the review comments, please let me know if this looks better now.
|
|
Ok cheers all, and looks good @mario-paniccia. Further to the point about generic test names, that's fine, as long as we don't make the test names so generic that there is not a clear differentiation between multiple tests within a class. |
|
|
||
| @Test | ||
| void updateTranscriptionShouldReturnTranscriptionNotFoundError() throws Exception { | ||
| void whenUpdateTranscriptionForNonExistingTranscriptionIsRequested_thenErrorIsReturned() throws Exception { |
There was a problem hiding this comment.
don't you need a given statement on here?
There was a problem hiding this comment.
@jackmaloney I initially had the test name as:
givenRequestedTranscriptionDoesNotExist_whenUpdateTranscriptionIsRequested_thenTranscriptionNotFoundErrorIsReturned
but the given part there felt a bit "forced". In this scenario I think there really isn't a valid precondition.
So I just renamed the test omitting it, which I think it's ok sometimes.
But if you prefer always having a given, we can use the initial name. Let me know
|
|
||
| @Test | ||
| void getTranscriptionNotFound() throws Exception { | ||
| void whenGetTranscriptionDetailsForNonExistingTranscriptionIsRequested_thenErrorIsReturned() throws Exception { |
There was a problem hiding this comment.
Should this be givenANonExistingTranscription_whenGetTranscriptionDetailsIsRequested_thenAnErrorisReturned()
There was a problem hiding this comment.
hi @mestebanez, you raised a good point. @jackmaloney raised a similar one here.
Please let me know your preference
|
|
||
| @Test | ||
| void testMapToMedia() { | ||
| void givenCourtroomExistsOrCreated_whenMapCourtroomAudioMetadataToMediaIsRequested_thenMetadataIsSuccessfullyMapped() { |
There was a problem hiding this comment.
This could get tedious when writing lots of unit tests. I tend to think of these as shouldDoSomething.
There was a problem hiding this comment.
hey @davet1985, it's true, for some test types it takes a bit of diligence and effort to come up with a good given/when/then name
hey cmagowan-hmcts, |


No description provided.