Skip to content

Commit

Permalink
add NBVAL_TEST_NAME, offset cell indices by 1
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Aug 8, 2023
1 parent d7bc348 commit c732286
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions nbval/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def pytest_addoption(parser):
help='(deprecated) Alias of --nbval-sanitize-with')

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

term_group = parser.getgroup("terminal reporting")
term_group._addoption(
Expand Down Expand Up @@ -154,6 +154,7 @@ def pytest_collect_file(path, parent):
comment_markers = {
'PYTEST_VALIDATE_IGNORE_OUTPUT': ('check', False), # For backwards compatibility
'NBVAL_IGNORE_OUTPUT': ('check', False),
'NBVAL_TEST_NAME': ('name', str),
'NBVAL_CHECK_OUTPUT': 'check',
'NBVAL_RAISES_EXCEPTION': 'check_exception',
'NBVAL_SKIP': 'skip',
Expand All @@ -177,14 +178,22 @@ def find_comment_markers(cellsource):
line = line.strip()
if line.startswith('#'):
# print("Found comment in '{}'".format(line))
comment = line.lstrip('#').strip()
comment_val = line.lstrip('#').strip()
if ':' in comment_val:
comment, val = comment_val.split(':', 1)
comment = comment.rstrip()
val = val.lstrip()
else:
comment = comment_val
if comment in comment_markers:
# print("Found marker {}".format(comment))
marker = comment_markers[comment]
if not isinstance(marker, tuple):
# If not an explicit tuple ('option', True/False),
# imply ('option', True)
marker = (marker, True)
elif marker[1] is str:
marker = (marker[0], val)
marker_type = marker[0]
if marker_type in found:
warnings.warn(
Expand Down Expand Up @@ -271,7 +280,7 @@ def setup(self):
self.kernel = RunningKernel(
kernel_name,
cwd=str(self.fspath.dirname),
startup_timeout=self.config.option.nbval_kernel_startup_timeout,
startup_timeout=self.config.option.nbval_kernel_startup_timeout,
)
self.setup_sanitize_files()
if getattr(self.parent.config.option, 'cov_source', None):
Expand Down Expand Up @@ -331,6 +340,8 @@ def collect(self):
with warnings.catch_warnings(record=True) as ws:
options = defaultdict(bool, find_metadata_tags(cell.metadata))
comment_opts = dict(find_comment_markers(cell.source))
# Update 'code' cell count
cell_num += 1
loc = '%s:Cell %d' % (getattr(self, "fspath", None), cell_num)
if set(comment_opts.keys()) & set(options.keys()):
warnings.warn_explicit(
Expand All @@ -349,8 +360,9 @@ def collect(self):
lineno=0
)
options.update(comment_opts)
options.setdefault('name', 'Cell %d' % cell_num)
options.setdefault('check', self.compare_outputs)
name = 'Cell ' + str(cell_num)
name = options['name']
# https://docs.pytest.org/en/stable/deprecations.html#node-construction-changed-to-node-from-parent
if hasattr(IPyNbCell, "from_parent"):
yield IPyNbCell.from_parent(
Expand All @@ -359,9 +371,6 @@ def collect(self):
else:
yield IPyNbCell(name, self, cell_num, cell, options)

# Update 'code' cell count
cell_num += 1

def teardown(self):
if self.kernel is not None and self.kernel.is_alive():
if getattr(self.parent.config.option, 'cov_source', None):
Expand Down

0 comments on commit c732286

Please sign in to comment.