Skip to content

Commit

Permalink
Fail unit tests on deprecation warning (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt474 authored Oct 31, 2023
1 parent ccd41a5 commit 33981d4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
3 changes: 3 additions & 0 deletions test/ibm_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging
import inspect
import unittest
import warnings
from contextlib import suppress
from collections import defaultdict
from typing import DefaultDict, Dict
Expand Down Expand Up @@ -45,6 +46,8 @@ def setUpClass(cls):
filename = "%s.log" % os.path.splitext(inspect.getfile(cls))[0]
setup_test_logging(cls.log, filename)
cls._set_logging_level(logging.getLogger(QISKIT_IBM_RUNTIME_LOGGER_NAME))
# fail test on deprecation warnings from qiskit
warnings.filterwarnings("error", category=DeprecationWarning, module="qiskit")

@classmethod
def _set_logging_level(cls, logger: logging.Logger) -> None:
Expand Down
23 changes: 8 additions & 15 deletions test/unit/test_data_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,10 @@ def test_coder_operators(self):

# filter warnings triggered by opflow imports
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", category=DeprecationWarning, module=r"qiskit\.opflow\."
)
warnings.filterwarnings("ignore", category=DeprecationWarning)
from qiskit.opflow import PauliSumOp # pylint: disable=import-outside-toplevel

# catch warnings triggered by opflow use
with warnings.catch_warnings(record=True) as w_log:
deprecated_op = PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[2]))
self.assertTrue(len(w_log) > 0)

coeff_x = Parameter("x")
coeff_y = coeff_x + 1
Expand All @@ -124,14 +119,7 @@ def test_coder_operators(self):
with warnings.catch_warnings():
# filter warnings triggered by opflow imports
# in L146 of utils/json.py
warnings.filterwarnings(
"ignore", category=DeprecationWarning, module=r"qiskit\.opflow\."
)
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
module=r"qiskit_ibm_runtime\.utils\.json",
)
warnings.filterwarnings("ignore", category=DeprecationWarning)
decoded = json.loads(encoded, cls=RuntimeDecoder)
self.assertEqual(operator, decoded)

Expand All @@ -141,7 +129,12 @@ def test_coder_noise_model(self):
self.assertIsInstance(noise_model, NoiseModel)
encoded = json.dumps(noise_model, cls=RuntimeEncoder)
self.assertIsInstance(encoded, str)
decoded = json.loads(encoded, cls=RuntimeDecoder)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
)
decoded = json.loads(encoded, cls=RuntimeDecoder)
self.assertIsInstance(decoded, NoiseModel)
self.assertEqual(noise_model.noise_qubits, decoded.noise_qubits)
self.assertEqual(noise_model.noise_instructions, decoded.noise_instructions)
Expand Down

0 comments on commit 33981d4

Please sign in to comment.