@@ -20,6 +20,7 @@ class CodeBlock:
2020 expect_exception : bool = False
2121 skip : bool = False
2222 skipif : str | None = None
23+ importorskip : str | None = None
2324
2425
2526def extract_from_file (
@@ -99,6 +100,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
99100 + "but block already has expected_output."
100101 )
101102 out [- 1 ].expected_output = "" .join (code_block )
103+
102104 elif keyword == "cont" :
103105 if len (out ) == 0 :
104106 raise RuntimeError (
@@ -111,8 +113,10 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
111113 out [- 1 ].expected_output ,
112114 out [- 1 ].expect_exception ,
113115 )
116+
114117 elif keyword == "skip" :
115118 out .append (CodeBlock ("" .join (code_block ), lineno , syntax , skip = True ))
119+
116120 elif keyword .startswith ("skipif" ):
117121 m = re .match (r"skipif\((.*)\)" , keyword )
118122 if m is None :
@@ -122,12 +126,26 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
122126 out .append (
123127 CodeBlock ("" .join (code_block ), lineno , syntax , skipif = m .group (1 ))
124128 )
129+
130+ elif keyword .startswith ("importorskip" ):
131+ m = re .match (r"importorskip\((.*)\)" , keyword )
132+ if m is None :
133+ raise RuntimeError (
134+ "pytest-codeblocks: Expected importorskip(some-module)"
135+ )
136+ out .append (
137+ CodeBlock (
138+ "" .join (code_block ), lineno , syntax , importorskip = m .group (1 )
139+ )
140+ )
141+
125142 elif keyword in ["expect-exception" , "expect-error" ]:
126143 out .append (
127144 CodeBlock (
128145 "" .join (code_block ), lineno , syntax , expect_exception = True
129146 )
130147 )
148+
131149 else :
132150 raise RuntimeError (f'Unknown pytest-codeblocks keyword "{ keyword } ."' )
133151
0 commit comments