Skip to content

Commit

Permalink
Fix potential import error from test runner
Browse files Browse the repository at this point in the history
Since Qiskit#3982 merged we've been using the python libraries fixtures and
testtools to construct a custom test results handler that can handle
attachments for parallel test execution. While this is only used by the
test runner the imports could cause an issue for isntalled copies of
terra trying to use the fake backends. Since the requirements-dev.txt
isn't installed for regular installs if a user were to import the
fake_backends with a plain install this would result in an ImportError
because fixtures and/or testtools wasn't installed. Since these aren't
really requirements for terra, and are only required to run tests this
commit wraps the imports in a try block so no error will be raised
unless it's actually a test run.

At the same time fixtures and testtools
are explicitly added to the requirements-dev.txt list. They are
implicitly installed because they're requirements of stestr. But having
them explicitly in the requirements-dev.txt list makes this dependency
for running tests explicit, especially in cases where a user isn't using
stestr as their test runner.
  • Loading branch information
mtreinish committed Sep 15, 2020
1 parent d76938b commit 8b8c420
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
15 changes: 11 additions & 4 deletions qiskit/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
import sys
import unittest
from unittest.util import safe_repr

import fixtures
from testtools.compat import advance_iterator
from testtools import content
try:
import fixtures
from testtools.compat import advance_iterator
from testtools import content
HAS_FIXTURES = True
except ImportError:
HAS_FIXTURES = False

from .runtest import RunTest, MultipleExceptions
from .utils import Path, _AssertNoLogsContext
Expand Down Expand Up @@ -86,6 +89,10 @@ class QiskitTestCase(unittest.TestCase):

def __init__(self, *args, **kwargs):
"""Construct a TestCase."""
if not HAS_FIXTURES:
raise ImportError(
"Test runner requirements are missing, install "
"requirements-dev.txt and run tests again.")
super(QiskitTestCase, self).__init__(*args, **kwargs)
self.__RunTest = self.run_tests_with
self._reset()
Expand Down
10 changes: 9 additions & 1 deletion qiskit/test/runtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@

import sys

from testtools.testresult import ExtendedToOriginalDecorator
try:
from testtools.testresult import ExtendedToOriginalDecorator
HAS_TESTTOOLS = True
except ImportError:
HAS_TESTTOOLS = False


class MultipleExceptions(Exception):
Expand Down Expand Up @@ -75,6 +79,10 @@ def __init__(self, case, handlers=None, last_resort=None):
raised - aborting the test run as this is inside the runner
machinery rather than the confined context of the test.
"""
if not HAS_TESTTOOLS:
raise ImportError(
'Test runner requirements are missing, install '
'requirements-dev.txt before running tests')
self.case = case
self.handlers = handlers or []
self.exception_caught = object()
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pycodestyle
pydot
astroid==2.3.3
pylint==2.4.4
fixtures>=3.0.0
testtools>=2.2.0
stestr>=2.0.0
PyGithub
wheel
Expand Down

0 comments on commit 8b8c420

Please sign in to comment.