16
16
import os
17
17
import json
18
18
import pickle
19
+ import sys
20
+ import unittest
19
21
import warnings
20
22
from typing import Any , Callable , Optional
21
23
22
24
import fixtures
23
25
import uncertainties
24
- from qiskit .test import QiskitTestCase
25
26
from qiskit .utils .deprecation import deprecate_func
26
27
28
+ import qiskit_experiments .warnings
27
29
from qiskit_experiments .framework import (
28
30
ExperimentDecoder ,
29
31
ExperimentEncoder ,
36
38
TEST_TIMEOUT = os .environ .get ("TEST_TIMEOUT" , 60 )
37
39
38
40
39
- class QiskitExperimentsTestCase (QiskitTestCase ):
41
+ # If testtools is installed use that as a (mostly) drop in replacement for
42
+ # unittest's TestCase. This will enable the fixtures used for capturing stdout
43
+ # stderr, and pylogging to attach the output to stestr's result stream.
44
+ if qiskit_experiments .warnings .HAS_TESTTOOLS :
45
+ import testtools
46
+
47
+ class BaseTestCase (testtools .TestCase ):
48
+ """Base test class."""
49
+
50
+ # testtools maintains their own version of assert functions which mostly
51
+ # behave as value adds to the std unittest assertion methods. However,
52
+ # for assertEquals and assertRaises modern unittest has diverged from
53
+ # the forks in testtools and offer more (or different) options that are
54
+ # incompatible testtools versions. Just use the stdlib versions so that
55
+ # our tests work as expected.
56
+ assertRaises = unittest .TestCase .assertRaises
57
+ assertEqual = unittest .TestCase .assertEqual
58
+
59
+ else :
60
+
61
+ class BaseTestCase (unittest .TestCase ):
62
+ """Base test class."""
63
+
64
+ pass
65
+
66
+
67
+
68
+
69
+ # TODO: copy enforce_subclasses_call decorator?
70
+ class QiskitExperimentsTestCase (BaseTestCase ):
40
71
"""Qiskit Experiments specific extra functionality for test cases."""
41
72
73
+ def __init__ (self , * args , ** kwargs ):
74
+ super ().__init__ (* args , ** kwargs )
75
+ self .__setup_called = False
76
+ self .__teardown_called = False
77
+
42
78
def setUp (self ):
43
79
super ().setUp ()
44
80
self .useFixture (fixtures .Timeout (TEST_TIMEOUT , gentle = True ))
81
+ if self .__setup_called :
82
+ raise ValueError (
83
+ "In File: %s\n "
84
+ "TestCase.setUp was already called. Do not explicitly call "
85
+ "setUp from your tests. In your own setUp, use super to call "
86
+ "the base setUp." % (sys .modules [self .__class__ .__module__ ].__file__ ,)
87
+ )
88
+ self .__setup_called = True
45
89
46
90
@classmethod
47
91
def setUpClass (cls ):
48
92
"""Set-up test class."""
49
93
super ().setUpClass ()
50
94
95
+ warnings .filterwarnings ("error" , category = DeprecationWarning )
96
+
51
97
# Some functionality may be deprecated in Qiskit Experiments. If the deprecation warnings aren't
52
98
# filtered, the tests will fail as ``QiskitTestCase`` sets all warnings to be treated as an error
53
99
# by default.
@@ -60,6 +106,17 @@ def setUpClass(cls):
60
106
for msg in allow_deprecationwarning_message :
61
107
warnings .filterwarnings ("default" , category = DeprecationWarning , message = msg )
62
108
109
+ def tearDown (self ):
110
+ super ().tearDown ()
111
+ if self .__teardown_called :
112
+ raise ValueError (
113
+ "In File: %s\n "
114
+ "TestCase.tearDown was already called. Do not explicitly call "
115
+ "tearDown from your tests. In your own tearDown, use super to "
116
+ "call the base tearDown." % (sys .modules [self .__class__ .__module__ ].__file__ ,)
117
+ )
118
+ self .__teardown_called = True
119
+
63
120
def assertExperimentDone (
64
121
self ,
65
122
experiment_data : ExperimentData ,
0 commit comments