Skip to content

Commit c37f927

Browse files
committed
Start using ipytest
1 parent 16a7f9c commit c37f927

6 files changed

Lines changed: 280 additions & 209 deletions

File tree

notebooks/beginner/exercises/testing1_exercise.ipynb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"# Let's make sure pytest and ipython_pytest plugin are installed\n",
9+
"# Required boilerplate\n",
1010
"import sys\n",
1111
"!{sys.executable} -m pip install pytest\n",
12-
"!{sys.executable} -m pip install ipython_pytest"
13-
]
14-
},
15-
{
16-
"cell_type": "code",
17-
"execution_count": null,
18-
"metadata": {},
19-
"outputs": [],
20-
"source": [
21-
"%load_ext ipython_pytest"
12+
"!{sys.executable} -m pip install ipytest\n",
13+
"\n",
14+
"import ipytest.magics\n",
15+
"import pytest\n",
16+
"\n",
17+
"__file__ = 'testing1_exercise.ipynb'"
2218
]
2319
},
2420
{
@@ -32,24 +28,28 @@
3228
{
3329
"cell_type": "code",
3430
"execution_count": null,
35-
"metadata": {},
31+
"metadata": {
32+
"editable": false
33+
},
3634
"outputs": [],
3735
"source": [
38-
"%%pytest\n",
39-
"\n",
4036
"def get_divisible_by_five(numbers):\n",
4137
" '''Returns a list of numbers which are divisible by five in the list got as an argument'''\n",
4238
" result = []\n",
4339
" for num in numbers:\n",
4440
" if not num % 5:\n",
4541
" result.append(num)\n",
4642
"\n",
47-
" return result\n",
48-
"\n",
49-
"\n",
50-
"#####################\n",
51-
"\n",
52-
"import pytest\n",
43+
" return result"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"%%run_pytest[clean]\n",
5353
"\n",
5454
"def test_get_divisible_by_five():\n",
5555
" # Your implementation here\n"

notebooks/beginner/exercises/testing2_exercise.ipynb

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"# Let's make sure pytest and ipython_pytest plugin are installed\n",
9+
"# Required boilerplate\n",
1010
"import sys\n",
1111
"!{sys.executable} -m pip install pytest\n",
12-
"!{sys.executable} -m pip install ipython_pytest"
13-
]
14-
},
15-
{
16-
"cell_type": "code",
17-
"execution_count": null,
18-
"metadata": {},
19-
"outputs": [],
20-
"source": [
21-
"%load_ext ipython_pytest"
12+
"!{sys.executable} -m pip install ipytest\n",
13+
"\n",
14+
"import ipytest.magics\n",
15+
"import pytest\n",
16+
"\n",
17+
"__file__ = 'testing2_exercise.ipynb'"
2218
]
2319
},
2420
{
@@ -32,17 +28,11 @@
3228
{
3329
"cell_type": "code",
3430
"execution_count": null,
35-
"metadata": {},
31+
"metadata": {
32+
"editable": false
33+
},
3634
"outputs": [],
3735
"source": [
38-
"%%pytest\n",
39-
"\n",
40-
"'''\n",
41-
"This could be in todo_list.py \n",
42-
"\n",
43-
"No need to change TodoNotFound nor TodoList.\n",
44-
"'''\n",
45-
"\n",
4636
"class TodoNotFound(Exception):\n",
4737
" pass\n",
4838
"\n",
@@ -76,17 +66,24 @@
7666
" if number not in self._todo:\n",
7767
" raise TodoNotFound('{} not in todos'.format(number))\n",
7868
"\n",
79-
" del self._todo[number]\n",
80-
"\n",
81-
" \n",
82-
"#############################\n",
83-
"'''\n",
84-
"This could be in test_todo_list.py \n",
85-
"\n",
86-
"Fill ____ parts of the implementation.\n",
87-
"'''\n",
69+
" del self._todo[number]"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"Finalize tests for `TodoList`."
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"%%run_pytest[clean]\n",
8886
"\n",
89-
"import pytest\n",
9087
"\n",
9188
"@pytest.____\n",
9289
"def todo_list():\n",
@@ -141,7 +138,7 @@
141138
"\n",
142139
"def test_remove_with_unknown_task_number(todo_list):\n",
143140
" with pytest.____(____):\n",
144-
" todo_list.remove(12)"
141+
" todo_list.remove(12)\n"
145142
]
146143
},
147144
{
@@ -156,21 +153,24 @@
156153
{
157154
"cell_type": "code",
158155
"execution_count": null,
159-
"metadata": {},
156+
"metadata": {
157+
"editable": false
158+
},
160159
"outputs": [],
161160
"source": [
162-
"%%pytest\n",
163-
"\n",
164-
"\n",
165161
"def fibonacci(number):\n",
166162
" if number in [0, 1]:\n",
167163
" return number\n",
168-
" return fibonacci(number - 1) + fibonacci(number - 2)\n",
169-
"\n",
170-
"\n",
171-
"############################\n",
172-
"\n",
173-
"import pytest\n",
164+
" return fibonacci(number - 1) + fibonacci(number - 2)"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": null,
170+
"metadata": {},
171+
"outputs": [],
172+
"source": [
173+
"%%run_pytest[clean]\n",
174174
"\n",
175175
"# Your implementation here\n"
176176
]

notebooks/beginner/html/testing1.html

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11777,7 +11777,7 @@
1177711777
}
1177811778

1177911779
$( document ).ready(function(){
11780-
show=false;
11780+
show=false;
1178111781
$('div.output').hide()
1178211782
});
1178311783
</script>
@@ -11841,7 +11841,7 @@ <h2 id="Test-driven-development-aka-TDD"><a href="https://en.wikipedia.org/wiki/
1184111841
</div>
1184211842
<div class="inner_cell">
1184311843
<div class="text_cell_render border-box-sizing rendered_html">
11844-
<h3 id="Running-pytest-inside-notebooks">Running pytest inside notebooks<a class="anchor-link" href="#Running-pytest-inside-notebooks">&#182;</a></h3><p>These are the steps required to run pytest inside Jupyter cells.</p>
11844+
<h3 id="Running-pytest-inside-notebooks">Running pytest inside notebooks<a class="anchor-link" href="#Running-pytest-inside-notebooks">&#182;</a></h3><p>These are the steps required to run pytest inside Jupyter cells. You can copy the content of this cell to the top of your notebook which contains tests.</p>
1184511845

1184611846
</div>
1184711847
</div>
@@ -11851,10 +11851,17 @@ <h3 id="Running-pytest-inside-notebooks">Running pytest inside notebooks<a class
1185111851
<div class="prompt input_prompt">In&nbsp;[1]:</div>
1185211852
<div class="inner_cell">
1185311853
<div class="input_area">
11854-
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># Let&#39;s make sure pytest and ipython_pytest plugin are installed</span>
11854+
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># Let&#39;s make sure pytest and ipytest packages are installed</span>
11855+
<span class="c1"># ipytest is required for running pytest inside Jupyter notebooks</span>
1185511856
<span class="kn">import</span> <span class="nn">sys</span>
1185611857
<span class="o">!{</span>sys.executable<span class="o">}</span> -m pip install pytest
11857-
<span class="o">!{</span>sys.executable<span class="o">}</span> -m pip install ipython_pytest
11858+
<span class="o">!{</span>sys.executable<span class="o">}</span> -m pip install ipytest
11859+
11860+
<span class="kn">import</span> <span class="nn">ipytest.magics</span>
11861+
<span class="kn">import</span> <span class="nn">pytest</span>
11862+
11863+
<span class="c1"># Filename has to be set explicitely for ipytest </span>
11864+
<span class="vm">__file__</span> <span class="o">=</span> <span class="s1">&#39;testing1.ipynb&#39;</span>
1185811865
</pre></div>
1185911866

1186011867
</div>
@@ -11872,29 +11879,38 @@ <h3 id="Running-pytest-inside-notebooks">Running pytest inside notebooks<a class
1187211879

1187311880
<div class="output_subarea output_stream output_stdout output_text">
1187411881
<pre>Requirement already satisfied: pytest in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (3.5.0)
11875-
Requirement already satisfied: attrs&gt;=17.4.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (17.4.0)
11876-
Requirement already satisfied: setuptools in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (39.0.1)
1187711882
Requirement already satisfied: py&gt;=1.5.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (1.5.3)
11883+
Requirement already satisfied: six&gt;=1.10.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (1.11.0)
11884+
Requirement already satisfied: attrs&gt;=17.4.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (17.4.0)
1187811885
Requirement already satisfied: pluggy&lt;0.7,&gt;=0.5 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (0.6.0)
1187911886
Requirement already satisfied: more-itertools&gt;=4.0.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (4.1.0)
11880-
Requirement already satisfied: six&gt;=1.10.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (1.11.0)
11881-
Requirement already satisfied: ipython_pytest in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (0.0.1)
11887+
Requirement already satisfied: setuptools in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (39.0.1)
11888+
Requirement already satisfied: ipytest in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (0.2.2)
1188211889
</pre>
1188311890
</div>
1188411891
</div>
1188511892

1188611893
</div>
1188711894
</div>
1188811895

11896+
</div>
11897+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
11898+
</div>
11899+
<div class="inner_cell">
11900+
<div class="text_cell_render border-box-sizing rendered_html">
11901+
<h2 id="pytest-test-cases"><code>pytest</code> test cases<a class="anchor-link" href="#pytest-test-cases">&#182;</a></h2><p>Let's consider we have a function called <code>sum_of_three_numbers</code> for which we want to write a test.</p>
11902+
11903+
</div>
11904+
</div>
1188911905
</div>
1189011906
<div class="cell border-box-sizing code_cell rendered">
1189111907
<div class="input">
1189211908
<div class="prompt input_prompt">In&nbsp;[2]:</div>
1189311909
<div class="inner_cell">
1189411910
<div class="input_area">
11895-
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># Let&#39;s load ipython_pytest extension which makes it possible to run</span>
11896-
<span class="c1"># pytest inside notebooks</span>
11897-
<span class="o">%</span><span class="k">load_ext</span> ipython_pytest
11911+
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># This would be in your e.g. implementation.py</span>
11912+
<span class="k">def</span> <span class="nf">sum_of_three_numbers</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="n">num2</span><span class="p">,</span> <span class="n">num3</span><span class="p">):</span>
11913+
<span class="k">return</span> <span class="n">num1</span> <span class="o">+</span> <span class="n">num2</span> <span class="o">+</span> <span class="n">num3</span>
1189811914
</pre></div>
1189911915

1190011916
</div>
@@ -11906,17 +11922,7 @@ <h3 id="Running-pytest-inside-notebooks">Running pytest inside notebooks<a class
1190611922
</div>
1190711923
<div class="inner_cell">
1190811924
<div class="text_cell_render border-box-sizing rendered_html">
11909-
<p>Due to technical limitations of Jupyter notebooks and pytest extension, all code that runs inside tests has to be in the same cell. In other words, you can not call e.g. functions that are defined in other cells.</p>
11910-
<p>In real applications you have separate directory and .py files for your tests. The above mentioned limititation is only present for writing tests in Jupyter environment.</p>
11911-
11912-
</div>
11913-
</div>
11914-
</div>
11915-
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
11916-
</div>
11917-
<div class="inner_cell">
11918-
<div class="text_cell_render border-box-sizing rendered_html">
11919-
<h2 id="pytest-test-cases"><code>pytest</code> test cases<a class="anchor-link" href="#pytest-test-cases">&#182;</a></h2><p>Pytest test cases are actually quite similar as you have already seen in the exercises. Most of the exercises are structured like pytest test cases by dividing each exercise into three cells:</p>
11925+
<p>Pytest test cases are actually quite similar as you have already seen in the exercises. Most of the exercises are structured like pytest test cases by dividing each exercise into three cells:</p>
1192011926
<ol>
1192111927
<li>Setup the variables used in the test</li>
1192211928
<li>Your implementation</li>
@@ -11932,12 +11938,9 @@ <h2 id="pytest-test-cases"><code>pytest</code> test cases<a class="anchor-link"
1193211938
<div class="prompt input_prompt">In&nbsp;[3]:</div>
1193311939
<div class="inner_cell">
1193411940
<div class="input_area">
11935-
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%%</span><span class="k">pytest</span>
11941+
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%%</span><span class="k">run_pytest</span>[clean]
1193611942
# Mention this at the top of cells which contain test(s)
11937-
11938-
# This would be in your e.g. implementation.py
11939-
def sum_of_three_numbers(num1, num2, num3):
11940-
return num1 + num2 + num3
11943+
# This is only required for running pytest in Jupyter notebooks
1194111944

1194211945

1194311946
# This would be in your test_implementation.py
@@ -11968,15 +11971,15 @@ <h2 id="pytest-test-cases"><code>pytest</code> test cases<a class="anchor-link"
1196811971

1196911972

1197011973
<div class="output_subarea output_stream output_stdout output_text">
11971-
<pre>============================================================ test session starts ============================================================
11974+
<pre>================================================================================================= test session starts =================================================================================================
1197211975
platform darwin -- Python 3.5.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
11973-
rootdir: /private/var/folders/nk/wgt71p5s1_d1wssm881h7ykc0000gn/T/tmpn9irzd9l, inifile:
11976+
rootdir: /Users/jerry/github/jerry-git/learn-python3, inifile: pytest.ini
1197411977
plugins: nbval-0.9.0
1197511978
collected 1 item
1197611979

11977-
_ipytesttmp.py . [100%]
11980+
testing1.py . [100%]
1197811981

11979-
========================================================= 1 passed in 0.02 seconds ==========================================================
11982+
============================================================================================== 1 passed in 0.02 seconds ===============================================================================================
1198011983
</pre>
1198111984
</div>
1198211985
</div>

0 commit comments

Comments
 (0)