mirror of https://github.com/Qiskit/qiskit.git
Make tests with temp files run on CI (#2514)
Fixes #2335 Create/destroy the temp files manually so that they can be used with AppVeyor. This is the method used by the pulse visualization testing. * Updated the testing to create and delete the files manually, rather than using temp files * Fixed a mistake with the filenaming * Commit to kick off the testing * Updated reference image * Fixed lint and added image * Changed from using tearDown() to addCleanup() * Moved the clean up calls to be in the correct place
This commit is contained in:
parent
078f75c71f
commit
3bf6a0e5ac
|
@ -15,8 +15,6 @@
|
|||
# pylint: disable=missing-docstring
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from qiskit import exceptions
|
||||
from qiskit.test import QiskitTestCase
|
||||
|
@ -25,74 +23,71 @@ from qiskit import user_config
|
|||
|
||||
class TestUserConfig(QiskitTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.file_path = 'temp.txt'
|
||||
|
||||
def test_empty_file_read(self):
|
||||
file_path = tempfile.NamedTemporaryFile()
|
||||
self.addCleanup(file_path.close)
|
||||
config = user_config.UserConfig(file_path.name)
|
||||
config = user_config.UserConfig(self.file_path)
|
||||
config.read_config_file()
|
||||
self.assertEqual({}, config.settings)
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_invalid_optimization_level(self):
|
||||
test_config = """
|
||||
[default]
|
||||
transpile_optimization_level = 76
|
||||
"""
|
||||
file_path = tempfile.NamedTemporaryFile(mode='w')
|
||||
self.addCleanup(file_path.close)
|
||||
file_path.write(test_config)
|
||||
file_path.flush()
|
||||
config = user_config.UserConfig(file_path.name)
|
||||
self.assertRaises(exceptions.QiskitUserConfigError,
|
||||
config.read_config_file)
|
||||
self.addCleanup(os.remove, self.file_path)
|
||||
with open(self.file_path, 'w') as file:
|
||||
file.write(test_config)
|
||||
file.flush()
|
||||
config = user_config.UserConfig(self.file_path)
|
||||
self.assertRaises(exceptions.QiskitUserConfigError,
|
||||
config.read_config_file)
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_invalid_circuit_drawer(self):
|
||||
test_config = """
|
||||
[default]
|
||||
circuit_drawer = MSPaint
|
||||
circuit_mpl_style = default
|
||||
"""
|
||||
file_path = tempfile.NamedTemporaryFile(mode='w')
|
||||
self.addCleanup(file_path.close)
|
||||
file_path.write(test_config)
|
||||
file_path.flush()
|
||||
config = user_config.UserConfig(file_path.name)
|
||||
self.assertRaises(exceptions.QiskitUserConfigError,
|
||||
config.read_config_file)
|
||||
self.addCleanup(os.remove, self.file_path)
|
||||
with open(self.file_path, 'w') as file:
|
||||
file.write(test_config)
|
||||
file.flush()
|
||||
config = user_config.UserConfig(self.file_path)
|
||||
self.assertRaises(exceptions.QiskitUserConfigError,
|
||||
config.read_config_file)
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_circuit_drawer_valid(self):
|
||||
test_config = """
|
||||
[default]
|
||||
circuit_drawer = latex
|
||||
circuit_mpl_style = default
|
||||
"""
|
||||
file_path = tempfile.NamedTemporaryFile(mode='w')
|
||||
self.addCleanup(file_path.close)
|
||||
file_path.write(test_config)
|
||||
file_path.flush()
|
||||
config = user_config.UserConfig(file_path.name)
|
||||
config.read_config_file()
|
||||
self.assertEqual({'circuit_drawer': 'latex',
|
||||
'circuit_mpl_style': 'default'}, config.settings)
|
||||
self.addCleanup(os.remove, self.file_path)
|
||||
with open(self.file_path, 'w') as file:
|
||||
file.write(test_config)
|
||||
file.flush()
|
||||
config = user_config.UserConfig(self.file_path)
|
||||
config.read_config_file()
|
||||
self.assertEqual({'circuit_drawer': 'latex',
|
||||
'circuit_mpl_style': 'default'}, config.settings)
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_optimization_level_valid(self):
|
||||
test_config = """
|
||||
[default]
|
||||
transpile_optimization_level = 1
|
||||
"""
|
||||
file_path = tempfile.NamedTemporaryFile(mode='w')
|
||||
self.addCleanup(file_path.close)
|
||||
file_path.write(test_config)
|
||||
file_path.flush()
|
||||
config = user_config.UserConfig(file_path.name)
|
||||
config.read_config_file()
|
||||
self.assertEqual({'transpile_optimization_level': 1},
|
||||
config.settings)
|
||||
self.addCleanup(os.remove, self.file_path)
|
||||
with open(self.file_path, 'w') as file:
|
||||
file.write(test_config)
|
||||
file.flush()
|
||||
config = user_config.UserConfig(self.file_path)
|
||||
config.read_config_file()
|
||||
self.assertEqual({'transpile_optimization_level': 1},
|
||||
config.settings)
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_all_options_valid(self):
|
||||
test_config = """
|
||||
[default]
|
||||
|
@ -100,12 +95,12 @@ class TestUserConfig(QiskitTestCase):
|
|||
circuit_mpl_style = default
|
||||
transpile_optimization_level = 3
|
||||
"""
|
||||
file_path = tempfile.NamedTemporaryFile(mode='w')
|
||||
self.addCleanup(file_path.close)
|
||||
file_path.write(test_config)
|
||||
file_path.flush()
|
||||
config = user_config.UserConfig(file_path.name)
|
||||
config.read_config_file()
|
||||
self.assertEqual({'circuit_drawer': 'latex',
|
||||
'circuit_mpl_style': 'default',
|
||||
'transpile_optimization_level': 3}, config.settings)
|
||||
self.addCleanup(os.remove, self.file_path)
|
||||
with open(self.file_path, 'w') as file:
|
||||
file.write(test_config)
|
||||
file.flush()
|
||||
config = user_config.UserConfig(self.file_path)
|
||||
config.read_config_file()
|
||||
self.assertEqual({'circuit_drawer': 'latex',
|
||||
'circuit_mpl_style': 'default',
|
||||
'transpile_optimization_level': 3}, config.settings)
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 8.8 KiB After Width: | Height: | Size: 12 KiB |
|
@ -14,20 +14,16 @@
|
|||
|
||||
# pylint: disable=invalid-name,missing-docstring
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
import os
|
||||
|
||||
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
|
||||
from qiskit.test import QiskitTestCase
|
||||
from qiskit import visualization
|
||||
|
||||
from .visualization import QiskitVisualizationTestCase
|
||||
|
||||
if visualization.HAS_MATPLOTLIB:
|
||||
from matplotlib import pyplot as plt
|
||||
from matplotlib.testing import compare
|
||||
import matplotlib
|
||||
import sys
|
||||
|
||||
|
||||
def _path_to_reference(filename):
|
||||
|
@ -38,7 +34,7 @@ def _this_directory():
|
|||
return os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
class TestMatplotlibDrawer(QiskitTestCase):
|
||||
class TestMatplotlibDrawer(QiskitVisualizationTestCase):
|
||||
|
||||
def _expected_empty(self):
|
||||
# Generate blank
|
||||
|
@ -52,37 +48,27 @@ class TestMatplotlibDrawer(QiskitTestCase):
|
|||
expected.set_size_inches(2.508333333333333, 0.2508333333333333)
|
||||
return expected
|
||||
|
||||
def _make_temp_file(self, plot):
|
||||
tmp = tempfile.NamedTemporaryFile(suffix='.png')
|
||||
self.addCleanup(tmp.close)
|
||||
plot.savefig(tmp.name)
|
||||
return tmp
|
||||
|
||||
@unittest.skipIf(not visualization.HAS_MATPLOTLIB,
|
||||
'matplotlib not available.')
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_empty_circuit(self):
|
||||
qc = QuantumCircuit()
|
||||
res = visualization.circuit_drawer(qc, output='mpl')
|
||||
res_out_file = self._make_temp_file(res)
|
||||
filename = self._get_resource_path('current_pulse_matplotlib_ref.png')
|
||||
visualization.circuit_drawer(qc, output='mpl', filename=filename)
|
||||
self.addCleanup(os.remove, filename)
|
||||
|
||||
expected_filename = self._get_resource_path('expected_current_pulse_matplotlib_ref.png')
|
||||
expected = self._expected_empty()
|
||||
expected_image_file = self._make_temp_file(expected)
|
||||
self.assertIsNone(compare.compare_images(expected_image_file.name,
|
||||
res_out_file.name, 0.0001))
|
||||
expected.savefig(expected_filename)
|
||||
self.addCleanup(os.remove, expected_filename)
|
||||
|
||||
self.assertImagesAreEqual(filename, expected_filename)
|
||||
|
||||
@unittest.skipIf(not visualization.HAS_MATPLOTLIB,
|
||||
'matplotlib not available.')
|
||||
@unittest.skipIf(os.name == 'nt', 'tempfile fails on appveyor')
|
||||
def test_plot_barriers(self):
|
||||
"""Test to see that plotting barriers works - if it is set to False, no
|
||||
blank columns are introduced"""
|
||||
|
||||
# Use a different backend as the default backend causes the test to fail.
|
||||
# This is because it adds less whitespace around the image than is present
|
||||
# in the reference image, but only on MacOS
|
||||
if sys.platform == 'darwin':
|
||||
matplotlib.use('agg')
|
||||
|
||||
# generate a circuit with barriers and other barrier like instructions in
|
||||
q = QuantumRegister(2, 'q')
|
||||
c = ClassicalRegister(2, 'c')
|
||||
|
@ -100,15 +86,18 @@ class TestMatplotlibDrawer(QiskitTestCase):
|
|||
qc.snapshot('1')
|
||||
|
||||
# check the barriers plot properly when plot_barriers= True
|
||||
barriers_plot = visualization.circuit_drawer(qc, output='mpl', plot_barriers=True)
|
||||
barriers_plot_file = self._make_temp_file(barriers_plot)
|
||||
self.assertIsNone(compare.compare_images(barriers_plot_file.name,
|
||||
_path_to_reference('matplotlib_barriers_ref.png'),
|
||||
0.0001))
|
||||
filename = self._get_resource_path('visualization/references/current_matplotlib_ref.png')
|
||||
visualization.circuit_drawer(qc, output='mpl', plot_barriers=True, filename=filename)
|
||||
self.addCleanup(os.remove, filename)
|
||||
|
||||
ref_filename = self._get_resource_path(
|
||||
'visualization/references/matplotlib_barriers_ref.png')
|
||||
self.assertImagesAreEqual(filename, ref_filename)
|
||||
|
||||
# check that the barrier aren't plotted when plot_barriers = False
|
||||
barriers_no_plot = visualization.circuit_drawer(qc, output='mpl', plot_barriers=False)
|
||||
barriers_no_plot_file = self._make_temp_file(barriers_no_plot)
|
||||
filename = self._get_resource_path('current_matplotlib_ref.png')
|
||||
visualization.circuit_drawer(qc, output='mpl', plot_barriers=False, filename=filename)
|
||||
self.addCleanup(os.remove, filename)
|
||||
|
||||
# generate the same circuit but without the barrier commands as this is what the
|
||||
# circuit should look like when displayed with plot barriers false
|
||||
|
@ -118,8 +107,9 @@ class TestMatplotlibDrawer(QiskitTestCase):
|
|||
qc1.h(q1[0])
|
||||
qc1.h(q1[1])
|
||||
|
||||
no_barriers = visualization.circuit_drawer(qc1, output='mpl', justify='None',)
|
||||
no_barriers_file = self._make_temp_file(no_barriers)
|
||||
no_barriers_filename = self._get_resource_path('current_no_barriers_matplotlib_ref.png')
|
||||
visualization.circuit_drawer(qc1, output='mpl', justify='None',
|
||||
filename=no_barriers_filename)
|
||||
self.addCleanup(os.remove, no_barriers_filename)
|
||||
|
||||
self.assertIsNone(compare.compare_images(barriers_no_plot_file.name,
|
||||
no_barriers_file.name, 0.0001))
|
||||
self.assertImagesAreEqual(filename, no_barriers_filename)
|
||||
|
|
Loading…
Reference in New Issue