-
Notifications
You must be signed in to change notification settings - Fork 892
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
Add unit tests for common.go in pkg/util #5331
Add unit tests for common.go in pkg/util #5331
Conversation
c7e2e93
to
13993ee
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #5331 +/- ##
==========================================
+ Coverage 28.45% 28.48% +0.02%
==========================================
Files 632 632
Lines 43812 43812
==========================================
+ Hits 12466 12479 +13
+ Misses 30445 30437 -8
+ Partials 901 896 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
6805621
to
9ca6274
Compare
Hey @XiShanYongYe-Chang added 100% test coverage in |
Thanks @NishantBansal2003 |
Hey @XiShanYongYe-Chang, I have some doubts I hope you can resolve:
|
I'm afraid I answered wrong. do you have any specific examples?
The functions to be tested should have clear purposes and the logic can be tested. For example, different test paths can be used to form different test cases. If the test case requires the same maintenance cost as the function under test, then I don't think the test is needed. |
pkg/util/common_test.go
Outdated
added, removed := DiffKey(tt.previous, tt.current) | ||
isAddedExpected := reflect.DeepEqual(added, tt.expectedAdded) | ||
isRemovedExpected := reflect.DeepEqual(removed, tt.expectedRemoved) | ||
if !isAddedExpected && !isRemovedExpected { | ||
t.Errorf("added = %v want %v, removed = %v want %v", added, tt.expectedAdded, removed, tt.expectedRemoved) | ||
} | ||
if !isAddedExpected { | ||
t.Errorf("added = %v, want %v", added, tt.expectedAdded) | ||
} | ||
if !isRemovedExpected { | ||
t.Errorf("removed = %v want %v", removed, tt.expectedRemoved) | ||
} |
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.
How about update like this:
added, removed := DiffKey(tt.previous, tt.current)
- isAddedExpected := reflect.DeepEqual(added, tt.expectedAdded)
- isRemovedExpected := reflect.DeepEqual(removed, tt.expectedRemoved)
- if !isAddedExpected && !isRemovedExpected {
- t.Errorf("added = %v want %v, removed = %v want %v", added, tt.expectedAdded, removed, tt.expectedRemoved)
- }
- if !isAddedExpected {
+ if !reflect.DeepEqual(added, tt.expectedAdded) {
t.Errorf("added = %v, want %v", added, tt.expectedAdded)
}
- if !isRemovedExpected {
+ if !reflect.DeepEqual(removed, tt.expectedRemoved) {
t.Errorf("removed = %v want %v", removed, tt.expectedRemoved)
}
pkg/util/common_test.go
Outdated
t.Run("keys exist", func(t *testing.T) { | ||
mcs := map[string]sigMultiCluster{ | ||
kubefed.Name: kubefed, | ||
karmada.Name: karmada, | ||
} | ||
got := Keys(mcs) | ||
expect := []string{kubefed.Name, karmada.Name} | ||
sort.Strings(got) | ||
sort.Strings(expect) | ||
if !reflect.DeepEqual(got, expect) { | ||
t.Errorf("got = %v, want %v", got, expect) | ||
} | ||
}) | ||
t.Run("empty keys", func(t *testing.T) { | ||
var mcs map[string]sigMultiCluster | ||
got := Keys(mcs) | ||
var expect []string | ||
sort.Strings(got) | ||
if !reflect.DeepEqual(got, expect) { | ||
t.Errorf("got = %v, want %v", got, expect) | ||
} | ||
}) |
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.
How about rewriting this test as tabel drive as well? Just like your rewrite above.
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 thought the same. Now I'm doing it...
Signed-off-by: Nishant Bansal <nishant.bansal.mec21@iitbhu.ac.in>
9ca6274
to
bf0294b
Compare
Yeah like this - karmada/pkg/util/testing/mock_responder.go Lines 32 to 36 in 6e9136d
Is used to test TestConnectCluster in https://github.com/karmada-io/karmada/blob/master/pkg/util/proxy/proxy_test.goBut other functions like - karmada/pkg/util/testing/mock_responder.go Lines 39 to 48 in 6e9136d
Has not been used, Hence remain untested. So should I create separate tests specifically for this(untested) function, or is it unnecessary to test it? |
PTAL... |
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.
Thanks~
/lgtm
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: XiShanYongYe-Chang 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 |
I understand that this is part of the test framework, no need to test them. |
What type of PR is this?
/kind failing-test
What this PR does / why we need it:
The test case coverage for common.go has been increased to 100%.
Which issue(s) this PR fixes:
Ref #5235
Special notes for your reviewer:
To verify the changes in the pkg/util directory run the following commands:
Does this PR introduce a user-facing change?: