Improvement: Reduce Boilerplate in Tests #229
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Okay, this is just a proof of concept, not ready to merge. Wanted to see what the response is to this before continuing.
Instead of the current method of using validateFunc to compare response bodies, I've come up with a new process.
First, in the test function, you define a custom struct that represents the expected structure of the response JSON, and then create an expectedBody value of this type:
And you place this expectedBody into a TestCase instance, as an interface.
In the test runner function, you mostly do the same thing as runTestCases: iterate through the tests, run the query, get the response, compare status code and content type, etc. All that would stay the same.
But then, instead of using validateFunc to compare the expectedBodies, instead I did this:
Using TestApi as a proof-of-concept, this seems to be working. I checked and it seems that the == operator is able to perform deep comparisons of interfaces, even if the values are complex nested structs.
Some of the reflection code is a little hard to read, and I'm not sure this is the most "golang" way to do this, but it does work. And I think this would massively reduce boilerplate in the actual test functions, and would make writing new tests or adding test cases much easier.
Please let me know what you think of this approach, if it seems like a good solution I'll try rewriting the first few cases of publishers endpoints and see how that looks.