Skip to content

Commit 94c1748

Browse files
committed
add NBVAL_TEST_NAME, offset cell indices by 1
- fixes #111
1 parent 6dd2eaa commit 94c1748

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

nbval/plugin.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def pytest_addoption(parser):
104104
help='(deprecated) Alias of --nbval-sanitize-with')
105105

106106
group.addoption('--current-env', action='store_true',
107-
help='(deprecated) Alias of --nbval-current-env')
107+
help='(deprecated) Alias of --nbval-current-env')
108108

109109
term_group = parser.getgroup("terminal reporting")
110110
term_group._addoption(
@@ -150,6 +150,7 @@ def pytest_collect_file(path, parent):
150150
comment_markers = {
151151
'PYTEST_VALIDATE_IGNORE_OUTPUT': ('check', False), # For backwards compatibility
152152
'NBVAL_IGNORE_OUTPUT': ('check', False),
153+
'NBVAL_TEST_NAME': ('name', str),
153154
'NBVAL_CHECK_OUTPUT': 'check',
154155
'NBVAL_RAISES_EXCEPTION': 'check_exception',
155156
'NBVAL_SKIP': 'skip',
@@ -173,14 +174,22 @@ def find_comment_markers(cellsource):
173174
line = line.strip()
174175
if line.startswith('#'):
175176
# print("Found comment in '{}'".format(line))
176-
comment = line.lstrip('#').strip()
177+
comment_val = line.lstrip('#').strip()
178+
if ':' in comment_val:
179+
comment, val = comment_val.split(':', 1)
180+
comment = comment.rstrip()
181+
val = val.lstrip()
182+
else:
183+
comment = comment_val
177184
if comment in comment_markers:
178185
# print("Found marker {}".format(comment))
179186
marker = comment_markers[comment]
180187
if not isinstance(marker, tuple):
181188
# If not an explicit tuple ('option', True/False),
182189
# imply ('option', True)
183190
marker = (marker, True)
191+
elif marker[1] is str:
192+
marker = (marker[0], val)
184193
marker_type = marker[0]
185194
if marker_type in found:
186195
warnings.warn(
@@ -267,7 +276,7 @@ def setup(self):
267276
self.kernel = RunningKernel(
268277
kernel_name,
269278
cwd=str(self.fspath.dirname),
270-
startup_timeout=self.config.option.nbval_kernel_startup_timeout,
279+
startup_timeout=self.config.option.nbval_kernel_startup_timeout,
271280
)
272281
self.setup_sanitize_files()
273282
if getattr(self.parent.config.option, 'cov_source', None):
@@ -327,6 +336,8 @@ def collect(self):
327336
with warnings.catch_warnings(record=True) as ws:
328337
options = defaultdict(bool, find_metadata_tags(cell.metadata))
329338
comment_opts = dict(find_comment_markers(cell.source))
339+
# Update 'code' cell count
340+
cell_num += 1
330341
loc = '%s:Cell %d' % (getattr(self, "fspath", None), cell_num)
331342
if set(comment_opts.keys()) & set(options.keys()):
332343
warnings.warn_explicit(
@@ -345,8 +356,9 @@ def collect(self):
345356
lineno=0
346357
)
347358
options.update(comment_opts)
359+
options.setdefault('name', 'Cell %d' % cell_num)
348360
options.setdefault('check', self.compare_outputs)
349-
name = 'Cell ' + str(cell_num)
361+
name = options['name']
350362
# https://docs.pytest.org/en/stable/deprecations.html#node-construction-changed-to-node-from-parent
351363
if hasattr(IPyNbCell, "from_parent"):
352364
yield IPyNbCell.from_parent(
@@ -355,9 +367,6 @@ def collect(self):
355367
else:
356368
yield IPyNbCell(name, self, cell_num, cell, options)
357369

358-
# Update 'code' cell count
359-
cell_num += 1
360-
361370
def teardown(self):
362371
if self.kernel is not None and self.kernel.is_alive():
363372
if getattr(self.parent.config.option, 'cov_source', None):

0 commit comments

Comments
 (0)