Skip to content

Commit 7cef4df

Browse files
committed
Add testing frameworks for notebooks, update documentation
1 parent edd830f commit 7cef4df

4 files changed

Lines changed: 110 additions & 3 deletions

File tree

docs/source/debug.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ These steps are performed using the GNU Debugger (GDB), so ensure it is installe
88

99
.. code-block:: bash
1010
11+
cd build
1112
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_PREFIX_PATH=$CONDA_PREFIX -D CMAKE_INSTALL_PREFIX=$CONDA_PREFIX -D CMAKE_INSTALL_LIBDIR=lib ..
1213
1314
In the same folder, run the command and copy the JSON displayed in the terminal.
@@ -34,5 +35,5 @@ Testing
3435

3536
The source code for the c++ tests is located in `test/test_interpreter.cpp`. The source code for the python tests is located in `test/test_xcpp_kernel.py`.
3637
Write the necessary tests and build the project as described in the repository's README or contributing guidelines.
37-
Then, execute `build/test/test_xeus_cpp` from the top level directory to verify if the c++ tests were successful,
38-
and `pytest -sv build/test/test_xcpp_kernel.py` to execute the python tests.
38+
Then, execute `build/test/test_xeus_cpp` from the top level directory to verify if the c++ tests were successful.
39+
and in the test directory run `pytest -sv build/test/test_xcpp_kernel.py` to execute the python tests.

environment-dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ dependencies:
1818
# Test dependencies
1919
- pytest
2020
- jupyter_kernel_test>=0.5,<0.6
21+
- papermill
22+
- nbformat
2123
- nbval
2224
- pytest-rerunfailures
2325
- doctest

test/Notebooks/Untitled.ipynb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "5b32725b-d7df-4756-b179-68ed3c429d3f",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <iostream>"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "cbf10f24-99a1-434d-b71a-4f8920e5ebb6",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"int a = 12;"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 4,
26+
"id": "f269cf5a-b365-4a1a-9a79-44f2222e6829",
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"12"
34+
]
35+
}
36+
],
37+
"source": [
38+
"std::cout<<a;"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"id": "b1650dcc-0728-4d0f-a156-b8ee0c8b40e3",
45+
"metadata": {},
46+
"outputs": [],
47+
"source": []
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "cpp 20 (xcpp)",
53+
"language": "cpp",
54+
"name": "xcpp"
55+
},
56+
"language_info": {
57+
"codemirror_mode": "text/x-c++src",
58+
"file_extension": ".cpp",
59+
"mimetype": "text/x-c++src",
60+
"name": "C++",
61+
"version": "20"
62+
}
63+
},
64+
"nbformat": 4,
65+
"nbformat_minor": 5
66+
}

test/test_xcpp_kernel.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import unittest
1010
import jupyter_kernel_test
1111
import platform
12-
12+
import papermill as pm
13+
import nbformat
1314

1415
class XCppCompleteTests(jupyter_kernel_test.KernelTests):
1516

@@ -132,6 +133,43 @@ class XCppTests(jupyter_kernel_test.KernelTests):
132133
}
133134
]
134135

136+
#Helper function to execute the notebooks
137+
def execute_notebook(inp, out):
138+
139+
# Execute the notebook
140+
executed_notebook = pm.execute_notebook(
141+
inp,
142+
out,
143+
log_output=True,
144+
kernel_name='xcpp20'
145+
)
146+
147+
# Read the input and output notebooks
148+
with open(inp) as f:
149+
input_nb = nbformat.read(f, as_version=4)
150+
with open(out) as f:
151+
output_nb = nbformat.read(f, as_version=4)
152+
153+
check = True
154+
155+
# Iterate over the cells in the input and output notebooks
156+
for input_cell, output_cell in zip(input_nb.cells, output_nb.cells):
157+
if input_cell.cell_type == 'code' and output_cell.cell_type == 'code':
158+
# Check the outputs of the cells
159+
for input_output, output_output in zip(input_cell.outputs, output_cell.outputs):
160+
if input_output.get('text') != output_output.get('text'):
161+
check = False
162+
break
163+
164+
return check
165+
166+
# Tests for Notebooks
167+
class XCppNotebookTests(unittest.TestCase):
168+
169+
def test_simple_notebook(self):
170+
self.assertEqual(execute_notebook('Notebooks/Untitled.ipynb', 'Notebooks/Untitled_output.ipynb'), True)
171+
172+
135173

136174
class XCppTests2(jupyter_kernel_test.KernelTests):
137175

0 commit comments

Comments
 (0)