Skip to content

Commit 966edac

Browse files
kernel flag (#58)
1 parent 3705acd commit 966edac

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ pytest --nbmake **/*ipynb
2525

2626
## Allow errors and Configure Cell Timeouts
2727

28-
You can configure the notebook run timeout with a the following pytest flag:
28+
You can configure the cell timeout with the following pytest flag:
2929

3030
```sh
31-
pytest --nbmake --nbmake-timeout=3000 # allows each notebook 3000 seconds to finish
31+
pytest --nbmake --nbmake-timeout=3000 # allows each cell 3000 seconds to finish
3232
```
3333

3434
Each notebook can also be separately overidden to allow errors and fail if running exceeds a timeout.
3535

36-
This configuration must be placed in the notebook's top-level metadata (not cell-level metadata).
36+
This configuration must be placed in the notebook's **top-level metadata** (not cell-level metadata).
3737

3838
Your notebook should look like this:
3939

@@ -50,10 +50,17 @@ Your notebook should look like this:
5050
}
5151
```
5252

53+
## Override Notebook Kernels when Testing
54+
55+
Regardless of the kernel configured in the notebook JSON, you can force nbmake to use a specific kernel when testing:
56+
57+
```
58+
pytest --nbmake --nbmake-kernel=mycustomkernel
59+
```
5360

5461
## Add Missing Jupyter Kernel to Your CI Environment
5562

56-
If you are using a kernel name other than the default ‘python3’. You will see an error message when executing your notebooks in a fresh CI environment: `Error - No such kernel: 'mycustomkernel'`
63+
If you are not using the flag above and are using a kernel name other than the default ‘python3’, you will see an error message when executing your notebooks in a fresh CI environment: `Error - No such kernel: 'mycustomkernel'`
5764

5865
Use ipykernel to install the custom kernel:
5966

src/nbmake/nb_run.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ def __init__(
2323
filename: Path,
2424
default_timeout: int,
2525
verbose: bool = False,
26+
kernel: Optional[str] = None,
2627
) -> None:
2728
self.filename = filename
2829
self.verbose = verbose
2930
self.default_timeout = default_timeout
31+
self.kernel = kernel
3032

3133
def execute(
3234
self,
@@ -46,12 +48,17 @@ def execute(
4648

4749
error: Optional[NotebookError] = None
4850

51+
extra_kwargs = {}
52+
if self.kernel:
53+
extra_kwargs["kernel_name"] = self.kernel
54+
4955
try:
5056
c = NotebookClient(
5157
nb,
5258
timeout=timeout,
5359
allow_errors=allow_errors,
5460
record_timing=True,
61+
**extra_kwargs,
5562
)
5663
c.execute(cwd=self.filename.parent)
5764
except CellExecutionError:

src/nbmake/pytest_items.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def runtest(self):
4646
source,
4747
option.nbmake_timeout,
4848
verbose=bool(option.verbose),
49+
kernel=option.nbmake_kernel,
4950
)
5051

5152
res: NotebookResult = run.execute()

src/nbmake/pytest_plugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ def pytest_addoption(parser: Any):
2929
group.addoption(
3030
"--nbmake-timeout",
3131
action="store",
32-
help="Sets the default timeout for a notebook (seconds)",
32+
help="Sets the default cell timeout (seconds)",
3333
default=300,
3434
type=int,
3535
)
36+
group.addoption(
37+
"--nbmake-kernel",
38+
action="store",
39+
help="Overrides the kernel used for all notebooks",
40+
type=str,
41+
)
3642

3743

3844
def pytest_collect_file(path: str, parent: Any) -> Optional[Any]:

tests/test_pytest_plugin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,21 @@ def test_when_explicit_metadata_then_ignore_timeout(testdir: Testdir):
163163

164164
hook_recorder = testdir.inline_run("--nbmake", "--nbmake-timeout=1")
165165
assert hook_recorder.ret == ExitCode.OK
166+
167+
168+
def test_when_kernel_passed_then_override(testdir: Testdir):
169+
write_nb(
170+
passing_nb,
171+
Path(f"x.ipynb"),
172+
metadata={
173+
"kernelspec": {
174+
"display_name": "Python 3",
175+
"language": "python",
176+
"name": "blah",
177+
}
178+
},
179+
)
180+
181+
hook_recorder = testdir.inline_run("--nbmake", "--nbmake-kernel=python3")
182+
183+
assert hook_recorder.ret == ExitCode.OK

0 commit comments

Comments
 (0)