import unittest


def require_modules(*required_modules):
    """Require that the given list of modules can be imported."""
    def test_requirement(obj):
        """Test the requirement and return an optionally decorated object."""
        missing = []
        for required_module in required_modules:
            try:
                __import__(required_module, globals(), locals(), [], 0)
            except ImportError:
                missing += [required_module]
        if not missing:
            return obj
        skip_decorator = unittest.skip('{} not installed'.format(
            ', '.join(missing)))
        return skip_decorator(obj)

    return test_requirement