<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fixtures Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/fixtures/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/fixtures/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Fri, 02 Sep 2022 06:29:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>pytest Tutorial &#8211; Part 1</title>
		<link>https://creatronix.de/pytest-tutorial-part-1/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Wed, 08 Jan 2020 08:45:38 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[fixtures]]></category>
		<category><![CDATA[pytest]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3001</guid>

					<description><![CDATA[<p>At PyConDE Berlin 2019 Florian Bruhin gave a really nice session about testing with pytest, which I try to recap here. Writing Tests If You want to work with pytest you can install it via: pip install pytest When you know basic python unittest fixtures, good news ahead: pytest is compatible and will run your&#8230;</p>
<p>The post <a href="https://creatronix.de/pytest-tutorial-part-1/">pytest Tutorial &#8211; Part 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>At <a href="https://creatronix.de/pyconde-berlin-2019/">PyConDE Berlin 2019</a> Florian Bruhin gave a really nice session about testing with pytest, which I try to recap here.</p>
<h2>Writing Tests</h2>
<p>If You want to work with pytest you can install it via:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>pip install pytest</code></pre>
</div>
<p>When you know basic python unittest fixtures, good news ahead:</p>
<p>pytest is compatible and will run your old unittest classes as well, but pytest doesn&#8217;t need to have a testclass derived from some subclass.</p>
<p><strong>It is sufficient to have a single python file which name starts with <em>test_</em> containing a function which name also starts with <em>test_</em></strong></p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>def test_stuff():
    x = 5.0
    assert x == 5</code></pre>
</div>
<p>That&#8217;s all You need!</p>
<h2>Autodiscovery</h2>
<p>If you run pytest from the command line it will automatically discover tests which cohere to this naming convention like unittest -discover</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>pytest</code></pre>
</div>
<h2>Select test cases</h2>
<p>You can specify the tests to be run by module::test_function or by module::test_class::test_method e.g.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>pytest test_sort_algortihms.py::TestSortAlgorithms::test_python_sorted</code></pre>
</div>
<p>As an alternative you can use the option -k &#8220;test_case_name&#8221; to glob testcases.</p>
<h2>Execution time</h2>
<p><span class="comment-copy">If you pass <code>--durations=0</code> execution time for all tests will be reported.</span></p>
<h2>Useful command line arguments</h2>
<p>When learning pytest, it is good to learn some command line flags as well</p>
<ul>
<li>-h : help</li>
<li>-k &#8220;&lt;name_of_test&gt;&#8221; : glob test name</li>
<li>-x : quit on first error</li>
<li>&#8211; -pdb : start debugger on error</li>
<li>-v : increase verbosity</li>
<li>-s : show print output</li>
<li>-rf : reason for failure</li>
<li>-rs : reason for skipping</li>
<li>-m &lt;marker_name&gt; : run only marked tests</li>
<li>&#8211;fixtures : show fixtures in modules</li>
</ul>
<h2>Configuration via file</h2>
<p>You can save your preferences for running pytest in a file called pytest.ini</p>
<pre>[pytest]
addopts = -x --durations=0 -vv</pre>
<h2>Understanding Fixtures</h2>
<p>Fixtures are pytest&#8217;s equivalent to setUp methods from unittest with the difference that you can define when they should be used.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>import pytest 

@pytest.fixture 
def some_value(): 
    return 42 

def test_function(some_value): 
    assert some_value == 42</code></pre>
</div>
<p>After declaring a fixture with decorator @pytest.fixture you can use this special function as a parameter to your test functions.</p>
<h3>Fixture scope</h3>
<p>Without any further definition a fixture function is called before every function it is used in. When You do some heavy lifting in a fixture and you want to reuse that you can specify the module scope so that the fixture function just runs once per module</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>@pytest.fixture(scope="module")</code></pre>
</div>
<p>If you even want to instantiate a fixture once per session this is also possible with</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>@pytest.fixture(scope="session")</code></pre>
</div>
<p>Next part <a href="https://creatronix.de/pytest-tutorial-part-2/">pytest Tutorial &#8211;  Part 2</a></p>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/pytest-tutorial-part-1/">pytest Tutorial &#8211; Part 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
