Skip to content

Commit

Permalink
Merge pull request #142 from ceball/named_kernel
Browse files Browse the repository at this point in the history
Add option to specify a kernel
  • Loading branch information
vidartf committed Jan 6, 2021
2 parents 4af78a7 + c6e92a3 commit deb72e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Specify `-p no:python` if you would like to execute notebooks only. Alternativel

py.test --nbval my_notebook.ipynb

By default, each `.ipynb` file will be executed using the kernel
specified in its metadata. You can override this behavior by passing
either `--nbval-kernel-name mykernel` to run all the notebooks using
`mykernel`, or `--current-env` to use a kernel in the same environment
in which pytest itself was launched.

If the output lines are going to be sanitized, an extra flag, `--sanitize-with`
together with the path to a confguration file with regex expressions, must be passed,
i.e.
Expand Down
21 changes: 18 additions & 3 deletions nbval/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ def pytest_addoption(parser):

group.addoption('--current-env', action='store_true',
help='Force test execution to use a python kernel in '
'the same enviornment that py.test was '
'launched from.')
'the same environment that py.test was '
'launched from. Without this flag, the kernel stored '
'in the notebook is used by default. '
'See also: --nbval-kernel-name')

group.addoption('--nbval-kernel-name', action='store', default=None,
help='Force test execution to use the named kernel. '
'If a kernel is not named, the kernel stored in the '
'notebook is used by default. '
'See also: --current-env')

group.addoption('--nbval-cell-timeout', action='store', default=2000,
type=float,
Expand All @@ -103,6 +111,10 @@ def pytest_configure(config):
from .nbdime_reporter import NbdimeReporter
reporter = NbdimeReporter(config, sys.stdout)
config.pluginmanager.register(reporter, 'nbdimereporter')
if config.option.nbval or config.option.nbval_lax:
if config.option.nbval_kernel_name and config.option.current_env:
raise ValueError("--current-env and --nbval-kernel-name are mutually exclusive.")



def pytest_collect_file(path, parent):
Expand Down Expand Up @@ -227,9 +239,12 @@ def setup(self):
Called by pytest to setup the collector cells in .
Here we start a kernel and setup the sanitize patterns.
"""

# we've already checked that --current-env and
# --nbval-kernel-name were not both supplied
if self.parent.config.option.current_env:
kernel_name = CURRENT_ENV_KERNEL_NAME
elif self.parent.config.option.nbval_kernel_name:
kernel_name = self.parent.config.option.nbval_kernel_name
else:
kernel_name = self.nb.metadata.get(
'kernelspec', {}).get('name', 'python')
Expand Down

0 comments on commit deb72e6

Please sign in to comment.