Skip to content

Commit d6827c4

Browse files
author
craigsdennis
committed
Adds widget based tests
1 parent b152b86 commit d6827c4

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

binder/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ dependencies:
55
- numpy=1.15.0
66
- pandas=0.23.4
77
- python=3.7.0
8+
- ipywidgets
89
- faker
910

s2n03-challenge1-top-referrers.ipynb

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"\n",
1111
"Currently, anyone with **5 or more** referrals is considered a top referrer.\n",
1212
"\n",
13-
"In the cell labelled CHALLENGE, follow the instructions to return what is required. Use `Kernel > Restart and run all cells` to check your work. If all the tests pass, you got it!"
13+
"When you have completed the challenge, press the `Run Tests` button"
1414
]
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 1,
18+
"execution_count": 37,
1919
"metadata": {},
2020
"outputs": [
2121
{
@@ -24,7 +24,7 @@
2424
"475"
2525
]
2626
},
27-
"execution_count": 1,
27+
"execution_count": 37,
2828
"metadata": {},
2929
"output_type": "execute_result"
3030
}
@@ -44,7 +44,7 @@
4444
},
4545
{
4646
"cell_type": "code",
47-
"execution_count": 2,
47+
"execution_count": 38,
4848
"metadata": {},
4949
"outputs": [
5050
{
@@ -223,7 +223,7 @@
223223
"[475 rows x 7 columns]"
224224
]
225225
},
226-
"execution_count": 2,
226+
"execution_count": 38,
227227
"metadata": {},
228228
"output_type": "execute_result"
229229
}
@@ -238,9 +238,68 @@
238238
},
239239
{
240240
"cell_type": "code",
241-
"execution_count": 3,
241+
"execution_count": 39,
242242
"metadata": {},
243243
"outputs": [
244+
{
245+
"data": {
246+
"application/javascript": [
247+
"IPython.notebook.execute_all_cells();"
248+
],
249+
"text/plain": [
250+
"<IPython.core.display.Javascript object>"
251+
]
252+
},
253+
"metadata": {},
254+
"output_type": "display_data"
255+
},
256+
{
257+
"data": {
258+
"application/vnd.jupyter.widget-view+json": {
259+
"model_id": "6dda60d04f0040859bd5f5db7db3eeea",
260+
"version_major": 2,
261+
"version_minor": 0
262+
},
263+
"text/plain": [
264+
"Button(description='Run Tests', style=ButtonStyle())"
265+
]
266+
},
267+
"metadata": {},
268+
"output_type": "display_data"
269+
},
270+
{
271+
"data": {
272+
"text/markdown": [
273+
"```\n",
274+
"...\n",
275+
"----------------------------------------------------------------------\n",
276+
"Ran 3 tests in 0.004s\n",
277+
"\n",
278+
"OK\n",
279+
"\n",
280+
"```"
281+
],
282+
"text/plain": [
283+
"<IPython.core.display.Markdown object>"
284+
]
285+
},
286+
"metadata": {},
287+
"output_type": "display_data"
288+
},
289+
{
290+
"data": {
291+
"application/vnd.jupyter.widget-view+json": {
292+
"model_id": "5fdab85d808240b5ba79999461a20256",
293+
"version_major": 2,
294+
"version_minor": 0
295+
},
296+
"text/plain": [
297+
"Button(description='Run Tests', style=ButtonStyle())"
298+
]
299+
},
300+
"metadata": {},
301+
"output_type": "display_data"
302+
},
244303
{
245304
"data": {
246305
"text/markdown": [
@@ -271,7 +330,7 @@
271330
"AssertionError: 475 != 142 : Whoops I received a different count than I expected, make sure the last line is the entire resulting DataFrame (not just the head)\n",
272331
"\n",
273332
"----------------------------------------------------------------------\n",
274-
"Ran 3 tests in 0.005s\n",
333+
"Ran 3 tests in 0.004s\n",
275334
"\n",
276335
"FAILED (failures=3)\n",
277336
"\n",
@@ -288,6 +347,13 @@
288347
"source": [
289348
"check(__name__, 'Find the top referrers')"
290349
]
350+
},
351+
{
352+
"cell_type": "code",
353+
"execution_count": null,
354+
"metadata": {},
355+
"outputs": [],
356+
"source": []
291357
}
292358
],
293359
"metadata": {
@@ -306,7 +372,7 @@
306372
"name": "python",
307373
"nbconvert_exporter": "python",
308374
"pygments_lexer": "ipython3",
309-
"version": "3.7.0"
375+
"version": "3.7.1"
310376
}
311377
},
312378
"nbformat": 4,

tests/helpers.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import sys
55
import unittest
66

7-
from IPython.display import display, Markdown
7+
from IPython.display import display, Markdown, Javascript
8+
import ipywidgets as widgets
89

910

1011
registered_tests = {}
@@ -23,9 +24,13 @@ def wrapped(cls):
2324
def cell_matching(module, test_text):
2425
# In is a list of notebook cell inputs
2526
# Out is a dictionary of cell outputs
27+
matching_indices = []
2628
for index, value in enumerate(module.In):
2729
if test_text in value:
28-
return Cell(value, module.Out.get(index))
30+
matching_indices.append(index)
31+
if matching_indices:
32+
# The last entry is the call to `check`
33+
return Cell(value, module.Out.get(matching_indices[-2]))
2934
return None, None
3035

3136

@@ -43,6 +48,9 @@ class BoundTestClass(test_cls):
4348
BoundTestClass.__doc__ = "Cell Tests for " + test_text
4449
return BoundTestClass
4550

51+
def execute_all_cells(b):
52+
display(Javascript('''IPython.notebook.execute_all_cells();'''))
53+
4654

4755
def check(module_name, test_text):
4856
module = sys.modules[module_name]
@@ -51,4 +59,7 @@ def check(module_name, test_text):
5159
runner = unittest.TextTestRunner(stream=output_stream)
5260
runner.run(unittest.defaultTestLoader.loadTestsFromTestCase(test_class))
5361
md = '```\n' + output_stream.getvalue() + '\n```'
62+
button = widgets.Button(description='Run Tests')
63+
button.on_click(execute_all_cells)
64+
display(button)
5465
display(Markdown(md))

0 commit comments

Comments
 (0)