From 0782bcf4443787966562daba8fa1199a14a562f0 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Tue, 9 Mar 2021 01:39:36 +0000 Subject: [PATCH] add NBVAL_TEST_NAME, offset cell indices by 1 - fixes #111 --- nbval/plugin.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/nbval/plugin.py b/nbval/plugin.py index ee3ff29..ac5cb0b 100644 --- a/nbval/plugin.py +++ b/nbval/plugin.py @@ -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( @@ -147,6 +147,7 @@ def pytest_collect_file(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', @@ -170,7 +171,13 @@ 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] @@ -178,6 +185,8 @@ def find_comment_markers(cellsource): # 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( @@ -264,7 +273,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): @@ -324,6 +333,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( @@ -342,15 +353,13 @@ def collect(self): lineno=0 ) options.update(comment_opts) + options.setdefault('name', f'Cell {cell_num:d}') options.setdefault('check', self.compare_outputs) - name = 'Cell ' + str(cell_num) + name = options['name'] yield IPyNbCell.from_parent( self, name=name, cell_num=cell_num, cell=cell, options=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):