Open pav-kv opened 9 months ago
Hi @pav-kv, For the code you mentioned in "Eliminate Boilerplate", I am interested in improving it and have the time to do so. If I understand correctly, the following code
if sm.state != tt.wstate {
t.Errorf("#%d.%d state = %v , want %v", i, j, sm.state, tt.wstate)
}
Should it be changed into something like this?
assert.Equal(t, tt.wstate, sm.state, "#%d.%d state = %v , want %v", i, j, sm.state, tt.wstate)
If you were expecting that, I would like to work on this as a starting point to contribute to etcd.
@MrDXY Yes, the idea is right. Feel free to send a PR.
In most cases we don't need the formatted text part. It's enough to do this:
assert.Equal(t, tt.wstate, sm.state)
There are some cases in which we need to add some information to the error, to identify the sub-test that failed. The example you quoted would then be like:
assert.Equal(t, tt.wstate, sm.state, "#%d.%d", i, j)
Also, t.Fatalf
should correspond to require
package calls, and t.Errorf
should correspond to assert
package calls.
Noted. Thanks! I'll give it a try.
Hi, @pav-kv I found multiple files that use t.Error/Fatal. While not all the files require a refactor, I am listing them for awareness. I plan to begin with raft_test.go
, and will split this work into multiple PRs to make it easy to review. Would that be acceptable to you?
@MrDXY Please don't reference this issue from individual commits. This spams the history of this issue (see above: every time the commit is updated, it posts a new link to this issue). Instead, please reference the issue only from the PR description.
Hey @pav-kv , it's been a while since I was on this. IMHO, I think the node_util_test.go
file doesn't need refactoring. If you agree, we can consider the refactor job for require/assert complete. What do you think? 😄
@MrDXY SGTM. Thanks a lot for this clean-up!
This is an umbrella issue for all kinds of unit-test improvements. Unit-tests in this repository are significantly outdated, and need a revamp. For example:
Examples of things to fix/improve:
RawNode / raft
structs arbitrarily, which breaks internal invariants and makes tests prone to false failures on any change. This necessitates PR authors to spend time debugging and fixing incorrect tests.require/assert
packages. https://github.com/etcd-io/raft/issues/146#issuecomment-1972343245r.Term >= r.raftLog.lastTerm()
. Tests that manipulateRawNode
structs directly may accidentally break invariants, and end up testing arbitrary behaviour rather than real one.