From 979c86b26ff916baec67bfe3d95b39facf6ca388 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 34dcce5..4fdc206 100644 --- a/nbval/plugin.py +++ b/nbval/plugin.py @@ -104,7 +104,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( @@ -150,6 +150,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', @@ -173,7 +174,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] @@ -181,6 +188,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( @@ -267,7 +276,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): @@ -327,6 +336,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( @@ -345,8 +356,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( @@ -355,9 +367,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):