Skip to content

Commit 0782bcf

Browse files
committed
add NBVAL_TEST_NAME, offset cell indices by 1
- fixes #111
1 parent f2c141d commit 0782bcf

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

nbval/plugin.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def pytest_addoption(parser):
105105
help='(deprecated) Alias of --nbval-sanitize-with')
106106

107107
group.addoption('--current-env', action='store_true',
108-
help='(deprecated) Alias of --nbval-current-env')
108+
help='(deprecated) Alias of --nbval-current-env')
109109

110110
term_group = parser.getgroup("terminal reporting")
111111
term_group._addoption(
@@ -147,6 +147,7 @@ def pytest_collect_file(file_path, parent):
147147
comment_markers = {
148148
'PYTEST_VALIDATE_IGNORE_OUTPUT': ('check', False), # For backwards compatibility
149149
'NBVAL_IGNORE_OUTPUT': ('check', False),
150+
'NBVAL_TEST_NAME': ('name', str),
150151
'NBVAL_CHECK_OUTPUT': 'check',
151152
'NBVAL_RAISES_EXCEPTION': 'check_exception',
152153
'NBVAL_SKIP': 'skip',
@@ -170,14 +171,22 @@ def find_comment_markers(cellsource):
170171
line = line.strip()
171172
if line.startswith('#'):
172173
# print("Found comment in '{}'".format(line))
173-
comment = line.lstrip('#').strip()
174+
comment_val = line.lstrip('#').strip()
175+
if ':' in comment_val:
176+
comment, val = comment_val.split(':', 1)
177+
comment = comment.rstrip()
178+
val = val.lstrip()
179+
else:
180+
comment = comment_val
174181
if comment in comment_markers:
175182
# print("Found marker {}".format(comment))
176183
marker = comment_markers[comment]
177184
if not isinstance(marker, tuple):
178185
# If not an explicit tuple ('option', True/False),
179186
# imply ('option', True)
180187
marker = (marker, True)
188+
elif marker[1] is str:
189+
marker = (marker[0], val)
181190
marker_type = marker[0]
182191
if marker_type in found:
183192
warnings.warn(
@@ -264,7 +273,7 @@ def setup(self):
264273
self.kernel = RunningKernel(
265274
kernel_name,
266275
cwd=str(self.fspath.dirname),
267-
startup_timeout=self.config.option.nbval_kernel_startup_timeout,
276+
startup_timeout=self.config.option.nbval_kernel_startup_timeout,
268277
)
269278
self.setup_sanitize_files()
270279
if getattr(self.parent.config.option, 'cov_source', None):
@@ -324,6 +333,8 @@ def collect(self):
324333
with warnings.catch_warnings(record=True) as ws:
325334
options = defaultdict(bool, find_metadata_tags(cell.metadata))
326335
comment_opts = dict(find_comment_markers(cell.source))
336+
# Update 'code' cell count
337+
cell_num += 1
327338
loc = '%s:Cell %d' % (getattr(self, "fspath", None), cell_num)
328339
if set(comment_opts.keys()) & set(options.keys()):
329340
warnings.warn_explicit(
@@ -342,15 +353,13 @@ def collect(self):
342353
lineno=0
343354
)
344355
options.update(comment_opts)
356+
options.setdefault('name', f'Cell {cell_num:d}')
345357
options.setdefault('check', self.compare_outputs)
346-
name = 'Cell ' + str(cell_num)
358+
name = options['name']
347359
yield IPyNbCell.from_parent(
348360
self, name=name, cell_num=cell_num, cell=cell, options=options
349361
)
350362

351-
# Update 'code' cell count
352-
cell_num += 1
353-
354363
def teardown(self):
355364
if self.kernel is not None and self.kernel.is_alive():
356365
if getattr(self.parent.config.option, 'cov_source', None):

0 commit comments

Comments
 (0)