-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing unit tests using Gomock #4110
Writing unit tests using Gomock #4110
Conversation
pkg/component/component_test.go
Outdated
@@ -29,6 +31,281 @@ import ( | |||
. "github.com/openshift/odo/pkg/config" | |||
) | |||
|
|||
// func TestGomockGetComponentFrom(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have commented out TestGomockGetComponentFrom
test because currently facing some issues while retrieving the returned value in gomock framework
pkg/component/component_test.go
Outdated
ComponentSettings: tt.cmpSetting, | ||
} | ||
|
||
cmpN := (reflect.ValueOf((mockLocalConfig.On("GetName").Return(envInfo.ComponentSettings.Name)).ReturnArguments[0])).Interface().(string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do cleanup and modify the variable name after the final decision on the tests. This is just a kind of unit test example using testify.
@@ -0,0 +1,191 @@ | |||
// Code generated by mockery v1.0.0. DO NOT EDIT. | |||
|
|||
package mocks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generated mocks for nested interface too using mockery
. I found ease of generating mocks for such kind of interfaces using mockery. However I will try to add mocks for such scenario with gomock as well which is little challenging and still working on that.
How to generator mocks using mockery
$ mockery -dir=./pkg/devfile/adapters/common/ -name "ComponentAdapter|StorageAdapter" -output ./pkg/devfile/adapters/common/mocks/
// Source: ./pkg/envinfo/envinfo.go | ||
|
||
// Package envinfo is a generated GoMock package. | ||
package envinfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocks generated for
https://github.com/openshift/odo/blob/a28e37838c5cbaa396f6675fe90a9986168165f5/pkg/envinfo/envinfo.go#L90-L97
using gomock mockgen
$ mockgen -source=./pkg/envinfo/envinfo.go -destination=./pkg/envinfo/LocalConfigProviderMocks.go -package=envinfo LocalConfigProvider
@@ -0,0 +1,99 @@ | |||
// Code generated by mockery v1.0.0. DO NOT EDIT. | |||
|
|||
package mocks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocks generated for
https://github.com/openshift/odo/blob/a28e37838c5cbaa396f6675fe90a9986168165f5/pkg/envinfo/envinfo.go#L90-L97
using testify mockery
$ mockery -dir=./pkg/envinfo/ -name LocalConfigProvider -output ./pkg/envinfo/mocks/
e03077b
to
8ef2a53
Compare
Codecov Report
@@ Coverage Diff @@
## master #4110 +/- ##
==========================================
- Coverage 42.28% 42.15% -0.13%
==========================================
Files 154 155 +1
Lines 13074 13114 +40
==========================================
Hits 5528 5528
- Misses 6954 6994 +40
Partials 592 592
Continue to review full report at Codecov.
|
For gomock comparison I am going through hard time to get it, However using mockery i have completely implemented the acceptance criteria. Issues with gomock:
@girishramnani @kadel I have fulfilled the acceptance criteria for issue #4097 using mockery and facing lots of challenges with gomock |
4e0e2c5
to
446b5c4
Compare
Observation and challenges faced to generate mocking using gomock/testify Pre-requisite:
Embedded interface mocking:
Generated mock - https://github.com/openshift/odo/pull/4110/files#diff-21de83616946ca64be5a4b32b6c372d1ed5d72a8d770c10a8b995b8f0baf1555
Writing unit tests
Thanks @mik-dass for providing useful feedback on the unit tests part over bjn. I have updated the unit tests as per our discussion. It would be great if you review once and let me know your thoughts. @kadel @girishramnani To the best of my knowledge I have jotted down all the pros and cons for both the mocking frameworks. Could you please let me know your thoughts on deciding factor. |
446b5c4
to
34ec602
Compare
platform issue /retest |
Already we have an issue for
|
/retest |
34ec602
to
d9325b1
Compare
platform issue /retest |
/retest |
/test unit |
/retest |
Final concensus is to use gomock and mockgen |
d61ba45
to
4a16805
Compare
4a16805
to
a317653
Compare
732a53c
to
5335c56
Compare
/retest |
…gomock and testify
5335c56
to
0fd0d9e
Compare
/retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code /lgtm.
However, I would have preferred having comments as well.
I've opened #4186 so that we don't miss this aspect. |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: girishramnani The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
the lgtm didn't kick in adding again 🙂 |
* Adding unit tests and mocks for interface and nested interface using gomock and testify * Add testify latest verion in go mod * Added vendor for dependencies * Added examples with both gomock and testify for interface and embeded interface * removed testify tests and mocks
What type of PR is this?
/kind feature
What does does this PR do / why we need it:
This PR is for deciding which mocking framework we should use in odo. It includes mocks for interfaces, nested interfaces and example of unit tests using gomock and testify, so that team can decide their area of comfortability in writing tests. I will add the documentation once we decide the framework.
Which issue(s) this PR fixes:
Fixes #4097
PR acceptance criteria:
Unit test
Integration test
Documentation
I have read the test guidelines
How to test changes / Special notes to the reviewer:
Unit tests should pass