From 82700a9dcdb2ff7af25fc0693c9ed4d6442dd198 Mon Sep 17 00:00:00 2001 From: Chirag Gupta <103719146+chiruu12@users.noreply.github.com> Date: Wed, 26 Aug 2026 03:12:09 +0530 Subject: [PATCH] skip tests whose optional extra is not installed --- tests/test_emailobject.py | 11 ++++++++++- tests/test_fileobject.py | 2 ++ tests/test_openioc.py | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/test_emailobject.py b/tests/test_emailobject.py index f46bdadb9..1475d9989 100644 --- a/tests/test_emailobject.py +++ b/tests/test_emailobject.py @@ -10,12 +10,21 @@ from typing import TypeVar from zipfile import ZipFile -from pymisp.tools import EMailObject from pymisp.exceptions import InvalidMISPObject +# EMailObject is only exported when the email extra is installed. Importing it +# unguarded makes this module a collection error rather than a skip, which +# fails the whole run for anyone who installed pymisp without extras. +try: + from pymisp.tools import EMailObject + HAS_EMAIL_EXTRA = True +except ImportError: + HAS_EMAIL_EXTRA = False + T = TypeVar('T', bound='TestEmailObject') +@unittest.skipUnless(HAS_EMAIL_EXTRA, "requires the email extra") class TestEmailObject(unittest.TestCase): eml_1: BytesIO diff --git a/tests/test_fileobject.py b/tests/test_fileobject.py index 1299b3a0d..a70dc995e 100644 --- a/tests/test_fileobject.py +++ b/tests/test_fileobject.py @@ -5,10 +5,12 @@ import unittest import json from pymisp.tools import FileObject +from pymisp.tools.fileobject import HAS_MAGIC import pathlib class TestFileObject(unittest.TestCase): + @unittest.skipUnless(HAS_MAGIC, "requires the fileobjects extra (libmagic)") def test_mimeType(self) -> None: file_object = FileObject(filepath=pathlib.Path(__file__)) attributes = json.loads(file_object.to_json())['Attribute'] diff --git a/tests/test_openioc.py b/tests/test_openioc.py index f75f3994c..5bd6606c8 100644 --- a/tests/test_openioc.py +++ b/tests/test_openioc.py @@ -14,6 +14,7 @@ pass + # Two children of an AND-indicator whose combined search-context pair is NOT a # defined composite (RegistryItem/Value + FileItem/Md5sum). These must come back # as their two individual attributes, not a merged composite. @@ -54,6 +55,10 @@ """ +# Every test here calls load_openioc, which raises when bs4 is absent. The +# import guard above already anticipates bs4 being missing; without this the +# tests fail instead of skipping. +@unittest.skipUnless(openioc.has_bs4, "requires the openioc extra (bs4)") class TestOpenIOC(unittest.TestCase): def test_non_composite_and_indicator_not_merged(self) -> None: