Skip to content

Commit

Permalink
Add test context for no exception raised (#3630)
Browse files Browse the repository at this point in the history
* Add test context for no exception raised

Fixes #1891

* Add missing test class
  • Loading branch information
ocelotl authored Jan 25, 2024
1 parent 373ed51 commit 4bac02e
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 74 deletions.
10 changes: 3 additions & 7 deletions exporter/opentelemetry-exporter-otlp/tests/test_otlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
Expand All @@ -26,19 +25,16 @@
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter as HTTPSpanExporter,
)
from opentelemetry.test import TestCase


class TestOTLPExporters(unittest.TestCase):
class TestOTLPExporters(TestCase):
def test_constructors(self):
for exporter in [
OTLPSpanExporter,
HTTPSpanExporter,
OTLPLogExporter,
OTLPMetricExporter,
]:
try:
with self.assertNotRaises(Exception):
exporter()
except Exception: # pylint: disable=broad-except
self.fail(
f"Unexpected exception raised when instantiating {exporter.__name__}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from math import inf
from sys import float_info, version_info
from unittest import TestCase
from unittest.mock import patch

from pytest import mark
Expand All @@ -31,6 +30,7 @@
MIN_NORMAL_EXPONENT,
MIN_NORMAL_VALUE,
)
from opentelemetry.test import TestCase

if version_info >= (3, 9):
from math import nextafter
Expand Down Expand Up @@ -69,12 +69,9 @@ def test_init_called_once(self, mock_init):

def test_exponent_mapping_0(self):

try:
with self.assertNotRaises(Exception):
ExponentMapping(0)

except Exception as error:
self.fail(f"Unexpected exception raised: {error}")

def test_exponent_mapping_zero(self):

