So this question is nearly a decade old, and still applicable! However, none of the other answers addressed the problem I was having today - decorators on the tests coming from the missing library. In this case, it was hypothesis
and its decorators like @given
... So I was forced to whip up this less-than-friendly alternative:
try:
from hypothesis import assume, example, given, strategies as st
hypothesis_missing = False
except ImportError:
hypothesis_missing = True
def whatever(*args, **kwargs):
if args or kwargs:
return whatever
raise ImportError("Requires hypothesis library")
example = given = whatever
# Then this part is ickier to be py2.7 compatible
from six import add_move, MovedModule
add_move(MoveModule('mock', 'mock', 'unittest.mock'))
from six.moves import mock
st = mock.Mock() # Without this, things like st.integers() fail
A Python3-native solution would just tighten it up a little:
import unittest
st = unittest.mock.Mock()
Then on each testing class (ugh) I needed:
@unittest.skipIf(hypothesis_missing, "Requires hypothesis library")
IMPORTANT NOTE: Without the skipIf
, the tests will sometimes silently pass - the raise ImportError
in whatever()
only caught about half of them in my very limited testing.