11import ast
22import copy
3+ import enum
34import inspect
5+ import signal
46import sys
57import time
68from pathlib import Path
79from types import ModuleType
810from typing import Any , Dict , Iterable , List , Optional , Tuple , Union
9- import enum
11+
1012import pytest
13+ from _pytest .main import Session
1114from _pytest .pathlib import fnmatch_ex , import_path
15+ from _pytest .python import Package
1216from pytest import Collector , Config , FixtureRequest , Parser
13- import signal
14- from _pytest .main import Session
1517
1618if sys .version_info >= (3 , 9 , 0 ):
1719 from ast import unparse as ast_unparse
@@ -99,12 +101,12 @@ def pytest_configure(config):
99101
100102@pytest .hookimpl ()
101103def pytest_collectstart (collector ):
102- if not isinstance (collector , Session ):
104+ if not isinstance (collector , ( Session , Package ) ):
103105 if collector .config .getoption ("inlinetest_only" ) and (
104106 not isinstance (collector , InlinetestModule )
105107 ):
106108 collector .collect = lambda : [] # type: ignore[assignment]
107- elif collector .config .getoption ("inlinetest_disable" ) and isinstance (
109+ if collector .config .getoption ("inlinetest_disable" ) and isinstance (
108110 collector , InlinetestModule
109111 ):
110112 collector .collect = lambda : [] # type: ignore[assignment]
@@ -122,8 +124,6 @@ def pytest_collect_file(
122124
123125
124126def _is_inlinetest (config : Config , file_path : Path ) -> bool :
125- if config .getoption ("inlinetest_disable" ):
126- return False
127127 globs = config .getoption ("inlinetest_glob" ) or ["*.py" ]
128128 return any (fnmatch_ex (glob , file_path ) for glob in globs )
129129
@@ -1391,11 +1391,12 @@ def collect(self) -> Iterable[InlinetestItem]:
13911391 try :
13921392 # TODO: still need to find the right way to import without errors. mode=ImportMode.importlib did not work
13931393 module = import_path (self .path , root = self .config .rootpath )
1394- except ImportError :
1394+ except ( ImportError or ModuleNotFoundError ) as e :
13951395 if self .config .getvalue ("inlinetest_ignore_import_errors" ):
13961396 pytest .skip ("unable to import module %r" % self .path )
13971397 else :
13981398 raise ImportError ("unable to import module %r" % self .path )
1399+
13991400 finder = InlineTestFinder ()
14001401 runner = InlineTestRunner ()
14011402
0 commit comments