Skip to content

Commit deb72e6

Browse files
authored
Merge pull request #142 from ceball/named_kernel
Add option to specify a kernel
2 parents 4af78a7 + c6e92a3 commit deb72e6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ Specify `-p no:python` if you would like to execute notebooks only. Alternativel
7272

7373
py.test --nbval my_notebook.ipynb
7474

75+
By default, each `.ipynb` file will be executed using the kernel
76+
specified in its metadata. You can override this behavior by passing
77+
either `--nbval-kernel-name mykernel` to run all the notebooks using
78+
`mykernel`, or `--current-env` to use a kernel in the same environment
79+
in which pytest itself was launched.
80+
7581
If the output lines are going to be sanitized, an extra flag, `--sanitize-with`
7682
together with the path to a confguration file with regex expressions, must be passed,
7783
i.e.

nbval/plugin.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,16 @@ def pytest_addoption(parser):
8181

8282
group.addoption('--current-env', action='store_true',
8383
help='Force test execution to use a python kernel in '
84-
'the same enviornment that py.test was '
85-
'launched from.')
84+
'the same environment that py.test was '
85+
'launched from. Without this flag, the kernel stored '
86+
'in the notebook is used by default. '
87+
'See also: --nbval-kernel-name')
88+
89+
group.addoption('--nbval-kernel-name', action='store', default=None,
90+
help='Force test execution to use the named kernel. '
91+
'If a kernel is not named, the kernel stored in the '
92+
'notebook is used by default. '
93+
'See also: --current-env')
8694

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

107119

108120
def pytest_collect_file(path, parent):
@@ -227,9 +239,12 @@ def setup(self):
227239
Called by pytest to setup the collector cells in .
228240
Here we start a kernel and setup the sanitize patterns.
229241
"""
230-
242+
# we've already checked that --current-env and
243+
# --nbval-kernel-name were not both supplied
231244
if self.parent.config.option.current_env:
232245
kernel_name = CURRENT_ENV_KERNEL_NAME
246+
elif self.parent.config.option.nbval_kernel_name:
247+
kernel_name = self.parent.config.option.nbval_kernel_name
233248
else:
234249
kernel_name = self.nb.metadata.get(
235250
'kernelspec', {}).get('name', 'python')

0 commit comments

Comments
 (0)