exponent_mapping = ExponentMapping(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from math import ldexp
from sys import float_info
from types import MethodType
from unittest import TestCase
from unittest.mock import Mock, patch

from opentelemetry.sdk.metrics._internal.aggregation import (
Expand All @@ -41,6 +40,7 @@
from opentelemetry.sdk.metrics.view import (
ExponentialBucketHistogramAggregation,
)
from opentelemetry.test import TestCase


def get_counts(buckets: Buckets) -> int:
Expand Down Expand Up @@ -793,11 +793,8 @@ def test_boundary_statistics(self):

index = mapping.map_to_index(value)

try:
with self.assertNotRaises(Exception):
boundary = mapping.get_lower_boundary(index + 1)
except Exception as error:
raise error
self.fail(f"Unexpected exception {error} raised")

if boundary < value:
above += 1
Expand Down Expand Up @@ -836,30 +833,6 @@ def test_aggregate_collect(self):
"""
Tests a repeated cycle of aggregation and collection.
"""
"""
try:
exponential_histogram_aggregation = (
_ExponentialBucketHistogramAggregation(
Mock(),
Mock(),
)
)
exponential_histogram_aggregation.aggregate(Measurement(2, Mock()))
exponential_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
exponential_histogram_aggregation.aggregate(Measurement(2, Mock()))
exponential_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
exponential_histogram_aggregation.aggregate(Measurement(2, Mock()))
exponential_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
except Exception as error:
self.fail(f"Unexpected exception raised: {error}")
"""
exponential_histogram_aggregation = (
_ExponentialBucketHistogramAggregation(
Mock(),
Expand Down
14 changes: 4 additions & 10 deletions opentelemetry-sdk/tests/metrics/test_backward_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"""

from typing import Iterable, Sequence
from unittest import TestCase

from opentelemetry.metrics import CallbackOptions, Observation
from opentelemetry.sdk.metrics import MeterProvider
Expand All @@ -38,6 +37,7 @@
MetricReader,
PeriodicExportingMetricReader,
)
from opentelemetry.test import TestCase


# Do not change these classes until after major version 1
Expand Down Expand Up @@ -82,30 +82,24 @@ def test_metric_exporter(self):
)
# produce some data
meter_provider.get_meter("foo").create_counter("mycounter").add(12)
try:
with self.assertNotRaises(Exception):
meter_provider.shutdown()
except Exception:
self.fail()

def test_metric_reader(self):
reader = OrigMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
# produce some data
meter_provider.get_meter("foo").create_counter("mycounter").add(12)
try:
with self.assertNotRaises(Exception):
meter_provider.shutdown()
except Exception:
self.fail()

def test_observable_callback(self):
reader = InMemoryMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
# produce some data
meter_provider.get_meter("foo").create_counter("mycounter").add(12)
try:
with self.assertNotRaises(Exception):
metrics_data = reader.get_metrics_data()
except Exception:
self.fail()

self.assertEqual(len(metrics_data.resource_metrics), 1)
self.assertEqual(
Expand Down
14 changes: 4 additions & 10 deletions opentelemetry-sdk/tests/metrics/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# pylint: disable=unused-import

from unittest import TestCase
from opentelemetry.test import TestCase


class TestImport(TestCase):
Expand All @@ -23,7 +23,7 @@ def test_import_init(self):
Test that the metrics root module has the right symbols
"""

try:
with self.assertNotRaises(Exception):
from opentelemetry.sdk.metrics import ( # noqa: F401
Counter,
Histogram,
Expand All @@ -34,15 +34,13 @@ def test_import_init(self):
ObservableUpDownCounter,
UpDownCounter,
)
except Exception as error:
self.fail(f"Unexpected error {error} was raised")

def test_import_export(self):
"""
Test that the metrics export module has the right symbols
"""

try:
with self.assertNotRaises(Exception):
from opentelemetry.sdk.metrics.export import ( # noqa: F401
AggregationTemporality,
ConsoleMetricExporter,
Expand All @@ -63,15 +61,13 @@ def test_import_export(self):
ScopeMetrics,
Sum,
)
except Exception as error:
self.fail(f"Unexpected error {error} was raised")

def test_import_view(self):
"""
Test that the metrics view module has the right symbols
"""

try:
with self.assertNotRaises(Exception):
from opentelemetry.sdk.metrics.view import ( # noqa: F401
Aggregation,
DefaultAggregation,
Expand All @@ -81,5 +77,3 @@ def test_import_view(self):
SumAggregation,
View,
)
except Exception as error:
self.fail(f"Unexpected error {error} was raised")
12 changes: 4 additions & 8 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from logging import WARNING
from time import sleep
from typing import Iterable, Sequence
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch

from opentelemetry.metrics import NoOpMeter
Expand All @@ -40,6 +39,7 @@
)
from opentelemetry.sdk.metrics.view import SumAggregation, View
from opentelemetry.sdk.resources import Resource
from opentelemetry.test import TestCase
from opentelemetry.test.concurrency_test import ConcurrencyTestBase, MockFunc


Expand All @@ -59,7 +59,7 @@ def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
return True


class TestMeterProvider(ConcurrencyTestBase):
class TestMeterProvider(ConcurrencyTestBase, TestCase):
def tearDown(self):

MeterProvider._all_metric_readers = set()
Expand All @@ -86,11 +86,9 @@ def test_register_metric_readers(self):
metric_reader_0 = PeriodicExportingMetricReader(mock_exporter)
metric_reader_1 = PeriodicExportingMetricReader(mock_exporter)

try:
with self.assertNotRaises(Exception):
MeterProvider(metric_readers=(metric_reader_0,))
MeterProvider(metric_readers=(metric_reader_1,))
except Exception as error:
self.fail(f"Unexpected exception {error} raised")

with self.assertRaises(Exception):
MeterProvider(metric_readers=(metric_reader_0,))
Expand Down Expand Up @@ -356,7 +354,7 @@ def setUp(self):
self.meter = Meter(Mock(), Mock())

def test_repeated_instrument_names(self):
try:
with self.assertNotRaises(Exception):
self.meter.create_counter("counter")
self.meter.create_up_down_counter("up_down_counter")
self.meter.create_observable_counter(
Expand All @@ -369,8 +367,6 @@ def test_repeated_instrument_names(self):
self.meter.create_observable_up_down_counter(
"observable_up_down_counter", callbacks=[Mock()]
)
except Exception as error:
self.fail(f"Unexpected exception raised {error}")

for instrument_name in [
"counter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from unittest.mock import Mock

import opentelemetry.trace as trace_api
Expand All @@ -24,6 +23,7 @@
)
from opentelemetry.sdk import trace
from opentelemetry.sdk.trace import id_generator
from opentelemetry.test import TestCase

FORMAT = jaeger.JaegerPropagator()

Expand Down Expand Up @@ -57,7 +57,7 @@ def get_context_new_carrier(old_carrier, carrier_baggage=None):
return ctx, new_carrier


class TestJaegerPropagator(unittest.TestCase):
class TestJaegerPropagator(TestCase):
@classmethod
def setUpClass(cls):
generator = id_generator.RandomIdGenerator()
Expand Down Expand Up @@ -236,7 +236,5 @@ def test_non_recording_span_does_not_crash(self):
mock_setter = Mock()
span = trace_api.NonRecordingSpan(trace_api.SpanContext(1, 1, True))
with trace_api.use_span(span, end_on_exit=True):
try:
with self.assertNotRaises(Exception):
FORMAT.inject({}, setter=mock_setter)
except Exception as exc: # pylint: disable=broad-except
self.fail(f"Injecting failed for NonRecordingSpan with {exc}")
55 changes: 55 additions & 0 deletions tests/opentelemetry-test-utils/src/opentelemetry/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# type: ignore

from traceback import format_tb
from unittest import TestCase


class _AssertNotRaisesMixin:
class _AssertNotRaises:
def __init__(self, test_case):
self._test_case = test_case

def __enter__(self):
return self

def __exit__(self, type_, value, tb): # pylint: disable=invalid-name
if value is not None and type_ in self._exception_types:

self._test_case.fail(
"Unexpected exception was raised:\n{}".format(
"\n".join(format_tb(tb))
)
)

return True

def __call__(self, exception, *exceptions):
# pylint: disable=attribute-defined-outside-init
self._exception_types = (exception, *exceptions)
return self

def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)
# pylint: disable=invalid-name
self.assertNotRaises = self._AssertNotRaises(self)


class TestCase(
_AssertNotRaisesMixin, TestCase
): # pylint: disable=function-redefined
pass
13 changes: 13 additions & 0 deletions tests/opentelemetry-test-utils/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading

0 comments on commit 4bac02e

Please sign in to comment.