-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Improved tests for a logging exporter and jaeger exporter #377
Improved tests for a logging exporter and jaeger exporter #377
Conversation
@agneum thank you for contributing! Please make sure the build passes and the maintainers will review the PR. |
@tigrannajaryan Thank you, fixed |
return | ||
} | ||
assert.NoError(t, err, "nil config") | ||
assert.NotNil(t, got) |
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.
It is better to use require.NotNil
here because got
is used below and will crash the test when it is nil (assert
does not stop test execution, require
does).
|
||
// This is expected to fail. | ||
err = got.ConsumeTraceData(context.Background(), consumerdata.TraceData{}) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewFails(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.
Strictly speaking there was no need to split TestNewFails as separate function to be able to use assert
, but this looks OK to me and probably is a bit clearer than having positive and negative cases in one function so let's keep it.
|
||
// This is expected to fail. | ||
err = got.ConsumeTraceData(context.Background(), consumerdata.TraceData{}) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewFails(t *testing.T) { | ||
tests := []struct { |
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.
Since there is only one test here the value of having an array of test definitions is unclear here.
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 assumed that other cases could be added here in the future. But you're right, for now there is only one. So I fixed this test.
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.
Let's use the principle of YAGNI here. :-)
We generally aim to have minimum amount of code that serves the purpose.
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := New(tt.args.config, tt.args.httpAddress, tt.args.headers, tt.args.timeout) | ||
assert.EqualError(t, err, "nil config") |
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.
This assumes all test cases in tests
(if we had more than one) will return the same error message. This may not be true. Generally unclear why we have tests
array and loop through it if it is one element.
@@ -66,66 +66,81 @@ func TestCreateInstanceViaFactory(t *testing.T) { | |||
|
|||
func TestFactory_CreateTraceExporter(t *testing.T) { | |||
tests := []struct { |
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.
Same here, tests
with single element.
t.Fatalf("Wanted nil got %v", err) | ||
} | ||
assert.NotNil(t, lte) | ||
assert.NoError(t, err) |
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.
👍
if err != nil { | ||
t.Fatalf("Wanted nil got %v", err) | ||
} | ||
assert.NotNil(t, lte) |
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.
This needs to use require
instead of assert
otherwise the test will crash if nil is returned. Same in other similar places.
@agneum thank you for working on this. Please make sure |
kicking another build to see if we get coverage numbers for this PR |
Codecov Report
@@ Coverage Diff @@
## master #377 +/- ##
=======================================
Coverage 74.81% 74.81%
=======================================
Files 115 115
Lines 6885 6885
=======================================
Hits 5151 5151
Misses 1478 1478
Partials 256 256
Continue to review full report at Codecov.
|
…, simplify some tests
@tigrannajaryan Thank you for a review. I've fixed all mistakes. Could you please check it one more time? |
timeout time.Duration | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
tests := []struct { |
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.
This one also is a single test case, let's remove the tests
. It can be added later if there are multiple similar tests.
if got == nil { | ||
return | ||
} | ||
assert.NoError(t, err, "nil config") |
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.
The third parameter of NoError looks incorrect to me. If I remember correctly it is the message to print when assertion fails. This assertion is not about config, why does it print "nil config"?
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.
Indeed, it is a wrong usage. Fixed
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.
LGTM
Thank you @agneum
… OpenTelemetry, use submodules for CMake deps (open-telemetry#377)
Some improvements of tests for an issue #266