Skip to content

Commit 5a89897

Browse files
committed
Add tmpdir examples
1 parent 5ca4a98 commit 5a89897

File tree

2 files changed

+120
-10
lines changed

2 files changed

+120
-10
lines changed

notebooks/intermediate/html/pytest_fixtures.html

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11843,11 +11843,11 @@ <h1 id="Efficient-use-of-pytest-fixtures">Efficient use of <code>pytest</code> f
1184311843
<div class="output_subarea output_stream output_stdout output_text">
1184411844
<pre>Requirement already satisfied: pytest in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (3.5.0)
1184511845
Requirement already satisfied: ipytest in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (0.2.2)
11846-
Requirement already satisfied: py&gt;=1.5.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (1.5.3)
11847-
Requirement already satisfied: attrs&gt;=17.4.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (17.4.0)
1184811846
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)
1184911847
Requirement already satisfied: six&gt;=1.10.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (1.11.0)
1185011848
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)
11849+
Requirement already satisfied: attrs&gt;=17.4.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (17.4.0)
11850+
Requirement already satisfied: py&gt;=1.5.0 in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (1.5.3)
1185111851
Requirement already satisfied: setuptools in /Users/jerry/.virtualenvs/learn-python3/lib/python3.5/site-packages (from pytest) (39.0.1)
1185211852
</pre>
1185311853
</div>
@@ -12215,13 +12215,88 @@ <h3 id="monkeypatch"><a href="https://docs.pytest.org/en/latest/reference.html#_
1221512215
</div>
1221612216
<div class="inner_cell">
1221712217
<div class="text_cell_render border-box-sizing rendered_html">
12218-
<h2 id="Fixture-scope">Fixture scope<a class="anchor-link" href="#Fixture-scope">&#182;</a></h2>
12218+
<h3 id="tmpdir"><a href="https://docs.pytest.org/en/latest/tmpdir.html#the-tmpdir-fixture"><code>tmpdir</code></a><a class="anchor-link" href="#tmpdir">&#182;</a></h3><p><a href="https://docs.pytest.org/en/latest/tmpdir.html#the-tmpdir-fixture"><code>tmpdir</code></a> fixture provides functionality for creating temporary files and directories.</p>
12219+
1221912220
</div>
1222012221
</div>
1222112222
</div>
1222212223
<div class="cell border-box-sizing code_cell rendered">
1222312224
<div class="input">
1222412225
<div class="prompt input_prompt">In&nbsp;[12]:</div>
12226+
<div class="inner_cell">
12227+
<div class="input_area">
12228+
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">word_count_of_txt_file</span><span class="p">(</span><span class="n">file_path</span><span class="p">):</span>
12229+
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
12230+
<span class="n">content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
12231+
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">content</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
12232+
</pre></div>
12233+
12234+
</div>
12235+
</div>
12236+
</div>
12237+
12238+
</div>
12239+
<div class="cell border-box-sizing code_cell rendered">
12240+
<div class="input">
12241+
<div class="prompt input_prompt">In&nbsp;[13]:</div>
12242+
<div class="inner_cell">
12243+
<div class="input_area">
12244+
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%%</span><span class="k">run_pytest</span>[clean] &#39;-s&#39;
12245+
12246+
def test_word_count(tmpdir):
12247+
test_file = tmpdir.join(&#39;test.txt&#39;)
12248+
test_file.write(&#39;This is example content of seven words&#39;)
12249+
res = word_count_of_txt_file(str(test_file)) # str returns the path
12250+
assert res == 7
12251+
</pre></div>
12252+
12253+
</div>
12254+
</div>
12255+
</div>
12256+
12257+
<div class="output_wrapper">
12258+
<div class="output">
12259+
12260+
12261+
<div class="output_area">
12262+
12263+
<div class="prompt"></div>
12264+
12265+
12266+
<div class="output_subarea output_stream output_stdout output_text">
12267+
<pre>=========================================================== test session starts ===========================================================
12268+
platform darwin -- Python 3.5.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
12269+
rootdir: /Users/jerry/github/jerry-git/learn-python3, inifile: pytest.ini
12270+
plugins: nbval-0.9.0
12271+
collected 1 item
12272+
12273+
pytest_fixtures.py .
12274+
12275+
============================================================ warnings summary =============================================================
12276+
None
12277+
Module already imported so cannot be rewritten: nbval
12278+
12279+
-- Docs: http://doc.pytest.org/en/latest/warnings.html
12280+
================================================== 1 passed, 1 warnings in 0.03 seconds ===================================================
12281+
</pre>
12282+
</div>
12283+
</div>
12284+
12285+
</div>
12286+
</div>
12287+
12288+
</div>
12289+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
12290+
</div>
12291+
<div class="inner_cell">
12292+
<div class="text_cell_render border-box-sizing rendered_html">
12293+
<h2 id="Fixture-scope">Fixture scope<a class="anchor-link" href="#Fixture-scope">&#182;</a></h2>
12294+
</div>
12295+
</div>
12296+
</div>
12297+
<div class="cell border-box-sizing code_cell rendered">
12298+
<div class="input">
12299+
<div class="prompt input_prompt">In&nbsp;[14]:</div>
1222512300
<div class="inner_cell">
1222612301
<div class="input_area">
1222712302
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># scope is function also by default</span>
@@ -12245,7 +12320,7 @@ <h2 id="Fixture-scope">Fixture scope<a class="anchor-link" href="#Fixture-scope"
1224512320
</div>
1224612321
<div class="cell border-box-sizing code_cell rendered">
1224712322
<div class="input">
12248-
<div class="prompt input_prompt">In&nbsp;[13]:</div>
12323+
<div class="prompt input_prompt">In&nbsp;[15]:</div>
1224912324
<div class="inner_cell">
1225012325
<div class="input_area">
1225112326
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%%</span><span class="k">run_pytest</span>[clean] &#39;-s&#39;
@@ -12311,7 +12386,7 @@ <h2 id="Setup-teardown-behaviour">Setup-teardown behaviour<a class="anchor-link"
1231112386
</div>
1231212387
<div class="cell border-box-sizing code_cell rendered">
1231312388
<div class="input">
12314-
<div class="prompt input_prompt">In&nbsp;[14]:</div>
12389+
<div class="prompt input_prompt">In&nbsp;[16]:</div>
1231512390
<div class="inner_cell">
1231612391
<div class="input_area">
1231712392
<div class=" highlight hl-ipython3"><pre><span></span><span class="nd">@pytest</span><span class="o">.</span><span class="n">fixture</span>
@@ -12328,7 +12403,7 @@ <h2 id="Setup-teardown-behaviour">Setup-teardown behaviour<a class="anchor-link"
1232812403
</div>
1232912404
<div class="cell border-box-sizing code_cell rendered">
1233012405
<div class="input">
12331-
<div class="prompt input_prompt">In&nbsp;[15]:</div>
12406+
<div class="prompt input_prompt">In&nbsp;[17]:</div>
1233212407
<div class="inner_cell">
1233312408
<div class="input_area">
1233412409
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%%</span><span class="k">run_pytest</span>[clean] &#39;-s&#39;
@@ -12390,7 +12465,7 @@ <h2 id="Using-fixtures-automatically">Using fixtures automatically<a class="anch
1239012465
</div>
1239112466
<div class="cell border-box-sizing code_cell rendered">
1239212467
<div class="input">
12393-
<div class="prompt input_prompt">In&nbsp;[16]:</div>
12468+
<div class="prompt input_prompt">In&nbsp;[18]:</div>
1239412469
<div class="inner_cell">
1239512470
<div class="input_area">
1239612471
<div class=" highlight hl-ipython3"><pre><span></span><span class="nd">@pytest</span><span class="o">.</span><span class="n">fixture</span><span class="p">(</span><span class="n">autouse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
@@ -12405,7 +12480,7 @@ <h2 id="Using-fixtures-automatically">Using fixtures automatically<a class="anch
1240512480
</div>
1240612481
<div class="cell border-box-sizing code_cell rendered">
1240712482
<div class="input">
12408-
<div class="prompt input_prompt">In&nbsp;[17]:</div>
12483+
<div class="prompt input_prompt">In&nbsp;[19]:</div>
1240912484
<div class="inner_cell">
1241012485
<div class="input_area">
1241112486
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%%</span><span class="k">run_pytest</span>[clean] &#39;-s&#39;
@@ -12448,7 +12523,7 @@ <h2 id="Using-fixtures-automatically">Using fixtures automatically<a class="anch
1244812523
Module already imported so cannot be rewritten: nbval
1244912524

1245012525
-- Docs: http://doc.pytest.org/en/latest/warnings.html
12451-
================================================== 2 passed, 1 warnings in 0.02 seconds ===================================================
12526+
================================================== 2 passed, 1 warnings in 0.03 seconds ===================================================
1245212527
</pre>
1245312528
</div>
1245412529
</div>

notebooks/intermediate/pytest_fixtures.ipynb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,41 @@
211211
" SomeClass.tell_the_truth()"
212212
]
213213
},
214+
{
215+
"cell_type": "markdown",
216+
"metadata": {},
217+
"source": [
218+
"### [`tmpdir`](https://docs.pytest.org/en/latest/tmpdir.html#the-tmpdir-fixture)\n",
219+
"[`tmpdir`](https://docs.pytest.org/en/latest/tmpdir.html#the-tmpdir-fixture) fixture provides functionality for creating temporary files and directories."
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {},
226+
"outputs": [],
227+
"source": [
228+
"def word_count_of_txt_file(file_path):\n",
229+
" with open(file_path, 'r') as f:\n",
230+
" content = f.read()\n",
231+
" return len(content.split())"
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": null,
237+
"metadata": {},
238+
"outputs": [],
239+
"source": [
240+
"%%run_pytest[clean] '-s'\n",
241+
"\n",
242+
"def test_word_count(tmpdir):\n",
243+
" test_file = tmpdir.join('test.txt')\n",
244+
" test_file.write('This is example content of seven words')\n",
245+
" res = word_count_of_txt_file(str(test_file)) # str returns the path\n",
246+
" assert res == 7"
247+
]
248+
},
214249
{
215250
"cell_type": "markdown",
216251
"metadata": {},
@@ -250,7 +285,7 @@
250285
" pass\n",
251286
"\n",
252287
"def test_something_else(func_fixture, module_fixture, session_fixture):\n",
253-
" pass\n"
288+
" pass"
254289
]
255290
},
256291
{

0 commit comments

Comments
 (0)