<?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>PyTest &#8211; Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/category/pytest/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com</link>
	<description></description>
	<lastBuildDate>Tue, 24 May 2022 07:07:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>PyTest &#8211; Be on the Right Side of Change</title>
	<link>https://blog.finxter.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Test Automation Engineer &#8212; Income and Opportunity</title>
		<link>https://blog.finxter.com/test-automation-engineer-income-and-opportunity/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Tue, 24 May 2022 06:59:42 +0000</pubDate>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[Coding Business]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Freelancing]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[PyTest]]></category>
		<category><![CDATA[Testing]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=386394</guid>

					<description><![CDATA[Before we learn about the money, let&#8217;s get this question out of the way: What is a Test Automation Engineer? A Test Automation Engineer is a software developer who creates automated software tests for existing or new applications. Testing is a crucial phase in the software development cycle to learn about bugs, usability, and security ... <a title="Test Automation Engineer &#8212; Income and Opportunity" class="read-more" href="https://blog.finxter.com/test-automation-engineer-income-and-opportunity/" aria-label="Read more about Test Automation Engineer &#8212; Income and Opportunity">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Before we learn about the money, let&#8217;s get this question out of the way:</p>



<h2 class="wp-block-heading">What is a Test Automation Engineer?</h2>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-274-1024x682.png" alt="" class="wp-image-386559" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-274-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-274-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-274-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-274.png 1028w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="has-global-color-8-background-color has-background wp-block-paragraph">A <strong>Test Automation Engineer</strong> is a software developer who creates automated software tests for existing or new applications. Testing is a crucial phase in the software development cycle to learn about bugs, usability, and security issues and fix them before deploying an application in the real world.</p>



<h2 class="wp-block-heading" id="annual-income">Annual Income of a Test Automation Engineer (US)</h2>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4ac.png" alt="💬" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How much does a Test Automation Engineer in the US make per year?</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="905" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-270.png" alt="Average Income of a Test Automation Engineer in the US by Source." class="wp-image-386504" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-270.png 905w, https://blog.finxter.com/wp-content/uploads/2022/05/image-270-300x191.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-270-768x489.png 768w" sizes="(max-width: 905px) 100vw, 905px" /><figcaption><strong>Figure</strong>: Average Income of a Test Automation Engineer in the US by Source. [1]</figcaption></figure></div>



<p class="has-global-color-8-background-color has-background wp-block-paragraph">The expected annual income of a <strong>Test Automation Engineer</strong> in the United States is between <strong>$74,821</strong> and <strong>$120,000</strong> per year, with an average annual income of <strong>$95,285 </strong>per year and a median income of <strong>$93,657</strong> per year.</p>



<p class="wp-block-paragraph">This data is based on our meta-study of 10 salary aggregators sources such as Glassdoor, ZipRecruiter, and PayScale.</p>



<figure class="wp-block-table is-style-stripes"><table><thead><tr><th>Source</th><th>Average Income</th></tr></thead><tbody><tr><td><a href="https://www.glassdoor.com/Salaries/test-automation-engineer-salary-SRCH_KO0,24.htm" data-type="URL" data-id="https://www.glassdoor.com/Salaries/test-automation-engineer-salary-SRCH_KO0,24.htm" target="_blank" rel="noreferrer noopener">Glassdoor.com</a></td><td>$89,025</td></tr><tr><td><a href="https://www.ziprecruiter.com/Salaries/Automation-TEST-Engineer-Salary" data-type="URL" data-id="https://www.ziprecruiter.com/Salaries/Automation-TEST-Engineer-Salary" target="_blank" rel="noreferrer noopener">ZipRecruiter.com</a></td><td>$98,965</td></tr><tr><td><a href="https://www.talent.com/salary?job=test+automation+engineer" data-type="URL" data-id="https://www.talent.com/salary?job=test+automation+engineer" target="_blank" rel="noreferrer noopener">Talent.com</a></td><td>$105,407</td></tr><tr><td><a href="https://www.indeed.com/career/automation-engineer/salaries" data-type="URL" data-id="https://www.indeed.com/career/automation-engineer/salaries" target="_blank" rel="noreferrer noopener">Indeed.com</a></td><td>$95,314</td></tr><tr><td><a href="https://www.payscale.com/research/US/Skill=Test_Automation/Salary" data-type="URL" data-id="https://www.payscale.com/research/US/Skill=Test_Automation/Salary" target="_blank" rel="noreferrer noopener">PayScale.com</a></td><td>$74,966</td></tr><tr><td><a href="https://hired.com/salaries/qa-test-automation-engineer" data-type="URL" data-id="https://hired.com/salaries/qa-test-automation-engineer" target="_blank" rel="noreferrer noopener">Hired.com</a></td><td>$120,000</td></tr><tr><td><a href="https://www.comparably.com/salaries/salaries-for-automation-engineer" data-type="URL" data-id="https://www.comparably.com/salaries/salaries-for-automation-engineer" target="_blank" rel="noreferrer noopener">Comparably.com</a></td><td>$92,000</td></tr><tr><td><a href="https://www.zippia.com/automation-test-engineer-jobs/salary/" data-type="URL" data-id="https://www.zippia.com/automation-test-engineer-jobs/salary/" target="_blank" rel="noreferrer noopener">Zippia.com</a></td><td>$91,589</td></tr><tr><td><a href="https://builtin.com/salaries/dev-engineer/automation-engineer" data-type="URL" data-id="https://builtin.com/salaries/dev-engineer/automation-engineer" target="_blank" rel="noreferrer noopener">BuiltIn.com</a></td><td>$110,772</td></tr><tr><td><a href="https://www.salary.com/research/salary/posting/automation-test-engineer-salary" data-type="URL" data-id="https://www.salary.com/research/salary/posting/automation-test-engineer-salary" target="_blank" rel="noreferrer noopener">Salary.com</a></td><td>$74,821</td></tr></tbody></table><figcaption><strong>Table</strong>: Average Income of a Test Automation Engineer in the US by Source.</figcaption></figure>



<p class="wp-block-paragraph"></p>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: This is the <strong>most comprehensive salary meta-study</strong> of test automation engineers&#8217; income in the world, to the best of my knowledge!</p>



<p class="wp-block-paragraph">Let&#8217;s have a look at the hourly rate of Test Automation Engineers next!</p>



<h2 class="wp-block-heading" id="hourly-rate">Hourly Rate</h2>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="514" height="685" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-275.png" alt="" class="wp-image-386561" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-275.png 514w, https://blog.finxter.com/wp-content/uploads/2022/05/image-275-225x300.png 225w" sizes="(max-width: 514px) 100vw, 514px" /></figure></div>



<p class="wp-block-paragraph">Test Automation Engineers are well-paid on freelancing platforms such as <a href="https://blog.finxter.com/upwork-vs-fiverr/" data-type="post" data-id="11152" target="_blank" rel="noreferrer noopener">Upwork or Fiverr</a>.</p>



<ul class="wp-block-list"><li><strong>Related Article</strong>: <a href="https://blog.finxter.com/best-python-freelancer-platforms/" data-type="post" data-id="3382" target="_blank" rel="noreferrer noopener">What&#8217;s the best freelancing platform?</a></li></ul>



<p class="has-global-color-8-background-color has-background wp-block-paragraph">If you decide to go the route as a freelance Test Automation Engineer, you can expect to make between $25 and $120 per hour on Upwork (<a href="https://www.upwork.com/ab/profiles/search/?hrs=1000&amp;nss=90&amp;q=Test%20Automation%20Engineer&amp;top_rated_plus=yes&amp;top_rated_status=top_rated" data-type="URL" data-id="https://www.upwork.com/ab/profiles/search/?hrs=1000&amp;nss=90&amp;q=Test%20Automation%20Engineer&amp;top_rated_plus=yes&amp;top_rated_status=top_rated" target="_blank" rel="noreferrer noopener">source</a>). Assuming an annual workload of 2000 hours, you can expect to make between $50,000 and $240,000 per year.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="611" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-271-611x1024.png" alt="" class="wp-image-386511" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-271-611x1024.png 611w, https://blog.finxter.com/wp-content/uploads/2022/05/image-271-179x300.png 179w, https://blog.finxter.com/wp-content/uploads/2022/05/image-271-768x1286.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-271.png 781w" sizes="auto, (max-width: 611px) 100vw, 611px" /></figure></div>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Do you want to create your own thriving coding business online? Feel free to check out our <a href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank" rel="noreferrer noopener">freelance developer course</a> &#8212; the world&#8217;s #1 best-selling freelance developer course that specifically shows you how to succeed on Upwork and Fiverr!</p>



<div class="wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-fe48e5de wp-block-buttons-is-layout-flex">
<div class="wp-block-button has-custom-width wp-block-button__width-50 is-style-fill"><a class="wp-block-button__link has-accent-background-color has-background" href="https://blog.finxter.com/become-python-freelancer-course/" target="_blank" rel="noreferrer noopener">Join Freelancer Course</a></div>
</div>



<h2 class="wp-block-heading">What Does a Test Automation Engineer Do?</h2>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="457" height="685" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-276.png" alt="" class="wp-image-386563" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-276.png 457w, https://blog.finxter.com/wp-content/uploads/2022/05/image-276-200x300.png 200w" sizes="auto, (max-width: 457px) 100vw, 457px" /></figure></div>



<p class="wp-block-paragraph">The daily responsibilities of a <strong>Test Automation Engineer</strong> revolve around the creation of automated testing solutions for software applications. </p>



<p class="wp-block-paragraph">A Test Automation Engineer works closely with teams to find and fix bugs, issues, and problems in an application. </p>



<p class="wp-block-paragraph">To accomplish this, Test Automation Engineers need to constantly research application requirements and develop solutions for process automation. </p>



<p class="wp-block-paragraph">Now that you know about <strong><em>what they do</em></strong>, let&#8217;s have a look at <strong><em>what test automation engineers earn</em></strong> next!</p>



<h2 class="wp-block-heading">How Do I Become a Test Automation Engineer?</h2>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-277-1024x682.png" alt="" class="wp-image-386564" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-277-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-277-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-277-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-277.png 1028w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">Do you want to become a Test Automation Engineer? Here&#8217;s a step-by-step learning path I&#8217;d propose to get started with Test Automation :</p>



<ul class="wp-block-list"><li><strong>Step 1</strong>: <a rel="noreferrer noopener" href="https://pll.harvard.edu/course/cs50-introduction-computer-science?delta=0" data-type="URL" data-id="https://pll.harvard.edu/course/cs50-introduction-computer-science?delta=0" target="_blank">Introduction to Computer Science</a> (~40 hours)</li><li><strong>Step 2</strong>: <a href="https://blog.finxter.com/python-developer-income-and-opportunity/" data-type="post" data-id="189354">Learn a Programming Langu</a><a href="https://blog.finxter.com/python-developer-income-and-opportunity/" data-type="post" data-id="189354" target="_blank" rel="noreferrer noopener">a</a><a href="https://blog.finxter.com/python-developer-income-and-opportunity/" data-type="post" data-id="189354">ge for Testing such as Python</a> (~50 hours)</li><li><strong>Step 3</strong>: <a rel="noreferrer noopener" href="https://www.google.com/search?q=introduction+to+Test Automation +programming" data-type="URL" data-id="https://www.google.com/search?q=introduction+to+Test Automation +programming" target="_blank">Introduction to Test Automation </a> (~20 hours)</li><li><strong>Step 4</strong>: <a rel="noreferrer noopener" href="https://www.youtube.com/results?search_query=introduction+to+Test Automation +programming" data-type="URL" data-id="https://www.youtube.com/results?search_query=introduction+to+Test Automation +programming" target="_blank">Introduction to Test Automation (Free Video Courses)</a> (~10 hours)</li></ul>



<p class="wp-block-paragraph">You can find many additional computer science courses on the <a rel="noreferrer noopener" href="https://academy.finxter.com/" data-type="URL" data-id="https://academy.finxter.com/" target="_blank">Finxter Computer Science Academy</a> (flatrate model).</p>



<p class="wp-block-paragraph">But don&#8217;t wait too long to acquire practical experience! </p>



<p class="wp-block-paragraph">Even if you have little skills, it&#8217;s best to get started as a <strong>freelance developer</strong> and learn as you work on real projects for clients &#8212; <strong>earning income as you learn</strong> and gaining motivation through real-world feedback.</p>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Tip</strong>: An excellent start to turbo-charge your freelancing career (earning more in less time) is our <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank">Finxter Freelancer Course</a>. The goal of the course is to pay for itself! </p>



<h2 class="wp-block-heading">Is Test Automation Engineering a Good Career?</h2>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-278-1024x682.png" alt="" class="wp-image-386565" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-278-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-278-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-278-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-278.png 1028w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><strong>Test Automation Engineering</strong> has the potential to be a good career if you&#8217;re interested in programming and scripting technologies and you love solving challenging problems. </p>



<p class="wp-block-paragraph">The earning potential is great: if you keep learning, you&#8217;re likely to reach a six-figure income in a couple of years in the US or as a freelancer online.</p>



<p class="wp-block-paragraph">You can find great work satisfaction and purpose as an automation engineer. Testing the software, finding bugs, and suggesting improvement ideas is a crucial part of improving the product which can be very rewarding and gratifying. </p>



<p class="wp-block-paragraph">You need to be able to push through problems and challenges and frustrations because if software doesn&#8217;t work and there are deadlines, life can be tough at times. </p>



<p class="wp-block-paragraph"><strong>Recommended Interview</strong>: <a href="https://vungle.com/blog/life-of-a-qa-automation-engineer-with-qamar/" data-type="URL" data-id="https://vungle.com/blog/life-of-a-qa-automation-engineer-with-qamar/" target="_blank" rel="noreferrer noopener">Life as a QA Automation Tester</a></p>



<h2 class="wp-block-heading">Is an Automation Engineer a Software Engineer?</h2>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="678" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-279-1024x678.png" alt="" class="wp-image-386566" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-279-1024x678.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-279-300x199.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-279-768x508.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-279.png 1035w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">Yes, automation engineers are software engineers because automation and testing are part of the software development life cycle. Every software engineer must be able to understand testing and every automation engineer must know software engineering. </p>



<p class="wp-block-paragraph">All automation engineers are software engineers but not all software engineers are automation engineers although all software engineers do testing from time to time. Thus, automation engineers are software engineers!</p>



<p class="wp-block-paragraph">To learn more about the software development lifecycle, check out my detailed book <a rel="noreferrer noopener" href="https://www.amazon.com/Art-Clean-Code-Practices-Complexity/dp/1718502184" data-type="URL" data-id="https://www.amazon.com/Art-Clean-Code-Practices-Complexity/dp/1718502184" target="_blank"><em>The Art of Clean Code</em></a> (NoStarch 2022). </p>



<h2 class="wp-block-heading">Are Automation Engineers in Demand?</h2>



<p class="wp-block-paragraph">But is there enough demand? Let&#8217;s have a look at Google trends to find out how interest evolves over time (<a rel="noreferrer noopener" href="https://trends.google.com/trends/explore?date=all&amp;q=Test Automation" target="_blank">source</a>):</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="461" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-272-1024x461.png" alt="" class="wp-image-386539" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-272-1024x461.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-272-300x135.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-272-768x346.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-272-1536x691.png 1536w, https://blog.finxter.com/wp-content/uploads/2022/05/image-272.png 1766w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="456" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-273-1024x456.png" alt="" class="wp-image-386541" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-273-1024x456.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-273-300x134.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-273-768x342.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-273-1536x684.png 1536w, https://blog.finxter.com/wp-content/uploads/2022/05/image-273.png 1766w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption><a href="https://trends.google.com/trends/explore?date=all&amp;q=test%20automation%20engineer" data-type="URL" data-id="https://trends.google.com/trends/explore?date=all&amp;q=test%20automation%20engineer" target="_blank" rel="noreferrer noopener">source</a></figcaption></figure></div>



<p class="has-global-color-8-background-color has-background wp-block-paragraph">The search demand for test automation has doubled between 2009 and 2022. The search demand for test automation engineer has exploded by roughly 1000% in the last two decades. Thus, <strong>Test Automation Engineers are in high demand today!</strong></p>



<h2 class="wp-block-heading">What is the Future of a Test Automation Engineer?</h2>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="548" height="657" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-280.png" alt="" class="wp-image-386569" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-280.png 548w, https://blog.finxter.com/wp-content/uploads/2022/05/image-280-250x300.png 250w" sizes="auto, (max-width: 548px) 100vw, 548px" /></figure></div>



<p class="wp-block-paragraph"><strong>Test Automation Engineers</strong> are in high demand today. The interest has grown steadily in the last two decades. </p>



<p class="wp-block-paragraph">Every software project needs testing as part of the <a href="https://blog.finxter.com/agile-software-development-scrum/" data-type="post" data-id="246769">software deve</a><a rel="noreferrer noopener" href="https://blog.finxter.com/agile-software-development-scrum/" data-type="post" data-id="246769" target="_blank">l</a><a href="https://blog.finxter.com/agile-software-development-scrum/" data-type="post" data-id="246769">opment lifecycle</a>. </p>



<p class="wp-block-paragraph">As &#8220;software eats the world&#8221;, test automation engineers are likely to keep experiencing an exploding demand and increasing income levels in the decades ahead. </p>



<p class="wp-block-paragraph"><strong>The future looks bright for test automation engineers! </strong><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f31e.png" alt="🌞" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">What Skills are Required for Automation Testing?</h2>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-281-1024x682.png" alt="" class="wp-image-386570" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-281-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/05/image-281-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2022/05/image-281-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2022/05/image-281.png 1028w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">The following 13 skills will help you find work quickly as a <strong>test automation engineer</strong>:</p>



<ol class="wp-block-list"><li>Programming language for automation (e.g., <a rel="noreferrer noopener" href="https://blog.finxter.com/python-crash-course/" data-type="post" data-id="3951" target="_blank">Python</a>)</li><li>Automation tool or library (e.g., <a rel="noreferrer noopener" href="https://blog.finxter.com/top-best-selenium-cheat-sheets/" data-type="post" data-id="21802" target="_blank">Selenium</a>)</li><li>Understand general testing methodology and process</li><li>Understand software performance</li><li>Understand software scalability</li><li>Life-long learning</li><li>Bachelor&#8217;s degree in computer science or related field</li><li>Master&#8217;s degree in computer science or related field</li><li>Internship and/or work experience in a software engineering company</li><li>Online course certificates (e.g., <a rel="noreferrer noopener" href="https://academy.finxter.com/" data-type="URL" data-id="https://academy.finxter.com/" target="_blank">Finxter Computer Science Academy</a>)</li><li>Understanding scripting and automation (e.g., <a rel="noreferrer noopener" href="https://blog.finxter.com/powershell-operators/" data-type="post" data-id="79052" target="_blank">Powershell</a>)</li><li>Familiarity with <a rel="noreferrer noopener" href="https://blog.finxter.com/agile-software-development-scrum/" data-type="post" data-id="246769" target="_blank">agile software development</a></li><li>Familiarity with <a rel="noreferrer noopener" href="https://blog.finxter.com/devops-specialist-income-and-opportunity/" data-type="post" data-id="310928" target="_blank">DevOps engineering</a></li></ol>



<p class="wp-block-paragraph">Soft skills such as time management and communication are important too!</p>



<h2 class="wp-block-heading" id="related-video">Related Video</h2>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="[Evan] $300 per Day Niche Idea: Website Testing Freelancer" width="937" height="527" src="https://www.youtube.com/embed/ppuHNlyQQKM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">You can find more job descriptions for coders, programmers, and computer scientists in our detailed overview guide:</p>



<ul class="wp-block-list"><li><a href="https://blog.finxter.com/get-your-job-in-it-overview/" data-type="post" data-id="186255" target="_blank" rel="noreferrer noopener">Get Your Job in IT [Overview]</a></li></ul>



<h2 class="wp-block-heading" id="related-income-of-professional-developers">Related Income of Professional Developers</h2>



<p class="wp-block-paragraph">The following statistic shows the self-reported income from 9,649 US-based professional developers (<a rel="noreferrer noopener" href="https://insights.stackoverflow.com/survey/2021#salary-comp-total-usa" data-type="URL" data-id="https://insights.stackoverflow.com/survey/2021#salary-comp-total-usa" target="_blank">source</a>).</p>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The average annual income of professional developers in the US is between <strong>$70,000</strong> and <strong>$177,500</strong> for various programming languages.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="641" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-48-641x1024.png" alt="" class="wp-image-163242" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-48-641x1024.png 641w, https://blog.finxter.com/wp-content/uploads/2022/01/image-48-188x300.png 188w, https://blog.finxter.com/wp-content/uploads/2022/01/image-48-768x1227.png 768w, https://blog.finxter.com/wp-content/uploads/2022/01/image-48.png 876w" sizes="auto, (max-width: 641px) 100vw, 641px" /></figure></div>



<p class="wp-block-paragraph"><strong>Question</strong>: <em>What is your current total compensation (salary, bonuses, and perks, before taxes and deductions)? Please enter a whole number in the box below, without any punctuation. If you are paid hourly, please estimate an equivalent weekly, monthly, or yearly salary.</em> (<a href="https://insights.stackoverflow.com/survey/2021#most-loved-dreaded-and-wanted-language-love-dread" data-type="URL" data-id="https://insights.stackoverflow.com/survey/2021#most-loved-dreaded-and-wanted-language-love-dread" target="_blank" rel="noreferrer noopener">source</a>)</p>



<p class="wp-block-paragraph">The following statistic compares the self-reported income from 46,693 professional programmers as conducted by StackOverflow.</p>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The average annual income of professional developers worldwide (US and non-US) is between <strong>$33,000</strong> and <strong>$95,000</strong> for various programming languages. </p>



<p class="wp-block-paragraph">Here&#8217;s a screenshot of a more detailed overview of each programming language considered in the report:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="350" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-41-350x1024.png" alt="" class="wp-image-163206" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-41-350x1024.png 350w, https://blog.finxter.com/wp-content/uploads/2022/01/image-41-103x300.png 103w, https://blog.finxter.com/wp-content/uploads/2022/01/image-41-525x1536.png 525w, https://blog.finxter.com/wp-content/uploads/2022/01/image-41-701x2048.png 701w, https://blog.finxter.com/wp-content/uploads/2022/01/image-41.png 745w" sizes="auto, (max-width: 350px) 100vw, 350px" /></figure>



<p class="wp-block-paragraph">Here&#8217;s what different database professionals earn:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="893" height="878" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-42.png" alt="" class="wp-image-163220" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-42.png 893w, https://blog.finxter.com/wp-content/uploads/2022/01/image-42-300x295.png 300w, https://blog.finxter.com/wp-content/uploads/2022/01/image-42-768x755.png 768w" sizes="auto, (max-width: 893px) 100vw, 893px" /></figure></div>



<p class="wp-block-paragraph">Here&#8217;s an overview of different cloud solutions experts:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="848" height="433" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-43.png" alt="" class="wp-image-163225" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-43.png 848w, https://blog.finxter.com/wp-content/uploads/2022/01/image-43-300x153.png 300w, https://blog.finxter.com/wp-content/uploads/2022/01/image-43-768x392.png 768w" sizes="auto, (max-width: 848px) 100vw, 848px" /></figure></div>



<p class="wp-block-paragraph">Here&#8217;s what professionals in web frameworks earn:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="740" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-44-740x1024.png" alt="" class="wp-image-163226" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-44-740x1024.png 740w, https://blog.finxter.com/wp-content/uploads/2022/01/image-44-217x300.png 217w, https://blog.finxter.com/wp-content/uploads/2022/01/image-44.png 755w" sizes="auto, (max-width: 740px) 100vw, 740px" /></figure></div>



<p class="wp-block-paragraph">There are many other interesting frameworks&#8212;that pay well!</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="816" height="757" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-45.png" alt="" class="wp-image-163229" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-45.png 816w, https://blog.finxter.com/wp-content/uploads/2022/01/image-45-300x278.png 300w, https://blog.finxter.com/wp-content/uploads/2022/01/image-45-768x712.png 768w" sizes="auto, (max-width: 816px) 100vw, 816px" /></figure></div>



<p class="wp-block-paragraph">Look at those tools:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="793" height="825" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-46.png" alt="" class="wp-image-163230" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-46.png 793w, https://blog.finxter.com/wp-content/uploads/2022/01/image-46-288x300.png 288w, https://blog.finxter.com/wp-content/uploads/2022/01/image-46-768x799.png 768w" sizes="auto, (max-width: 793px) 100vw, 793px" /></figure>



<p class="wp-block-paragraph">Okay, but what do you need to do to get there? What are the skill requirements and qualifications to make you become a professional developer in the area you desire?</p>



<p class="wp-block-paragraph">Let&#8217;s find out next!</p>



<h2 class="wp-block-heading" id="general-qualifications-of-professionals">General Qualifications of Professionals</h2>



<p class="wp-block-paragraph">StackOverflow performs an annual survey asking professionals, coders, developers, researchers, and engineers various questions about their background and job satisfaction on their website.</p>



<p class="wp-block-paragraph">Interestingly, when aggregating the data of the developers&#8217; educational background, a good three quarters have an academic background.</p>



<p class="wp-block-paragraph">Here&#8217;s the question asked by StackOverflow (<a href="https://insights.stackoverflow.com/survey/2021#education-ed-level-prof" data-type="URL" data-id="https://insights.stackoverflow.com/survey/2021#education-ed-level-prof" target="_blank" rel="noreferrer noopener">source</a>): </p>



<p class="wp-block-paragraph"><strong>Which of the following best describes the highest level of formal education that you’ve completed?</strong></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="589" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-37-1024x589.png" alt="" class="wp-image-163017" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-37-1024x589.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/01/image-37-300x173.png 300w, https://blog.finxter.com/wp-content/uploads/2022/01/image-37-768x442.png 768w, https://blog.finxter.com/wp-content/uploads/2022/01/image-37.png 1057w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">However, if you don&#8217;t have a formal degree, don&#8217;t fear! Many of the respondents with degrees don&#8217;t have a degree in their field&#8212;so it may not be of much value for their coding careers anyways. </p>



<p class="wp-block-paragraph">Also, about one out of four don&#8217;t have a formal degree and still succeeds in their field! You certainly don&#8217;t need a degree if you&#8217;re committed to your own success!</p>



<h2 class="wp-block-heading" id="freelancing-vs-employment-status">Freelancing vs Employment Status</h2>



<p class="wp-block-paragraph">The percentage of <a href="https://blog.finxter.com/freelance-developer/" data-type="post" data-id="5912" target="_blank" rel="noreferrer noopener">freelance developers</a> increases steadily. The fraction of freelance developers has already reached 11.21%! </p>



<p class="wp-block-paragraph">This indicates that more and more work will be done in a more flexible work environment&#8212;and fewer and fewer companies and clients want to hire inflexible talent. </p>



<p class="wp-block-paragraph">Here are the stats from the StackOverflow developer survey (<a rel="noreferrer noopener" href="https://insights.stackoverflow.com/survey/2021#employment-employment-prof" data-type="URL" data-id="https://insights.stackoverflow.com/survey/2021#employment-employment-prof" target="_blank">source</a>):</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="780" height="538" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-47.png" alt="" class="wp-image-163234" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-47.png 780w, https://blog.finxter.com/wp-content/uploads/2022/01/image-47-300x207.png 300w, https://blog.finxter.com/wp-content/uploads/2022/01/image-47-768x530.png 768w" sizes="auto, (max-width: 780px) 100vw, 780px" /></figure></div>



<p class="wp-block-paragraph">Do you want to become a professional freelance developer and earn some money on the side or as your primary source of income?</p>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><strong>Resource</strong>: Check out our <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="URL" data-id="https://blog.finxter.com/become-python-freelancer-course/" target="_blank">freelance developer course</a>&#8212;it&#8217;s the best freelance developer course in the world with the highest student success rate in the industry!</p>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button aligncenter has-custom-width wp-block-button__width-100"><a class="wp-block-button__link" href="https://blog.finxter.com/become-python-freelancer-course/" target="_blank" rel="noreferrer noopener">Become Freelance Developer (Course)</a></div>
</div>



<h2 class="wp-block-heading" id="other-programming-languages-used-by-professional-developers">Other Programming Languages Used by Professional Developers</h2>



<p class="wp-block-paragraph">The StackOverflow developer survey collected 58000 responses about the following question (<a href="https://insights.stackoverflow.com/survey/2021#most-popular-technologies-language-prof" data-type="URL" data-id="https://insights.stackoverflow.com/survey/2021#most-popular-technologies-language-prof" target="_blank" rel="noreferrer noopener">source</a>):</p>



<p class="wp-block-paragraph"><em><strong>Which programming, scripting, and markup languages have you done extensive development work in over the past year, and which do you want to work in over the next year?</strong></em></p>



<p class="wp-block-paragraph">These are the languages you want to focus on when starting out as a coder:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="372" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-38-372x1024.png" alt="" class="wp-image-163026" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-38-372x1024.png 372w, https://blog.finxter.com/wp-content/uploads/2022/01/image-38-109x300.png 109w, https://blog.finxter.com/wp-content/uploads/2022/01/image-38-768x2114.png 768w, https://blog.finxter.com/wp-content/uploads/2022/01/image-38-558x1536.png 558w, https://blog.finxter.com/wp-content/uploads/2022/01/image-38-744x2048.png 744w, https://blog.finxter.com/wp-content/uploads/2022/01/image-38.png 780w" sizes="auto, (max-width: 372px) 100vw, 372px" /></figure>



<p class="wp-block-paragraph">And don&#8217;t worry&#8212;if you feel stuck or struggle with a nasty bug. We all go through it. Here&#8217;s what SO survey respondents and professional developers do when they&#8217;re stuck:</p>



<p class="wp-block-paragraph"><strong>What do you do when you get stuck on a problem? Select all that apply. </strong>(<a href="https://insights.stackoverflow.com/survey/2021#most-loved-dreaded-and-wanted-language-love-dread" data-type="URL" data-id="https://insights.stackoverflow.com/survey/2021#most-loved-dreaded-and-wanted-language-love-dread" target="_blank" rel="noreferrer noopener">source</a>)</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="846" height="684" src="https://blog.finxter.com/wp-content/uploads/2022/01/image-39.png" alt="" class="wp-image-163044" srcset="https://blog.finxter.com/wp-content/uploads/2022/01/image-39.png 846w, https://blog.finxter.com/wp-content/uploads/2022/01/image-39-300x243.png 300w, https://blog.finxter.com/wp-content/uploads/2022/01/image-39-768x621.png 768w" sizes="auto, (max-width: 846px) 100vw, 846px" /></figure>



<h2 class="wp-block-heading" id="related-tutorials">Related Tutorials</h2>



<p class="wp-block-paragraph">To get started with some of the fundamentals and industry concepts, feel free to check out these articles:</p>



<ul class="wp-block-list"><li><a rel="noreferrer noopener" href="https://blog.finxter.com/freelance-developer/" data-type="URL" data-id="https://blog.finxter.com/freelance-developer/" target="_blank">Freelance Developer – How to Code From Home and Earn Six Figures [Industry Report]</a></li><li><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-earn-1000-on-the-side-as-a-python-freelancer-a-step-by-step-tutorial/" data-type="URL" data-id="https://blog.finxter.com/how-to-earn-1000-on-the-side-as-a-python-freelancer-a-step-by-step-tutorial/" target="_blank">How to Become a Python Freelancer—and Earn $1,000 on the Side? [A Step-by-Step Tutorial]</a></li><li><a rel="noreferrer noopener" href="https://blog.finxter.com/how-adam-earns-5000-per-month-as-a-python-freelancer-on-upwork-month-4/" data-type="URL" data-id="https://blog.finxter.com/how-adam-earns-5000-per-month-as-a-python-freelancer-on-upwork-month-4/" target="_blank">How Adam Earns $5000 per Month as a Python Freelancer on Upwork [Month 4]</a></li><li><a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Test Automation" data-type="URL" target="_blank">Test Automation Wikipedia</a></li><li><a rel="noreferrer noopener" href="https://www.google.com/search?q=Test Automation" data-type="URL" data-id="https://www.google.com/search?q=learn+Test Automation " target="_blank">Learn Test Automation Google</a></li></ul>



<h2 class="wp-block-heading">Where to Go From Here?</h2>



<p class="wp-block-paragraph">Enough theory. Let’s get some practice!</p>



<p class="wp-block-paragraph">Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. </p>



<p class="wp-block-paragraph">To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>



<p class="wp-block-paragraph"><strong>You build high-value coding skills by working on practical coding projects!</strong></p>



<p class="wp-block-paragraph">Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?</p>



<p class="has-global-color-8-background-color has-background wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If your answer is <strong><em>YES!</em></strong>, consider becoming a <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank">Python freelance developer</a>! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>



<p class="wp-block-paragraph">If you just want to learn about the freelancing opportunity, feel free to watch my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and learn how I grew my coding business online and how you can, too—from the comfort of your own home.</p>



<p class="wp-block-paragraph"><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>



<h2 class="wp-block-heading">References</h2>



<p class="wp-block-paragraph">[1] The figure was generated using the following code snippet: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import matplotlib.pyplot as plt
import numpy as np
import math

data = [89025,
        98965,
        105407,
        95314,
        74966,
        120000,
        92000,
        91589,
        110772,
        74821]

labels = ['Glassdoor.com',
          'ZipRecruiter.com',
          'Talent.com',
          'Indeed.com',
          'PayScale.com',
          'Hired.com',
          'Comparably.com',
          'Zippia.com',
          'BuiltIn.com',
          'Salary.com']

median = np.median(data)
average = np.average(data)
print(median, average)
n = len(data)

plt.plot(range(n), [median] * n, color='black', label='Median: $' + str(int(median)))
plt.plot(range(n), [average] * n, '--', color='red', label='Average: $' + str(int(average)))
plt.bar(range(len(data)), data)
plt.xticks(range(len(data)), labels, rotation='vertical', position = (0,0.45), color='white', weight='bold')
plt.ylabel('Average Income ($)')
plt.title('Test Automation Engineer Annual Income - by Finxter')
plt.legend()
plt.show()
</pre>



<hr class="wp-block-separator has-css-opacity"/>



<h2 class="wp-block-heading">The Art of Clean Code</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://www.amazon.com/Art-Clean-Code-Practices-Complexity/dp/1718502184" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="325" height="427" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-269.png" alt="" class="wp-image-385474" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-269.png 325w, https://blog.finxter.com/wp-content/uploads/2022/05/image-269-228x300.png 228w" sizes="auto, (max-width: 325px) 100vw, 325px" /></a></figure>
</div>


<p class="wp-block-paragraph">Most software developers waste thousands of hours working with overly complex code. The eight core principles in The Art of Clean Coding will teach you how to write clear, maintainable code without compromising functionality. The book’s guiding principle is simplicity: reduce and simplify, then reinvest energy in the important parts to save you countless hours and ease the often onerous task of code maintenance.</p>



<ol class="wp-block-list">
<li>Concentrate on the important stuff with the <strong>80/20 principle</strong> &#8212; focus on the 20% of your code that matters most</li>



<li>Avoid coding in isolation: create a <strong>minimum viable product</strong> to get early feedback</li>



<li>Write code cleanly and simply to <strong>eliminate clutter</strong>&nbsp;</li>



<li><strong>Avoid premature optimization</strong> that risks over-complicating code&nbsp;</li>



<li>Balance your goals, capacity, and feedback to achieve the productive state of <strong>Flow</strong></li>



<li>Apply the <strong>Do One Thing Well</strong> philosophy to vastly improve functionality</li>



<li>Design efficient user interfaces with the <strong>Less is More</strong> principle</li>



<li>Tie your new skills together into one unifying principle: <strong>Focus</strong></li>
</ol>



<p class="wp-block-paragraph">The Python-based <em><strong><a rel="noreferrer noopener" href="https://www.amazon.com/Art-Clean-Code-Practices-Complexity/dp/1718502184" data-type="URL" data-id="https://www.amazon.com/Art-Clean-Code-Practices-Complexity/dp/1718502184" target="_blank">The Art of Clean Coding</a></strong></em> is suitable for programmers at any level, with ideas presented in a language-agnostic manner.</p>



<div class="wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-fe48e5de wp-block-buttons-is-layout-flex">
<div class="wp-block-button has-custom-width wp-block-button__width-50"><a class="wp-block-button__link wp-element-button" href="https://www.amazon.com/Art-Clean-Code-Practices-Complexity/dp/1718502184" target="_blank" rel="noreferrer noopener">Get on Amazon!</a></div>
</div>



<hr class="wp-block-separator has-css-opacity"/>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Use Mock in PyTest?</title>
		<link>https://blog.finxter.com/how-to-use-mock-in-pytest/</link>
		
		<dc:creator><![CDATA[Mikio Oba]]></dc:creator>
		<pubDate>Tue, 12 Oct 2021 12:04:06 +0000</pubDate>
				<category><![CDATA[PyTest]]></category>
		<category><![CDATA[Testing]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=36662</guid>

					<description><![CDATA[In unit tests, you would want to focus on the code you want to test and avoid dealing with external dependencies that your code might be using. Or, your program might have external dependencies that are not available at the time of the test. In these situations, you can use mocks in your tests. This ... <a title="How to Use Mock in PyTest?" class="read-more" href="https://blog.finxter.com/how-to-use-mock-in-pytest/" aria-label="Read more about How to Use Mock in PyTest?">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In unit tests, you would want to focus on the code you want to test and avoid dealing with <a href="https://blog.finxter.com/the-complete-python-library-guide/" target="_blank" rel="noreferrer noopener" title="The Complete Python Library Guide">external dependencies</a> that your code might be using. Or, your program might have external dependencies that are not available at the time of the test. In these situations, you can <strong>use mocks in your tests</strong>.</p>



<p class="wp-block-paragraph">This article will explain the basic functionality of the mock library in Python and how to use it with <code>pytest</code> using a simple example of an object-oriented program. As you go over the article, you can watch the accompanying video tutorial:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="How to Use Mock in PyTest?" width="937" height="527" src="https://www.youtube.com/embed/EUS1PDm3e7s?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading">What is Mock in Testing?</h2>



<p class="wp-block-paragraph">In testing, a mock is an object that replaces a dependency. When testing software programs that use external dependencies, you might want to use mocks for the following reasons:</p>



<ul class="wp-block-list"><li>You want to isolate the code from the dependencies and focus on the part to test.&nbsp;</li><li>The dependencies are not available.</li><li>The dependencies are available, but it is costly to use them in tests regarding money or time.</li></ul>



<p class="wp-block-paragraph">In these situations, it is convenient to replace the dependencies with objects that can simulate the behavior of the actual objects, and it is precisely what mocks can offer.</p>



<h2 class="wp-block-heading"><strong>How Does Mock Work in Python?</strong></h2>



<p class="wp-block-paragraph">Python has a built-in mock library called <code>unittest.mock</code>. It has a lot of functionality, but you only need to know the main functionality of the Mock object to get started.</p>



<h3 class="wp-block-heading">How does the Mock object work?</h3>



<p class="wp-block-paragraph">The mock library has a class called <code>Mock</code>. When you create a Mock object, it is a Mock object. If you call it, you get another Mock object. If you access any attributes or methods of the Mock object, it creates them dynamically and returns a new Mock object. You can configure them to return any value you like.</p>



<p class="wp-block-paragraph">Open a Python interpreter and import Mock from <code>unittest.mock</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ python
Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec  7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest.mock import Mock</pre>



<p class="wp-block-paragraph">You can create a mock instance from the Mock class, and you can see that it is an object of the Mock class.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock = Mock()
>>> type(mock)
&lt;class 'unittest.mock.Mock'>
</pre>



<p class="wp-block-paragraph">When you call the Mock object, you get another Mock object.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock()
&lt;Mock name='mock()' id='4309251120'>
</pre>



<p class="wp-block-paragraph">You can create a new attribute <code>a</code> on the fly. It returns a new Mock object by default.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock.a
&lt;Mock name='mock.a' id='4309251312'>
>>> type(mock.a)
&lt;class 'unittest.mock.Mock'>
</pre>



<p class="wp-block-paragraph">You can create another new attribute called <code>b</code>, and it also returns a new Mock object.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock.b
&lt;Mock name='mock.b' id='4309147168'>
>>> type(mock.b)
&lt;class 'unittest.mock.Mock'>
</pre>



<p class="wp-block-paragraph">By default, a Mock object returns another Mock object when you call it.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> type(mock.a())
&lt;class 'unittest.mock.Mock'>
</pre>



<p class="wp-block-paragraph">But you can use <code>return_value</code> so that the <a href="https://blog.finxter.com/python-attributes/" target="_blank" rel="noreferrer noopener" title="Python Class vs Instance Attributes [Tutorial+Video]">attribute </a>acts as a method returning a specific value, for example, <code>'A'</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock.a.return_value = 'A'
>>> mock.a()
'A'
</pre>



<p class="wp-block-paragraph">Alternatively, you can use <code>side_effect</code> so that the mock calls a specific function when it is called.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> def func(n):
...     return n + 1
...
>>> mock.b.side_effect = func
>>> mock.b(1)
2
</pre>



<p class="wp-block-paragraph">You can specify an Error or an Exception in <code>side_effect</code>. For example, you can configure a mock to raise a ZeroDivisionError.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock.c.side_effect = ZeroDivisionError
>>> mock.c()
Traceback (most recent call last):
  File "&lt;stdin>", line 1, in &lt;module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1093, in __call__
    return self._mock_call(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1097, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1152, in _execute_mock_call
    raise effect
ZeroDivisionError
</pre>



<p class="wp-block-paragraph">Now you can create a mock and make it behave in whatever way you like.</p>



<h3 class="wp-block-heading">How Can You Use Mock in Tests?</h3>



<p class="wp-block-paragraph">The <code>Mock</code> class provides various assertion methods to verify the mock object’s behavior. The following example shows that the mock keeps track of the call (<code>a()</code>), and you can check it by using <code>mock_calls</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock = Mock()
>>> mock.a()
&lt;Mock name='mock.a()' id='4375069216'>
>>> mock.mock_calls
[call.a()]
</pre>



<p class="wp-block-paragraph">You can verify whether a specific mock is called or not by using <code>assert_called()</code>, which returns <code>None</code> when the assertion is successful. For example, if you call the method <code>a()</code> of the mock object mock, the assertion <code>mock</code>.a.assert_called() returns <code>None</code> (= pass).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock.a()
&lt;Mock name='mock.a()' id='4315854064'>
>>> mock.a.assert_called()
</pre>



<p class="wp-block-paragraph">If <code>a()</code> has not been called, the assertion raises <code>AssertionError</code> below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock = Mock()
>>> mock.a.assert_called()
Traceback (most recent call last):
  File "&lt;stdin>", line 1, in &lt;module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 876, in assert_called
    raise AssertionError(msg)
AssertionError: Expected 'a' to have been called.
</pre>



<p class="wp-block-paragraph">There are several variations, such as <code>assert_called_once()</code>, <code>assert_called_with(*args, **kwargs)</code>, <code>assert_not_called()</code>, etc. to check more specific assert conditions.</p>



<p class="wp-block-paragraph">You can also check the order of calls. The attribute <code>mock_calls</code> returns all the calls, and <code>assert_has_calls()</code> checks whether the specified (expected) calls match the actual calls or not. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> from unittest.mock import Mock, call
>>> mock = Mock()
>>> mock.a()
&lt;Mock name='mock.a()' id='4346518544'>
>>> mock.b()
&lt;Mock name='mock.b()' id='4346663312'>
>>> mock.a()
&lt;Mock name='mock.a()' id='4346518544'>
>>> mock.mock_calls
[call.a(), call.b(), call.a()]
>>> mock.assert_has_calls([call.a(), call.b(), call.a()])
</pre>



<p class="wp-block-paragraph">If the actual call order is different from the expected call order, it will raise an <code>AssertionError</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> mock.assert_has_calls([call.a(), call.a(), call.b()])
Traceback (most recent call last):
  File "&lt;stdin>", line 1, in &lt;module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 944, in assert_has_calls
    raise AssertionError(
AssertionError: Calls not found.
Expected: [call.a(), call.a(), call.b()]
Actual: [call.a(), call.b(), call.a()]
</pre>



<h2 class="wp-block-heading">Example of Unit Tests Using Mock in Python</h2>



<p class="wp-block-paragraph">In this section, I will use an example in the article <a href="https://martinfowler.com/articles/mocksArentStubs.html" target="_blank" rel="noreferrer noopener" title="https://martinfowler.com/articles/mocksArentStubs.html">Mocks Aren&#8217;t Stubs</a> by Martin Fowler. </p>



<p class="wp-block-paragraph">We have two classes, <code>Warehouse</code> and <code>Order</code>. The <code>Warehouse</code> object holds inventories of various products. When we get an <code>Order</code> object, we fill it from a <code>Warehouse</code> object. If the <code>Warehouse</code> object has enough product, the inventory is reduced, and the order is filled. If there is not enough product, the order is not filled. </p>



<p class="wp-block-paragraph">Here, let’s assume that we already know the specification of the <code>Warehouse</code> class. So, we can implement the <code>Order</code> class based on the specification as shown below. </p>



<p class="wp-block-paragraph"><code><strong>order.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Order:

    def __init__(self, product, quantity):
        self._product = product
        self._quantity = quantity
        self._filled = False

    def fill(self, warehouse):
        has_inventry = warehouse.has_inventory(
            self._product, 
            self._quantity
        )
        if has_inventry:
            warehouse.remove(self._product, self._quantity)
            self._filled = True

    def is_filled(self):
        return self._filled
</pre>



<p class="wp-block-paragraph">The <code>Order</code> class initializer takes two arguments, product and quantity, which are set to the attributes <code>self._product</code> and <code>self._quantity</code>, respectively. </p>



<p class="wp-block-paragraph">The class has an instance method called <code>fill()</code>, which takes a warehouse object. According to the (imaginary) spec, we need to invoke its method <code>has_inventory()</code> with the arguments <code>self._product</code> and <code>self._quantity</code> to check the inventory. </p>



<p class="wp-block-paragraph">The method <code>has_inventory()</code> returns <code>True</code> or <code>False</code>, depending on the inventory status. If it has a sufficient amount of the product, it returns <code>True</code>, so the <code>fill()</code> method can call <code>remove()</code> to update the inventory. Then, we can set the attribute <code>self._filled</code> to <code>True</code>, indicating that the order has been filled.</p>



<p class="wp-block-paragraph">If the inventory does not have enough quantity of the product, <code>has_inventory()</code> returns <code>False</code>, so the <code>self._filled</code> value stays <code>False</code>, indicating that the order is not filled. </p>



<p class="wp-block-paragraph">The instance method <code>is_filled()</code> returns the current value of the <code>self._filled</code> attribute.</p>



<p class="wp-block-paragraph">Now let’s look at how we can run unit tests for this code. We will mock the <code>Warehouse</code> object as we only focus on the <code>Order</code> class. </p>



<h3 class="wp-block-heading">How to use Mock in Pytest</h3>



<p class="wp-block-paragraph">As shown below, we can write the first test case to check that the order is filled when the warehouse has enough product.&nbsp;</p>



<p class="wp-block-paragraph"><code><strong>test_order.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from unittest.mock import Mock
from order import Order

def test_order_is_filled():
    
    # setup - data
    order = Order('Talisker', 50)
    warehouse = Mock()

    # setup - expectations
    warehouse.has_inventory.return_value = True
    warehouse.remove.return_value = None

    # exercise
    order.fill(warehouse)
  
    # verify
    assert order.is_filled()
</pre>



<p class="wp-block-paragraph">Firstly, we need to import the <code>Mock</code> class from <code>unittest.mock</code>. We also need to import the <code>Order</code> class to test.</p>



<p class="wp-block-paragraph">The first part of the test is to set up the test data. We can create an order of Talisker with the quantity 50. Then, we need a <code>Warehouse</code> object. As we are not interested in testing the <code>Warehouse</code> class (or we might not have access to the <code>Warehouse</code> class in the test environment), we mock a warehouse object by using <code>Mock</code> to simulate the behavior of a <code>Warehouse</code> object. It means that warehouse is not an actual Warehouse object but behaves like a Warehouse object, and we can control how it behaves.</p>



<p class="wp-block-paragraph">In the second part, we set the expectations. As we saw earlier, we can create any methods dynamically in a Mock object, so we can create the <code>has_inventory()</code> method and set it to return <code>True</code>, simulating the inventory has enough Talisker. We then create the <code>remove()</code> method, which returns <code>None</code>. </p>



<p class="wp-block-paragraph">In the third part, the order object invokes the <code>fill()</code> method with the warehouse object as an argument. The order object does not know that warehouse is a Mock object, so it simply runs the method as if it were a real warehouse object. </p>



<p class="wp-block-paragraph">Finally, we check if the order has been filled or not. In this case, <code>is_filled()</code> should return <code>True</code> because <code>has_inventory()</code> returns <code>True</code>.</p>



<p class="wp-block-paragraph">You can check that the test passes by running pytest.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_order.py -v
==================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest4/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest4
collected 1 item                                            

test_order.py::test_order_is_filled PASSED            [100%]

===================== 1 passed in 0.04s =====================     
</pre>



<p class="wp-block-paragraph">We can also add another test case where the warehouse does not have enough inventories.</p>



<p class="wp-block-paragraph"><code><strong>test_order.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from unittest.mock import Mock
from order import Order

def test_order_is_filled():
    
    # setup - data
    order = Order('Talisker', 50)
    warehouse = Mock()

    # setup - expectations
    warehouse.has_inventory.return_value = True
    warehouse.remove.return_value = None

    # exercise
    order.fill(warehouse)
  
    # verify
    assert order.is_filled()

def test_order_is_not_filled():
    
    # setup - data
    order = Order('Talisker', 50)
    warehouse = Mock()
  
    # setup - expectations
    warehouse.has_inventory.return_value = False
  
    # exercise
    order.fill(warehouse)

    # verify
    assert not order.is_filled()
</pre>



<p class="wp-block-paragraph">In the second test case, the <code>has_inventory()</code> method of the mocked warehouse object returns <code>False</code>, which simulates the inventory has insufficient Talisker. So, the order is not filled, and the <code>is_filled()</code> method should return <code>False</code>. You can check that the second test also passes.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_order.py -v 
==================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest4/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest4
collected 2 items                                           

test_order.py::test_order_is_filled PASSED            [ 50%]
test_order.py::test_order_is_not_filled PASSED        [100%]

===================== 2 passed in 0.03s ===================== 
</pre>



<p class="wp-block-paragraph">If the example above looks confusing at first glance, you can try changing the return value and see what happens. For example, if you change the return value of <code>has_inventory</code> to <code>True</code> in the second test, the test will fail.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_order.py -v
==================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest4/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest4
collected 2 items                                           

test_order.py::test_order_is_filled PASSED            [ 50%]
test_order.py::test_order_is_not_filled FAILED        [100%]

========================= FAILURES ==========================
_________________ test_order_is_not_filled __________________

    def test_order_is_not_filled():
    
        # setup - data
        order = Order('Talisker', 50)
        warehouse = Mock()
    
        # setup - expectations
        warehouse.has_inventory.return_value = True
    
        # exercise
        order.fill(warehouse)
    
        # verify
>       assert not order.is_filled()
E       assert not True
E        +  where True = &lt;bound method Order.is_filled of &lt;order.Order object at 0x10b2481c0>>()
E        +    where &lt;bound method Order.is_filled of &lt;order.Order object at 0x10b2481c0>> = &lt;order.Order object at 0x10b2481c0>.is_filled

test_order.py:33: AssertionError
================== short test summary info ==================
FAILED test_order.py::test_order_is_not_filled - assert no...
================ 1 failed, 1 passed in 0.05s ================
</pre>



<p class="wp-block-paragraph">You can see that the <code>is_filled()</code> method returns <code>True</code>. It is because <code>has_inventory</code> returns <code>True</code>, meaning that the inventory has enough Talisker. Hopefully, you can see that you can fully control the behaviour of the dependency (= warehouse) because we are using Mock instead of the real Warehouse object.</p>



<h3 class="wp-block-heading">How to assert method calls using Mock in pytest </h3>



<p class="wp-block-paragraph">As we saw earlier, the Mock class has various assertion methods about method calls. So, we can check what methods the order object internally calls.</p>



<p class="wp-block-paragraph">We can add two additional assert statements at the end of each test, as shown below.</p>



<p class="wp-block-paragraph"><code><strong>test_order.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from unittest.mock import Mock, call
from order import Order


def test_order_is_filled():
    
    # setup - data
    order = Order('Talisker', 50)
    warehouse = Mock()

    # setup - expectations
    warehouse.has_inventory.return_value = True
    warehouse.remove.return_value = None

    # exercise
    order.fill(warehouse)
  
    # verify
    assert order.is_filled()
    warehouse.has_inventory.assert_called_with('Talisker', 50)
    warehouse.assert_has_calls(
        [
            call.has_inventory('Talisker', 50), 
            call.remove('Talisker', 50)
        ]
    )
    
    
def test_order_is_not_filled():
    
    # setup - data
    order = Order('Talisker', 50)
    warehouse = Mock()
  
    # setup - expectations
    warehouse.has_inventory.return_value = False
  
    # exercise
    order.fill(warehouse)

    # verify
    assert not order.is_filled()
    warehouse.has_inventory.assert_called_with('Talisker', 50)
    warehouse.remove.assert_not_called()
</pre>



<p class="wp-block-paragraph">In the first test, we can add the two assertions:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">warehouse.has_inventory.assert_called_with('Talisker', 50)
warehouse.assert_has_calls(
    [
        call.has_inventory('Talisker', 50), 
        call.remove('Talisker', 50)
    ]
)
</pre>



<p class="wp-block-paragraph">The first <code>assert_called_with()</code> method checks that the warehouse object has called the method <code>has_inventory()</code> with the arguments <code>'Talisker'</code>, 50.</p>



<p class="wp-block-paragraph">The second <code>assert_has_calls()</code> method verifies that the warehouse object has called the two methods (<code>has_inventory('Talisker', 50)</code> and <code>remove('Talisker', 50)</code>) in this order.</p>



<p class="wp-block-paragraph">We can also add similar assertions to the second test.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">warehouse.has_inventory.assert_called_with('Talisker', 50)
warehouse.remove.assert_not_called()
</pre>



<p class="wp-block-paragraph">The first <code>assert_called_with()</code> is the same as the first test, but the second <code>assert_not_called()</code> checks that the warehouse object has not called the <code>remove()</code> method. It is important to have this assertion because we cannot check it just by looking at the output of the <code>is_filled()</code> method.</p>



<p class="wp-block-paragraph">We can confirm that the tests still pass.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_order.py -v
==================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest4/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest4
collected 2 items                                           

test_order.py::test_order_is_filled PASSED            [ 50%]
test_order.py::test_order_is_not_filled PASSED        [100%]

===================== 2 passed in 0.05s =====================
</pre>



<h2 class="wp-block-heading"><strong>Considerations</strong></h2>



<p class="wp-block-paragraph">Mocking is a powerful tool in testing, but there are some considerations.</p>



<p class="wp-block-paragraph">Firstly, mocks require maintenance. As we create mocks ourselves based on a specification, any changes to the dependencies will also need to be implemented in the mocks. Otherwise, all unit tests might pass, but the entire software program will not function. If you strictly follow the <a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle">Open–closed principle</a>, this might not be a big issue, but it would depend on how you develop software.</p>



<p class="wp-block-paragraph">It leads to the second point. Mocks work better in some software design styles than others. Martin Fowler highlights the differences between testers who use mocks (mockist) and more classical style testers (classicists) in the article <a href="https://martinfowler.com/articles/mocksArentStubs.html" target="_blank" rel="noreferrer noopener">Mocks Aren&#8217;t Stubs</a> as mentioned above, saying that one of the differences is that mockist prefers the &#8220;outside-in approach”:</p>



<p class="wp-block-paragraph"><em>Once you have your first test running, the expectations on the mocks provide a specification for the next step and a starting point for the tests. You turn each expectation into a test on a collaborator and repeat the process working your way into the system one SUT at a time. This style is also referred to as outside-in, which is a very descriptive name for it. It works well with layered systems. You first start by programming the UI using mock layers underneath. Then you write tests for the lower layer, gradually stepping through the system one layer at a time. This is a very structured and controlled approach, one that many people believe is helpful to guide newcomers to OO and TDD.</em></p>



<p class="wp-block-paragraph">In the above example, the expectations in test_order.py would become a specification of the Warehouse class so that we could create tests based on this specification, and so on. You can read the article to get more insights into unit testing, mocking and test-driven development in general</p>



<h2 class="wp-block-heading"><strong>Summary&nbsp;</strong></h2>



<p class="wp-block-paragraph">In this article, I’ve explained how mocks work in Python by looking at the basic functionality of Mock in the <code>unittest.mock</code> library. It is a handy tool to replace an external dependency and set up tests. Then we looked at how we can use it with pytest by using an example of an object-oriented program.</p>



<p class="wp-block-paragraph">I hope this article has helped you understand how mocks work and how you can use them with pytest to run tests more effectively.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Use Pytest Fixtures</title>
		<link>https://blog.finxter.com/how-to-use-pytest-fixtures/</link>
		
		<dc:creator><![CDATA[Mikio Oba]]></dc:creator>
		<pubDate>Mon, 04 Oct 2021 08:08:58 +0000</pubDate>
				<category><![CDATA[PyTest]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=36193</guid>

					<description><![CDATA[In this article, you&#8217;ll deep dive into a powerful testing feature in Python called Pytest Fixtures. Feel free to dive into our background articles on Pytest in case you need a quick refresher (with video)! Pytest – A Complete Overview Pytest – How to Run Tests Efficiently You can watch this tutorial in video format ... <a title="How to Use Pytest Fixtures" class="read-more" href="https://blog.finxter.com/how-to-use-pytest-fixtures/" aria-label="Read more about How to Use Pytest Fixtures">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In this article, you&#8217;ll deep dive into a powerful testing feature in Python called <em><strong>Pytest Fixtures</strong></em>. Feel free to dive into our background articles on Pytest in case you need a quick refresher (with video)!</p>



<ol class="wp-block-list"><li><a href="https://blog.finxter.com/pytest-a-complete-overview/" target="_blank" rel="noreferrer noopener">Pytest – A Complete Overview</a></li><li><a href="https://blog.finxter.com/how-to-run-tests-efficiently-in-pytest/" target="_blank" rel="noreferrer noopener">Pytest – How to Run Tests Efficiently<abbr title="3.000000 is the YARPP match score between the current entry and this related entry. You are seeing this value because you are logged in to WordPress as an administrator. It is not shown to regular visitors."></a></li></ol>



<p class="wp-block-paragraph">You can watch this tutorial in video format or just read the article with code. Here&#8217;s the video first:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="How to Use Pytest Fixtures" width="937" height="527" src="https://www.youtube.com/embed/WWSU5MMiWNM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">What are Pytest Fixtures?</h2>



<p class="wp-block-paragraph">Pytest fixtures are functions that you can use to initialise your test environment. They can provide consistent test data or set up the initial state of the environment. </p>



<p class="wp-block-paragraph">Software testing often requires specific dependencies, such as input data or external resources. It can be tedious and inefficient if each test case needs to create dependencies on its own. pytest fixtures can provide a solution for the problem.</p>



<p class="wp-block-paragraph">In pytest, you can create a function that provides the dependency and mark it as a fixture. Then, test cases can use it when you specify the function name as an argument. Fixtures are modular by design and are easy to share across test cases in different files. In addition, it is easy to add steps to clean up the resources. Let&#8217;s look at the details.</p>



<h2 class="wp-block-heading">How to Use a Pytest Fixture?</h2>



<h3 class="wp-block-heading">Test Case Without a Fixture</h3>



<p class="wp-block-paragraph">Let’s use a simple example of a <a href="https://blog.finxter.com/an-introduction-to-python-classes-inheritance-encapsulation-and-polymorphism/" target="_blank" rel="noreferrer noopener" title="An Introduction To Python Classes – Inheritance, Encapsulation, and Polymorphism">Python class</a> Customer as shown below.</p>



<p class="wp-block-paragraph"><code><strong>customer.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Customer:

    def __init__(self, cust_id, level=0):
        self._cust_id = cust_id
        self._level = level

    def __str__(self):
        return f'&lt; Customer cust_id = {self._cust_id}, level = {self._level} >'

    @property 
    def cust_id(self):
        return self._cust_id

    @property
    def level(self):
        return self._level

    def level_up(self):
        self._level += 1

    def level_down(self):
        self._level -= 1 if self._level >= 1 else self._level
</pre>



<p class="wp-block-paragraph">This class has two properties, <code>cust_id</code> and <code>level</code>, which can be accessed using the getter methods <code>cust_id()</code> and <code>level()</code>, respectively. It also has two methods, <code>level_up()</code> and <code>level_down()</code>, to increase and decrease the level value by 1, respectively, but when decreasing the level, the level value should not go below 0. The initializer takes parameters for <code>cust_id</code> and <code>level</code>, and the default value of <code>level</code> is set to 0 if not specified.</p>



<p class="wp-block-paragraph">Let’s first look at an example without fixtures. We can create simple test cases to test this class, as shown below.</p>



<p class="wp-block-paragraph"><code><strong>test_customer1.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer

def test_customer_has_default_properties():
    customer = Customer(100)
    assert customer.cust_id == 100
    assert customer.level == 0

def test_customer_has_initial_level():
    customer = Customer(100, 1)
    assert customer.cust_id == 100
    assert customer.level == 1

def test_customer_level_up_increases_level():
    customer = Customer(100)
    customer.level_up()
    assert customer.level == 1

def test_customer_level_down_decreases_level():
    customer = Customer(100)
    customer.level_up()
    customer.level_down()
    assert customer.level == 0

def test_customer_level_not_goes_below_0():
    customer = Customer(100)
    customer.level_down()
    assert customer.level == 0
</pre>



<p class="wp-block-paragraph">When you run the <code>pytest</code> command, the tests should pass.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer1.py
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer1.py .....                          [100%]

================== 5 passed in 0.00s ===================
</pre>



<p class="wp-block-paragraph">But looking at the test cases, you might notice that each test case creates a new <code>Customer</code> object. In this example, we need only <a href="https://pythononeliners.com/" target="_blank" rel="noreferrer noopener" title="https://pythononeliners.com/">one line</a> to create an object, but it can be more complex, and it would be inefficient to repeat the same steps in each test case. Let&#8217;s make it more efficient by using a fixture.</p>



<h3 class="wp-block-heading"><strong>How to Create a Fixture</strong></h3>



<p class="wp-block-paragraph">You can create a fixture by defining a function with the <code>@pytest.fixture</code> <a href="https://blog.finxter.com/closures-and-decorators-in-python/" target="_blank" rel="noreferrer noopener" title="Closures and Decorators in Python">decorator </a>in the following three steps:</p>



<ol class="wp-block-list"><li>Import Pytest into the test file.</li></ol>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest</pre>



<ol class="wp-block-list" start="2"><li>Create a function with the <code>@pytest.fixture</code> decorator.</li></ol>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@pytest.fixture
def customer():
    _customer = Customer(100)
    return _customer
</pre>



<ol class="wp-block-list" start="3"><li>Specify the function name as an argument in the test functions.</li></ol>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_customer_has_default_properties(customer):
    assert customer.cust_id == 100
    assert customer.level == 0
</pre>



<p class="wp-block-paragraph">Now the entire test file looks like this:</p>



<p class="wp-block-paragraph"><code><strong>test_customer2.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture
def customer():
    _customer = Customer(100)
    return _customer

def test_customer_has_default_properties(customer):
    assert customer.cust_id == 100
    assert customer.level == 0

def test_customer_has_initial_level():
    customer = Customer(100, 1)
    assert customer.cust_id == 100
    assert customer.level == 1

def test_customer_level_up_increases_level(customer):
    customer.level_up()
    assert customer.level == 1

def test_customer_level_down_decreases_level(customer):
    customer.level_up()
    customer.level_down()
    assert customer.level == 0

def test_customer_level_not_goes_below_0(customer):
    customer.level_down()
    assert customer.level == 0
</pre>



<p class="wp-block-paragraph">The second test function (<code>test_customer_has_initial_level</code>) is not requesting (i.e. using) the fixture because the object is initialized differently. Still, we have managed to eliminate the object initialization from the other test functions.</p>



<p class="wp-block-paragraph">The tests should still pass.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer2.py
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer2.py .....                          [100%]

================== 5 passed in 0.00s ===================
</pre>



<h2 class="wp-block-heading">How to Share Fixtures? </h2>



<p class="wp-block-paragraph">In the previous section, we eliminated some repetitions in the test functions by using a fixture. But if the fixture stays in the same test file, you might not find it very useful. One of the advantages of using fixtures is that they can be shared across multiple test files in the same directory and subdirectories. All you need to do is to define them in a file called <code>conftest.py</code>.</p>



<h3 class="wp-block-heading">How to Use conftest.py</h3>



<p class="wp-block-paragraph">Let&#8217;s create the file <code>conftest.py</code> in the current <a href="https://blog.finxter.com/list-all-files-of-a-directory-python/" target="_blank" rel="noreferrer noopener" title="How Do I List All Files of a Directory in Python?">directory </a>and move the fixture from the test file. Be sure to import the class and Pytest as well.</p>



<p class="wp-block-paragraph"><code><strong>conftest.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture
def customer():
    _customer = Customer(100)
    return _customer
</pre>



<p class="wp-block-paragraph">Now the test file looks like this:</p>



<p class="wp-block-paragraph"><code><strong>test_customer3.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer

def test_customer_has_default_properties(customer):
    assert customer.cust_id == 100
    assert customer.level == 0

def test_customer_has_initial_level():
    customer = Customer(100, 1)
    assert customer.cust_id == 100
    assert customer.level == 1

def test_customer_level_up_increases_level(customer):
    customer.level_up()
    assert customer.level == 1

def test_customer_level_down_decreases_level(customer):
    customer.level_up()
    customer.level_down()
    assert customer.level == 0

def test_customer_level_not_goes_below_0(customer):
    customer.level_down()
    assert customer.level == 0
</pre>



<p class="wp-block-paragraph">When the fixture is not defined in the same file, Pytest automatically looks for <code>conftest.py</code> and finds it in the file, so the tests should still pass. If you had other test files in the same directory, the fixture would automatically become available in the tests (but we will only use a single test file in this article).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer3.py
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer3.py .....                          [100%]

================== 5 passed in 0.00s ===================
</pre>



<h3 class="wp-block-heading"><strong>How to Set a Fixture Scope</strong></h3>



<p class="wp-block-paragraph">Each fixture has a <a href="https://blog.finxter.com/understanding-namespaces-in-python/" title="Understanding Namespaces in Python" target="_blank" rel="noreferrer noopener">scope</a>. The default scope is function, meaning that fixtures are initialized when requested in a test function and destroyed when the test function finishes. It is the same behavior as our first test file, <code>test_customer1.py</code>, where each function creates a <code>Customer</code> object.</p>



<p class="wp-block-paragraph">Let&#8217;s verify the scope by looking at the object ID. Add a <a href="https://blog.finxter.com/python-print/" target="_blank" rel="noreferrer noopener" title="Python print()">print </a>statement to each test function, as shown below.</p>



<p class="wp-block-paragraph"><code><strong>test_customer4.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer

def test_customer_has_default_properties(customer):
    print(f'{id(customer)=}')
    assert customer.cust_id == 100
    assert customer.level == 0

def test_customer_has_initial_level():
    customer = Customer(100, 1)
    print(f'{id(customer)=}')
    assert customer.cust_id == 100
    assert customer.level == 1

def test_customer_level_up_increases_level(customer):
    print(f'{id(customer)=}')
    customer.level_up()
    assert customer.level == 1

def test_customer_level_down_decreases_level(customer):
    print(f'{id(customer)=}')
    customer.level_up()
    customer.level_down()
    assert customer.level == 0

def test_customer_level_not_goes_below_0(customer):
    print(f'{id(customer)=}')
    customer.level_down()
    assert customer.level == 0
</pre>



<p class="wp-block-paragraph">Then run Pytest with the <code>-rP</code> option, which shows the output of print statements in the summary info section. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer4.py -rP
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer4.py .....                          [100%]

======================== PASSES ========================
_________ test_customer_has_default_properties _________
----------------- Captured stdout call -----------------
id(customer)=4384350896
___________ test_customer_has_initial_level ____________
----------------- Captured stdout call -----------------
id(customer)=4384440480
________ test_customer_level_up_increases_level ________
----------------- Captured stdout call -----------------
id(customer)=4384440528
_______ test_customer_level_down_decreases_level _______
----------------- Captured stdout call -----------------
id(customer)=4384440624
_________ test_customer_level_not_goes_below_0 _________
----------------- Captured stdout call -----------------
id(customer)=4384440576
================== 5 passed in 0.00s ===================
</pre>



<p class="wp-block-paragraph">As you can see, the object ID is different in each test function, which means each object is different.</p>



<p class="wp-block-paragraph">You can use other scopes, such as <code>session</code>. The session scope means that fixtures are initialized when they are first requested in the test session. Then, Pytest uses the same object during the test session and destroys it when the test session ends.</p>



<p class="wp-block-paragraph">Let&#8217;s try changing the scope of our fixture to session. You can change the scope by adding the parameter scope to the <code>@pytest.fixture</code> <a href="https://blog.finxter.com/a-3-min-primer-on-python-decorators/" target="_blank" rel="noreferrer noopener" title="A 3-Min Primer on Python Decorators">decorator</a>.</p>



<p class="wp-block-paragraph"><code><strong>conftest.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture(scope='session')
def customer():
    _customer = Customer(100)
    return _customer
</pre>



<p class="wp-block-paragraph">Now, when you run the tests, the 4th test (<code>test_customer_level_down_decreases_level</code>) fails because the customer object is now shared across the test functions, as you can see from the object ID value. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer4.py -rP
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer4.py ...F.                          [100%]

======================= FAILURES =======================
_______ test_customer_level_down_decreases_level _______

customer = &lt;customer.Customer object at 0x10143a250>

    def test_customer_level_down_decreases_level(customer):
        print(f'{id(customer)=}')
        customer.level_up()
        customer.level_down()
>       assert customer.level == 0
E       assert 1 == 0
E        +  where 1 = &lt;customer.Customer object at 0x10143a250>.level

test_customer4.py:23: AssertionError
----------------- Captured stdout call -----------------
id(customer)=4316176976
======================== PASSES ========================
_________ test_customer_has_default_properties _________
----------------- Captured stdout call -----------------
id(customer)=4316176976
___________ test_customer_has_initial_level ____________
----------------- Captured stdout call -----------------
id(customer)=4316365056
________ test_customer_level_up_increases_level ________
----------------- Captured stdout call -----------------
id(customer)=4316176976
_________ test_customer_level_not_goes_below_0 _________
----------------- Captured stdout call -----------------
id(customer)=4316176976
============= 1 failed, 4 passed in 0.02s ==============
</pre>



<p class="wp-block-paragraph">When the third function (<code>test_customer_level_up_increases</code>_level) runs, the level value of the customer object is increased. So, the customer object in the 4th test function does not have the default level value. We should probably update the tests as shown below.</p>



<p class="wp-block-paragraph"><code><strong>test_customer5.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer

def test_customer_has_default_properties(customer):
    print(f'{id(customer)=}')
    assert customer.cust_id == 100
    assert customer.level == 0

def test_customer_has_initial_level():
    customer = Customer(100, 1)
    print(f'{id(customer)=}')
    assert customer.cust_id == 100
    assert customer.level == 1

def test_customer_level_up_increases_level(customer):
    print(f'{id(customer)=}')
    original_level = customer.level
    customer.level_up()
    assert customer.level == original_level + 1

def test_customer_level_down_decreases_level(customer):
    print(f'{id(customer)=}')
    original_level = customer.level
    customer.level_up()
    customer.level_down()
    assert customer.level == original_level

def test_customer_level_not_goes_below_0(customer):
    print(f'{id(customer)=}')
    original_level = customer.level
    customer.level_down()
    assert customer.level == (original_level - 1 if original_level >= 1 else 0)
</pre>



<p class="wp-block-paragraph">Now the tests should pass.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer5.py -rP
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer5.py .....                          [100%]

======================== PASSES ========================
_________ test_customer_has_default_properties _________
----------------- Captured stdout call -----------------
id(customer)=4395372800
___________ test_customer_has_initial_level ____________
----------------- Captured stdout call -----------------
id(customer)=4395373088
________ test_customer_level_up_increases_level ________
----------------- Captured stdout call -----------------
id(customer)=4395372800
_______ test_customer_level_down_decreases_level _______
----------------- Captured stdout call -----------------
id(customer)=4395372800
_________ test_customer_level_not_goes_below_0 _________
----------------- Captured stdout call -----------------
id(customer)=4395372800
================== 5 passed in 0.00s ===================
</pre>



<h2 class="wp-block-heading">How to Add Arguments to a Fixture?</h2>



<p class="wp-block-paragraph">We&#8217;ve been using a fixture, but it is a little bit limited because it only has a fixed cust_id and the default level value. It would be more useful if we could create an object using different parameter values.&nbsp;</p>



<p class="wp-block-paragraph">Instead of creating an object with fixed parameter values, you can create a fixture that returns a function to create an object so that you can specify the parameter values as arguments in the function. This style is sometimes called <strong><em><a href="https://blog.finxter.com/how-to-dynamically-create-a-function-in-python/" title="How to Dynamically Create a Function in Python?" target="_blank" rel="noreferrer noopener">Factory</a></em></strong> pattern in <a href="https://blog.finxter.com/object-oriented-programming-terminology-cheat-sheet/" target="_blank" rel="noreferrer noopener" title="[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming">object-oriented programming</a>, so let&#8217;s create a new fixture in <code>conftest.py</code> and call it <code>customer_factory</code> as shown below.</p>



<p class="wp-block-paragraph"><code><strong>conftest.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture(scope='session')
def customer():
    _customer = Customer(100)
    return _customer

@pytest.fixture(scope='session')
def customer_factory():
    def _customer(cust_id, level=0):
        _cust = Customer(cust_id, level)   
        return _cust
    return _customer
</pre>



<p class="wp-block-paragraph">In the function <code>customer_factory</code>, another inner function <code>_custoemr()</code> is defined. The inner function uses two argument values (<code>cust_id</code> and <code>level</code>) to create a <code>Customer</code> object and returns it. When this fixture is requested, the test function does not receive a <code>Customer</code> object but receives this inner function instead. So, it can create a <code>Customer</code> object with any parameter values.</p>



<p class="wp-block-paragraph">Let&#8217;s use this fixture in the test file. In the second function (<code>test_customer_has_initial_level</code>), you can specify the new fixture as an argument and use it to create a customer object. In this case, the change is minimal, but if the initialization step were more complex, it would significantly simplify the test function. We can also remove the line to import the <code>Customer</code> class because we no longer use it in the test file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_customer_has_initial_level(customer_factory):
    customer = customer_factory(100, 1)
    assert customer.cust_id == 100
    assert customer.level == 1
</pre>



<p class="wp-block-paragraph">The entire test file now looks like this:</p>



<p class="wp-block-paragraph"><code><strong>test_customer6.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_customer_has_default_properties(customer):
    print(f'{id(customer)=}')
    assert customer.cust_id == 100
    assert customer.level == 0

def test_customer_has_initial_level(customer_factory):
    customer = customer_factory(100, 1)
    print(f'{id(customer)=}')
    assert customer.cust_id == 100
    assert customer.level == 1

def test_customer_level_up_increases_level(customer):
    print(f'{id(customer)=}')
    original_level = customer.level
    customer.level_up()
    assert customer.level == original_level + 1

def test_customer_level_down_decreases_level(customer):
    print(f'{id(customer)=}')
    original_level = customer.level
    customer.level_up()
    customer.level_down()
    assert customer.level == original_level

def test_customer_level_not_goes_below_0(customer):
    print(f'{id(customer)=}')
    original_level = customer.level
    customer.level_down()
    assert customer.level == (original_level - 1 if original_level >= 1 else 0)
</pre>



<p class="wp-block-paragraph">The tests should still pass.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer6.py
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer6.py .....                          [100%]

================== 5 passed in 0.00s ===================
</pre>



<p class="wp-block-paragraph">Fixtures can use another fixture, so in this example, we can use the new fixture in the first fixture, as shown below.</p>



<p class="wp-block-paragraph"><code><strong>conftest.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture(scope='session')
def customer(customer_factory):
    _customer = customer_factory(100)
    return _customer

@pytest.fixture(scope='session')
def customer_factory():
    def _customer(cust_id, level=0):
        _cust = Customer(cust_id, level)   
        return _cust
    return _customer
</pre>



<p class="wp-block-paragraph">Note that the fixture customer uses the other fixture <code>customer_factory</code> to create a <code>Customer</code> object. The tests still pass, and you can see that the object is shared across the test functions except for the second test, which creates a separate object.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer6.py -rP
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer6.py .....                          [100%]

======================== PASSES ========================
_________ test_customer_has_default_properties _________
----------------- Captured stdout call -----------------
id(customer)=4387652800
___________ test_customer_has_initial_level ____________
----------------- Captured stdout call -----------------
id(customer)=4387653136
________ test_customer_level_up_increases_level ________
----------------- Captured stdout call -----------------
id(customer)=4387652800
_______ test_customer_level_down_decreases_level _______
----------------- Captured stdout call -----------------
id(customer)=4387652800
_________ test_customer_level_not_goes_below_0 _________
----------------- Captured stdout call -----------------
id(customer)=4387652800
================== 5 passed in 0.01s ===================
</pre>



<h2 class="wp-block-heading">How to Implement Fixture Teardown?</h2>



<p class="wp-block-paragraph">When using external resources in tests, it is essential to clean up when the tests finish (sometimes called <strong><em>teardown</em></strong>). For example, if you open a file, you should close the file descriptor. If you create a temporary file, you should delete it. If you connect to a database, you should disconnect from the database. In Pytest, it is easy to implement teardown logic when using fixtures.</p>



<p class="wp-block-paragraph">Instead of using <a href="https://blog.finxter.com/python-return/" target="_blank" rel="noreferrer noopener" title="Return Keyword in Python – A Simple Illustrated Guide">return</a>, we can use <a href="https://blog.finxter.com/yield-keyword-in-python-a-simple-illustrated-guide/" target="_blank" rel="noreferrer noopener" title="Yield Keyword in Python – A Simple Illustrated Guide">yield </a>to return the object after creating it in the fixture functions. When test functions finish using the fixture, the code after yield runs, so you write clean-up logic there.</p>



<p class="wp-block-paragraph">There is nothing to clean up in our example, but let&#8217;s add print statements for demonstration purposes. In <code>conftest.py</code>, update the function <code>customer_factory()</code> as shown below.</p>



<p class="wp-block-paragraph"><code><strong>conftest.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture(scope='session')
def customer(customer_factory):
    _customer = customer_factory(100)
    return _customer

@pytest.fixture(scope='session')
def customer_factory():
    print('Fixture setup')
    def _customer(cust_id, level=0):
        _cust = Customer(cust_id, level)   
        return _cust
    yield _customer
    print('Fixture teardown')
</pre>



<p class="wp-block-paragraph">Note that the two <a href="https://blog.finxter.com/python-print/" target="_blank" rel="noreferrer noopener" title="Python print()">print </a>statements, one before the inner function and the other at the end. Also, the <em>return </em>is changed to <em>yield </em>in the function <code>customer_factory()</code>.</p>



<p class="wp-block-paragraph">When running Pytest, we can see the messages at the session&#8217;s beginning and end.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer6.py -rP
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer6.py .....                          [100%]

======================== PASSES ========================
_________ test_customer_has_default_properties _________
---------------- Captured stdout setup -----------------
Fixture setup
----------------- Captured stdout call -----------------
id(customer)=4359226512
___________ test_customer_has_initial_level ____________
----------------- Captured stdout call -----------------
id(customer)=4359226848
________ test_customer_level_up_increases_level ________
----------------- Captured stdout call -----------------
id(customer)=4359226512
_______ test_customer_level_down_decreases_level _______
----------------- Captured stdout call -----------------
id(customer)=4359226512
_________ test_customer_level_not_goes_below_0 _________
----------------- Captured stdout call -----------------
id(customer)=4359226512
--------------- Captured stdout teardown ---------------
Fixture teardown
================== 5 passed in 0.00s ===================
</pre>



<p class="wp-block-paragraph">The setup and teardown run depending on the scope of the fixture. Let&#8217;s change the scope to function as shown below.</p>



<p class="wp-block-paragraph"><code><strong>conftest.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from customer import Customer
import pytest

@pytest.fixture(scope='function')
def customer(customer_factory):
    _customer = customer_factory(100)
    return _customer

@pytest.fixture(scope='function')
def customer_factory():
    print('Fixture setup')
    def _customer(cust_id, level=0):
        _cust = Customer(cust_id, level)   
        return _cust
    yield _customer
    print('Fixture teardown')
</pre>



<p class="wp-block-paragraph">We can see the setup and teardown messages before and after each test function runs.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_customer6.py -rP
================= test session starts ==================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest3
collected 5 items

test_customer6.py .....                          [100%]

======================== PASSES ========================
_________ test_customer_has_default_properties _________
---------------- Captured stdout setup -----------------
Fixture setup
----------------- Captured stdout call -----------------
id(customer)=4387931376
--------------- Captured stdout teardown ---------------
Fixture teardown
___________ test_customer_has_initial_level ____________
---------------- Captured stdout setup -----------------
Fixture setup
----------------- Captured stdout call -----------------
id(customer)=4387931472
--------------- Captured stdout teardown ---------------
Fixture teardown
________ test_customer_level_up_increases_level ________
---------------- Captured stdout setup -----------------
Fixture setup
----------------- Captured stdout call -----------------
id(customer)=4387931520
--------------- Captured stdout teardown ---------------
Fixture teardown
_______ test_customer_level_down_decreases_level _______
---------------- Captured stdout setup -----------------
Fixture setup
----------------- Captured stdout call -----------------
id(customer)=4387931280
--------------- Captured stdout teardown ---------------
Fixture teardown
_________ test_customer_level_not_goes_below_0 _________
---------------- Captured stdout setup -----------------
Fixture setup
----------------- Captured stdout call -----------------
id(customer)=4387931472
--------------- Captured stdout teardown ---------------
Fixture teardown
================== 5 passed in 0.01s ===================
</pre>



<h2 class="wp-block-heading"><strong>Summary</strong></h2>



<p class="wp-block-paragraph">In this article, we looked at how to use Pytest fixtures to initialize your test environment. </p>



<p class="wp-block-paragraph">We first looked at how to create a Pytest fixture. You can create a fixture by creating a function with the <code>@pytest.fixture</code> decorator. You can use the same fixtures in multiple test files if you define them in the file <code>conftest.py</code>. You can also control the scope of fixtures by adding the scope argument. </p>



<p class="wp-block-paragraph">We then looked at how to add function arguments to fixtures by using the Factory pattern. </p>



<p class="wp-block-paragraph">Finally, we learned that adding teardown logic to fixtures is easy, which helps automate cleaning up the test environment.</p>



<p class="wp-block-paragraph">Fixtures in Pytest are very powerful, and it is critical to understand how to use them if you want to run your tests efficiently. The functionality in this article should be enough to get you started, but you can find more information on the <a href="https://docs.pytest.org/en/6.2.x/fixture.html" target="_blank" rel="noreferrer noopener">pytest website</a> that might help you solve your specific requirements.</p>



<p class="wp-block-paragraph">To boost your Python skills, feel free to join our free email academy:</p>



]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Pytest &#8211; How to Run Tests Efficiently</title>
		<link>https://blog.finxter.com/how-to-run-tests-efficiently-in-pytest/</link>
		
		<dc:creator><![CDATA[Mikio Oba]]></dc:creator>
		<pubDate>Mon, 27 Sep 2021 07:35:44 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[PyTest]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=35742</guid>

					<description><![CDATA[Pytest can offer various options that can increase your productivity when you test your code. Although Pytest provides sensible default settings, and you can use it out of the box, it will not offer a one-size-fits-all solution. As you continue writing tests, you will sooner or later start to look for ideas that can make ... <a title="Pytest &#8211; How to Run Tests Efficiently" class="read-more" href="https://blog.finxter.com/how-to-run-tests-efficiently-in-pytest/" aria-label="Read more about Pytest &#8211; How to Run Tests Efficiently">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><a href="https://blog.finxter.com/pytest-a-complete-overview/" target="_blank" rel="noreferrer noopener" title="Pytest – A Complete Overview">Pytest </a>can offer various options that can increase your productivity when you test your code. Although Pytest provides sensible default settings, and you can use it out of the box, it will not offer a one-size-fits-all solution. As you continue writing tests, you will sooner or later start to look for ideas that can make your test activities more efficient. This article will explain some of the basic options that you must know as a proficient Python developer.</p>



<p class="wp-block-paragraph">As you go through the article, you can also watch my explainer video:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="PyTest - Tips For Efficient Testing in Python" width="937" height="527" src="https://www.youtube.com/embed/m56askHJnYA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading"><strong>The Very Basics</strong></h2>



<p class="wp-block-paragraph">First of all, let&#8217;s have a quick look at what Pytest output looks like when it runs without any options.</p>



<p class="wp-block-paragraph">Just for explanation purposes, let&#8217;s use the following trivial tests.</p>



<p class="wp-block-paragraph"><code><strong>test_baic.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_that_always_passes():
    a = 1
    b = 1
    assert a == b

def test_that_always_fails():
    a = 1
    b = 2
    assert a == b
</pre>



<p class="wp-block-paragraph">When you run this test using Pytest, the output will look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .F                                    [100%]

======================== FAILURES =========================
_________________ test_that_always_fails __________________

    def test_that_always_fails():
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2

test_basic.py:9: AssertionError
================= short test summary info =================
FAILED test_basic.py::test_that_always_fails - assert 1 ...
=============== 1 failed, 1 passed in 0.02s ===============
</pre>



<p class="wp-block-paragraph">The first part is the header, which shows the <a href="https://blog.finxter.com/how-to-check-your-python-version/" target="_blank" rel="noreferrer noopener" title="HOW TO CHECK YOUR PYTHON VERSION">version </a>of the Python interpreter, pytest and where the root directory is.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
</pre>



<p class="wp-block-paragraph">Then, you see the test file name and its result. The dot means that the first function passed, and the following &#8220;F&#8221; means the second function failed.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">test_basic.py .F                                    [100%]</pre>



<p class="wp-block-paragraph">Then there is a summary section including the traceback, which I will explain more later in this article.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">======================== FAILURES =========================
_________________ test_that_always_fails __________________

    def test_that_always_fails():
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2

test_basic.py:9: AssertionError
================= short test summary info =================
FAILED test_basic.py::test_that_always_fails - assert 1 ...
=============== 1 failed, 1 passed in 0.02s ===============
</pre>



<h2 class="wp-block-heading"><strong>Run Tests Selectively in Pytest</strong></h2>



<p class="wp-block-paragraph">Pytest offers various ways to specify which test files to run. By default, Pytest runs the tests in the Python files whose names start with <code>test_</code> in the current <a href="https://blog.finxter.com/list-all-files-of-a-directory-python/" target="_blank" rel="noreferrer noopener" title="How Do I List All Files of a Directory in Python?">directory </a>and its subdirectories.&nbsp;</p>



<p class="wp-block-paragraph">So, if you only have a test file called <code>test_basic.py</code> in the current directory, you can run the command pytest, which will run the tests in this file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ ls
test_basic.py venv
$ pytest
=================== test session starts ===================
platform darwin -- Python Fest2
collected 1 item

test_basic.py .                                     [100%]

==================== 1 passed in 0.00s ====================
</pre>



<h3 class="wp-block-heading"><strong>Specify Pytest files or directory</strong></h3>



<p class="wp-block-paragraph">If you want to run tests in a specific file, you can specify the file name in the Pytest command, and Pytest will only run the specified file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 1 item

test_basic.py .                                     [100%]

==================== 1 passed in 0.00s ====================
</pre>



<p class="wp-block-paragraph">You can also specify a directory, and Pytest will run tests in the <a href="https://blog.finxter.com/how-to-open-python-py-files-in-idle-with-a-doubleclick/" target="_blank" rel="noreferrer noopener" title="How to Open Python .py Files in IDLE with a Doubleclick?">files </a>that reside in the specified directory and its subdirectories.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest /Users/mikio/pytest2
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 1 item

test_basic.py .                                     [100%]

==================== 1 passed in 0.00s ====================</pre>



<h3 class="wp-block-heading">Use the -k Option </h3>



<p class="wp-block-paragraph">You can also use the <code>-k</code> option to specify part of the file names to choose specific files selectively. The following example shows that I have <code>test_basic.py</code> and <code>test_advanced.py</code> in the current directory and another file <code>test_subdir.py</code> in the subdirectory called subdir. Pytest automatically runs all test files by default:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ tree -I venv
.
├── subdir
│   └── test_subdir.py
├── test_advanced.py
└── test_basic.py

$ pytest
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 3 items

test_advanced.py .                                  [ 33%]
test_basic.py .                                     [ 66%]
subdir/test_subdir.py .                             [100%]

==================== 3 passed in 0.01s ====================
</pre>



<p class="wp-block-paragraph">If you specify the option <code>-k basic</code>, Pytest will run only <code>test_basic.py</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -k basic
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 3 items / 2 deselected / 1 selected

test_basic.py .                                     [100%]

============= 1 passed, 2 deselected in 0.00s =============
</pre>



<p class="wp-block-paragraph">If you specify the option <code>-k subdir</code>, Pytest will run only <code>subdir/test_subdire.py</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -k subdir
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 3 items / 2 deselected / 1 selected

subdir/test_subdir.py .                             [100%]

============= 1 passed, 2 deselected in 0.00s =============
</pre>



<h2 class="wp-block-heading"><strong>Use Command Options in Pytest</strong></h2>



<p class="wp-block-paragraph">Pytest has various command-line options, which mainly control how Pytest runs and what information you see in the output. I will explain some of the most commonly used options in this section.</p>



<h3 class="wp-block-heading"><strong>Change the Verbosity of the Pytest Output</strong></h3>



<p class="wp-block-paragraph">You can make the Pytest output more verbose or less verbose, depending on your needs.</p>



<p class="wp-block-paragraph">Adding the <code>-v</code> option to the Pytest command allows you to see more information in the output.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -v test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py::test_that_always_passes PASSED       [ 50%]
test_basic.py::test_that_always_fails FAILED        [100%]

======================== FAILURES =========================
_________________ test_that_always_fails __________________

    def test_that_always_fails():
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2
E         +1
E         -2

test_basic.py:9: AssertionError
================= short test summary info =================
FAILED test_basic.py::test_that_always_fails - assert 1 ...
=============== 1 failed, 1 passed in 0.01s ===============
</pre>



<p class="wp-block-paragraph">Now you can see the file name (<code>test_basic.py</code>), the function names (<code>test_that_always_passes</code> and <code>test_that_always_fail</code>s ) and the results (PASSED and FAILED).</p>



<p class="wp-block-paragraph">On a side note, you might be used to command-line programs that show the <a href="https://blog.finxter.com/how-to-check-python-version-in-jupyter-notebook/" target="_blank" rel="noreferrer noopener" title="How to Check Python Version in Jupyter Notebook?">version </a>with the <code>-v</code> option. In Pytest, <code>--version</code> and <code>-V</code> are the options to display the version number of Pytest, so be careful not to get confused.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest --version
pytest 6.2.5
$ pytest -V
pytest 6.2.5
</pre>



<p class="wp-block-paragraph">If you want to see less information in the output, you can use the <code>-q</code> option, which only shows the test results and the traceback.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -q test_basic.py
.F                                                  [100%]
======================== FAILURES =========================
_________________ test_that_always_fails __________________

    def test_that_always_fails():
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2

test_basic.py:9: AssertionError
================= short test summary info =================
FAILED test_basic.py::test_that_always_fails - assert 1 ...
1 failed, 1 passed in 0.01s
</pre>



<p class="wp-block-paragraph">This option might be helpful if you run hundreds of tests regularly and want to see the summary of the test results. As shown above, if there were errors, you can still get information to find out what went wrong.</p>



<p class="wp-block-paragraph">If you don&#8217;t even want to show the traceback, you can use the <code>--no-summary</code> option to suppress it. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest --no-summary -q test_basic.py
.F                                                  [100%]
1 failed, 1 passed in 0.02s
</pre>



<h3 class="wp-block-heading"><strong>Show the Values in the Local Variables </strong></h3>



<p class="wp-block-paragraph">The <code>-l</code> option shows the actual values in the local variables in the tracebacks. So, you can see what values were used when tests failed.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -l test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .F                                    [100%]

======================== FAILURES =========================
_________________ test_that_always_fails __________________

    def test_that_always_fails():
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2

a          = 1
b          = 2

test_basic.py:9: AssertionError
================= short test summary info =================
FAILED test_basic.py::test_that_always_fails - assert 1 ...
=============== 1 failed, 1 passed in 0.02s ===============
</pre>



<p class="wp-block-paragraph">This example is too simple to see the benefits, but when you have more complex functions in the tests, it can save a lot of time to analyze the cause of test failures.</p>



<h3 class="wp-block-heading"><strong>Capture the standard output&nbsp;</strong></h3>



<p class="wp-block-paragraph">You can add a <a href="https://blog.finxter.com/python-print/" target="_blank" rel="noreferrer noopener" title="Python print()">print </a>statement in the test function. Pytest captures the output and shows it in the summary info section, but it might not be evident at first glance.</p>



<p class="wp-block-paragraph">Let&#8217;s add a print statement to the <code>test_basic.py</code> file like this:</p>



<p class="wp-block-paragraph"><code><strong>test_basic.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_that_always_passes():
    print("This test always passes.")
    a = 1
    b = 1
    assert a == b

def test_that_always_fails():
    print("This test always fails.")
    a = 1
    b = 2
    assert a == b
</pre>



<p class="wp-block-paragraph">Run Pytest.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_basic.py
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .F                                       [100%]

========================== FAILURES ==========================
___________________ test_that_always_fails ___________________

    def test_that_always_fails():
        print("This test always fails.")
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2

test_basic.py:11: AssertionError
-------------------- Captured stdout call --------------------
This test always fails.
================== short test summary info ===================
FAILED test_basic.py::test_that_always_fails - assert 1 == 2
================ 1 failed, 1 passed in 0.02s =================
</pre>



<p class="wp-block-paragraph">You can see that the section &#8220;Captured stdout call&#8221; has been added after the traceback in the &#8220;FAILURES&#8221; section, which includes the text in the print statement in the failed test.&nbsp;</p>



<p class="wp-block-paragraph">But you don&#8217;t see the print statement output from the passed test. To make the output easier to read, let&#8217;s remove the failing test from <code>test_basic.py</code>.</p>



<p class="wp-block-paragraph"><code><strong>test_basic.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_that_always_passes():
    print("This test always passes.")
    print("This is another line.")
    a = 1
    b = 1
    assert a == b
</pre>



<p class="wp-block-paragraph">When you run Pytest, you don&#8217;t see the print output by default.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_basic.py
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 1 item

test_basic.py .                                        [100%]

===================== 1 passed in 0.00s ======================</pre>



<p class="wp-block-paragraph">To show the print output for the passed tests, you can use the <code>-rP</code> option (summary info for the passed tests with output).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rP test_basic.py
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 1 item

test_basic.py .                                        [100%]

=========================== PASSES ===========================
__________________ test_that_always_passes ___________________
-------------------- Captured stdout call --------------------
This test always passes.
This is another line.
===================== 1 passed in 0.00s ======================
</pre>



<p class="wp-block-paragraph">Alternatively, you can use the <code>-rA</code> option (summary info for all tests).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rA test_basic.py
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 1 item

test_basic.py .                                        [100%]

=========================== PASSES ===========================
__________________ test_that_always_passes ___________________
-------------------- Captured stdout call --------------------
This test always passes.
This is another line.
================== short test summary info ===================
PASSED test_basic.py::test_that_always_passes
===================== 1 passed in 0.00s ======================
</pre>



<p class="wp-block-paragraph">You can now see the print output in the &#8220;Captured stdout call&#8221; section in both cases.</p>



<p class="wp-block-paragraph">You can also use the <code>-s</code> option, which tells Pytest not to capture the standard output. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rA -s test_basic.py
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 1 item

test_basic.py This test always passes.
This is another line.
.

=========================== PASSES ===========================
================== short test summary info ===================
PASSED test_basic.py::test_that_always_passes
===================== 1 passed in 0.00s ======================
</pre>



<p class="wp-block-paragraph">You can see that the &#8220;Captured stdout call&#8221; section does not appear in the summary info. Instead, you can see the <a href="https://blog.finxter.com/python-print/" target="_blank" rel="noreferrer noopener" title="Python print()">print </a>output right after the test file name. Although the output in the summary info, as shown previously, would look nicer, this option might serve the purpose in some cases. </p>



<h2 class="wp-block-heading"><strong>Use Markers to Select Test Functions</strong></h2>



<p class="wp-block-paragraph">Pytest has the built-in functionality to mark test functions. Markers are like tags, which you can use to categorize your test functions into different groups. </p>



<p class="wp-block-paragraph">Pytest provides various built-in markers, but two of the most commonly used markers are <code>skip</code> and <code>xfail</code>, so I will explain them first. Then I will explain custom markers.</p>



<h3 class="wp-block-heading">Skip Tests with @pytest.mark.skip<strong> </strong></h3>



<p class="wp-block-paragraph">You can use the skip marker when you want to skip specific tests (as the name suggests). Pytest will exclude the marked functions but will show that they have been skipped just as a reminder. You might want to use this marker, for example, when some external dependencies are temporarily unavailable, and the tests cannot pass until they become available again.&nbsp;</p>



<p class="wp-block-paragraph">To mark a function as skip, you can use the <code>@pytest.mark.skip</code> <a href="https://blog.finxter.com/closures-and-decorators-in-python/" target="_blank" rel="noreferrer noopener" title="Closures and Decorators in Python">decorator </a>as shown below. Be sure to <code><a href="https://blog.finxter.com/python-dot-relative-import/" target="_blank" rel="noreferrer noopener" title="Python “from . import x” — What’s the Purpose of the Dot?">import pytest</a></code> in the test file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest

@pytest.mark.skip
def test_that_should_be_skipped():
    a = 2
    b = 3
    assert a == b
</pre>



<p class="wp-block-paragraph">When you run Pytest with the <code>-v</code> option, you can see that the function <code>test_that_should_be_skipped</code> is shown as &#8220;SKIPPED&#8221;.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -v test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py::test_that_always_passes PASSED       [ 50%]
test_basic.py::test_that_should_be_skipped SKIPPED  [100%]

============== 1 passed, 1 skipped in 0.00s ===============
</pre>



<p class="wp-block-paragraph">If you run the <code>pytest</code> command without the <code>-v</code> option, you see &#8220;s&#8221; instead.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .s                                    [100%]

============== 1 passed, 1 skipped in 0.00s ===============
</pre>



<p class="wp-block-paragraph">If you add the <code>-rs</code> option (extra summary info of skipped functions), you can see an additional section about the skipped function.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rs test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .s                                    [100%]

================= short test summary info =================
SKIPPED [1] test_basic.py:8: unconditional skip
============== 1 passed, 1 skipped in 0.00s ===============
</pre>



<p class="wp-block-paragraph">You can optionally specify the reason to skip the function. If you set it, you can see it in the summary info. The following is an example to add a reason:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest

@pytest.mark.skip(reason="Just skipping...")
def test_that_should_be_skipped():
    a = 2
    b = 3
    assert a == b
</pre>



<p class="wp-block-paragraph">Then it is shown in the summary info:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rs test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .s                                    [100%]

================= short test summary info =================
SKIPPED [1] test_basic.py:8: Just skipping...
============== 1 passed, 1 skipped in 0.00s ===============
</pre>



<h3 class="wp-block-heading">Skip Tests Conditionally with @pytest.mark.skipif<strong> </strong></h3>



<p class="wp-block-paragraph">If you want to skip specific tests based on some conditions which you can programmatically determine, you can use <code>@pytest.mark.skipif</code>. For example, there might be a situation where you don&#8217;t want to run some tests on a particular operating system. You can get the platform name from <code>sys.platform</code> in Python, so you can use it with <code>@pytest.mark.skipif</code> to automatically skip platform-specific tests. </p>



<p class="wp-block-paragraph">Just for demonstration purposes, let&#8217;s say you don&#8217;t want to run the test on macOS, where <code>sys.platform</code> returns <code>darwin</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import sys
>>> sys.platform
'darwin'
</pre>



<p class="wp-block-paragraph">So, you can specify<code> skipif</code> as shown below to exclude the test function <code>test_that_should_be_skippe</code>d.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest
import sys

@pytest.mark.skipif(sys.platform=='darwin', reason=f'Skip on {sys.platform}')
def test_that_should_be_skipped():
    a = 2
    b = 3
    assert a == b
</pre>



<p class="wp-block-paragraph">Then, Pytest will skip it if you run it on macOS:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rs test_basic.py
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .s                                    [100%]

================= short test summary info =================
SKIPPED [1] test_basic.py:9: Skip on darwin
============== 1 passed, 1 skipped in 0.00s ===============
</pre>



<p class="wp-block-paragraph">You can find other return values of <code>sys.platform</code> on the Python documentation page.</p>



<h3 class="wp-block-heading">Skip Failing Tests with @pytest.mark.xfail<strong> </strong></h3>



<p class="wp-block-paragraph">You can skip tests when you already know they will fail but do not want to remove them from the test suite. For example, you might have a test that fails because of a bug in the code. You can mark it as <code>xfail</code> to acknowledge that it will fail until the fix is implemented, but keep it in the suite.</p>



<p class="wp-block-paragraph">Let&#8217;s add the <code>xfail</code> marker to the test function <code>test_that_fails</code> as shown below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest

@pytest.mark.xfail
def test_that_fails():
    a = 2
    b = 3
    assert a == b
</pre>



<p class="wp-block-paragraph">When you run Pytest, the result is shown as &#8220;XFAIL&#8221;.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -v test_basic.py
=================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py::test_that_always_passes PASSED        [ 50%]
test_basic.py::test_that_fails XFAIL                 [100%]

=============== 1 passed, 1 xfailed in 0.02s ===============
</pre>



<p class="wp-block-paragraph">You can use the <code>-rx</code> option (extra summary info of expected fail functions) to see additional information.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -rx test_basic.py
=================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py .x                                     [100%]

================= short test summary info ==================
XFAIL test_basic.py::test_that_fails
=============== 1 passed, 1 xfailed in 0.01s ===============
</pre>



<p class="wp-block-paragraph">To simulate the situation where the bug has been fixed, let&#8217;s update the test function so that the <a href="https://blog.finxter.com/pytest-a-complete-overview/" target="_blank" rel="noreferrer noopener" title="Pytest – A Complete Overview">assert </a>returns <code>True</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest

@pytest.mark.xfail
def test_that_fails():
    a = 2
    b = 2
    assert a == b
</pre>



<p class="wp-block-paragraph">When you run Pytest, the result is now shown as &#8220;XPASS&#8221; (unexpectedly passed). It is because the test now passes but is still marked as <code>xfail</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -v test_basic.py
=================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2
collected 2 items

test_basic.py::test_that_always_passes PASSED        [ 50%]
test_basic.py::test_that_fails XPASS                 [100%]

=============== 1 passed, 1 xpassed in 0.00s ===============
</pre>



<p class="wp-block-paragraph">This way, you can find out that the fix has been successfully implemented, and therefore you can now remove the <code>xfail</code> marker from the test function.</p>



<h3 class="wp-block-heading"><strong>Using Custom Markers</strong></h3>



<p class="wp-block-paragraph">You can define your custom markers and use them to categorize your test functions into different groups. </p>



<p class="wp-block-paragraph">The following example specifies a custom marker called &#8220;basic&#8221;.</p>



<p class="wp-block-paragraph"><code><strong>test_basic.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest

@pytest.mark.basic
def test_that_always_passes():
    a = 1
    b = 1
    assert a == b
</pre>



<p class="wp-block-paragraph">You can run Pytest with the option <code>-m basic</code> to select the functions that have the marker &#8220;basic&#8221;. In this example, there are three test files, as shown below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ tree -I venv
.
├── subdir
│   └── test_subdir.py
├── test_advanced.py
└── test_basic.py
</pre>



<p class="wp-block-paragraph">When running the <code>pytest</code> command with the option <code>-m basic</code>, only <code>test_basic.py</code> is run.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -m basic
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 3 items / 2 deselected / 1 selected

test_basic.py .                                     [100%]

...

======= 1 passed, 2 deselected, 1 warning in 0.01s ========
</pre>



<p class="wp-block-paragraph">If you run the same command, you will see the warning below. I will explain how to fix it later in this article.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">==================== warnings summary =====================
test_basic.py:3
  /Users/mikio/pytest2/test_basic.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.basic - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.basic

-- Docs: https://docs.pytest.org/en/stable/warnings.html
</pre>



<p class="wp-block-paragraph">You can also specify <code>-m "not basic"</code> to exclude this function. In this example, <code>test_basic.py</code> is excluded, and the other two test files are run.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -m "not basic"
=================== test session starts ===================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2
collected 3 items / 1 deselected / 2 selected

test_advanced.py .                                  [ 50%]
subdir/test_subdir.py .                             [100%]

...

======= 2 passed, 1 deselected, 1 warning in 0.01s ========
</pre>



<h2 class="wp-block-heading">Use the Configuration File pytest.ini<strong> </strong></h2>



<p class="wp-block-paragraph">As you see in the previous sections, you can specify various options when running the Pytest command. But if you run the same command regularly, it is not very convenient to manually type all the options each time. Pytest has a configuration file to keep all your settings, so let&#8217;s use it to save some typing.</p>



<p class="wp-block-paragraph">Pytest has various options to use configuration files, but we use <code>pytest.ini</code> in this article. </p>



<h3 class="wp-block-heading">Identify Which Configuration File is Being Used</h3>



<p class="wp-block-paragraph">First of all, it&#8217;s helpful to understand how Pytest determines which configuration file to use.</p>



<p class="wp-block-paragraph">Let&#8217;s create an empty file called <code>pytest.ini</code> in the current directory, as shown below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ touch pytest.ini 
</pre>



<p class="wp-block-paragraph">I am using the current directory <code>/Users/mikio/pytest2</code>, as shown below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pwd
/Users/mikio/pytest2
</pre>



<p class="wp-block-paragraph">Now run the Pytest command without any arguments.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/mikio/pytest2, configfile: pytest.ini
collected 3 items

test_advanced.py .                                     [ 33%]
test_basic.py .                                        [ 66%]
subdir/test_subdir.py .                                [100%]

===================== 3 passed in 0.01s ======================
</pre>



<p class="wp-block-paragraph">In the header of the output, you now see a line <code>rootdir: /Users/mikio/pytest2, configfile: pytest.ini</code>. It confirms that Pytest is using the configuration file <code>pytest.ini</code> in the current directory (<code>/Users/mikio/pytest2</code>).</p>



<p class="wp-block-paragraph">You can place <code>pytest.ini</code> in the current directory or any one of the parent directories. Pytest will find it and set the <code>rootdir</code> to the directory where <code>pytst.ini</code> exists. <code>pytest.ini</code> is often placed in the project root directory (or the repository&#8217;s root directory), but for demonstration purposes, let&#8217;s use <code>pytest.ini</code> in the current directory in this article.</p>



<h3 class="wp-block-heading"><strong>Save command line options in </strong>addopts</h3>



<p class="wp-block-paragraph">Let&#8217;s add the <code>addopts</code> option to <code>pytest.ini</code> as shown below. </p>



<p class="wp-block-paragraph"><code><strong>pytest.ini</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[pytest]
addopts = -v
</pre>



<p class="wp-block-paragraph">Then run the Pytest command without any arguments.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest
=================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini
collected 3 items

test_advanced.py::test_very_advanced_feature PASSED  [ 33%]
test_basic.py::test_that_always_passes PASSED        [ 66%]
subdir/test_subdir.py::test_in_a_sub_directory PASSED [100%]

==================== 3 passed in 0.01s =====================
</pre>



<p class="wp-block-paragraph">As you can see, the output is the same as when you specify the <code>-v</code> option. You can specify any other command-line options to <code>addopts</code>. </p>



<h3 class="wp-block-heading">Specify Test File Search Paths in testpaths</h3>



<p class="wp-block-paragraph">So far, we have been using test files in the current directory and its subdirectory. But as you develop the Python application, the project directory structure becomes more complex, and you would probably keep all your tests in a specific directory, such as tests. You can specify the directory in <code>pytest.ini</code> so Pytest doesn&#8217;t waste time searching test files in other directories.</p>



<p class="wp-block-paragraph">Let&#8217;s create a subdirectory called <code>tests</code> in the current directory and move the test files in the current directory to tests as shown below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ mkdir tests
$ mv test*.py tests/
</pre>



<p class="wp-block-paragraph">So, the current directory structure looks like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ tree -I venv
.
├── pytest.ini
├── subdir
│   └── test_subdir.py
└── tests
    ├── test_advanced.py
    └── test_basic.py
</pre>



<p class="wp-block-paragraph">Then, add the <code>testpaths</code> option in <code>pytest.ini</code> like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[pytest]
addopts = -v
testpaths = tests 
</pre>



<p class="wp-block-paragraph">Now, run the Pytest command without any arguments from the current directory.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest
=================== test session starts ====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini, testpaths: tests
collected 2 items

tests/test_advanced.py::test_very_advanced_feature PASSED [ 50%]
tests/test_basic.py::test_that_always_passes PASSED  [100%]

==================== 2 passed in 0.01s =====================
</pre>



<p class="wp-block-paragraph">You can see the following:</p>



<ul class="wp-block-list"><li><code>testpaths</code> is set to tests in the header.</li><li>Collected two items in the header confirms the correct number of test files. </li><li>The results are shown only for the test files in tests, ignoring subdir. </li></ul>



<h3 class="wp-block-heading">Specify Test File and Function Name Prefix in python_files and python_functions</h3>



<p class="wp-block-paragraph">So far, we have been using test files with the prefix<code> test_</code>, such as <code>test_basic.py</code>, but this prefix is configurable.</p>



<p class="wp-block-paragraph">Let&#8217;s add the <code>python_files</code> option to <code>pytest.ini</code> as shown below:</p>



<p class="wp-block-paragraph"><code><strong>pytest.ini</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[pytest]
addopts = -v
testpaths = tests
python_files = a_*.py
</pre>



<p class="wp-block-paragraph">Pytest will now search files with the prefix <code>a_</code> and consider them as test files.</p>



<p class="wp-block-paragraph">Let&#8217;s rename one of the test files in the subdirectory tests.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ mv tests/test_basic.py tests/a_basic.py
$ tree -I venv
.
├── pytest.ini
├── subdir
│   └── test_subdir.py
└── tests
    ├── a_basic.py
    └── test_advanced.py
</pre>



<p class="wp-block-paragraph">Now run Pytest without any arguments.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini, testpaths: tests
collected 1 item

tests/a_basic.py::test_that_always_passes PASSED       [100%]

===================== 1 passed in 0.00s ======================
</pre>



<p class="wp-block-paragraph">You can see that Pytest only found and ran one test file, <code>tests/a_basic.py</code>.</p>



<p class="wp-block-paragraph">Likewise, the function name prefix is configurable. Let&#8217;s add another test function called <code>my_test_that_always_passes</code> to the file <code>tests/a_basic.py</code> as shown below:</p>



<p class="wp-block-paragraph"><code><strong>tests/a_basic.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def test_that_always_passes():
    a = 1
    b = 1
    assert a == b

def my_test_that_always_passes():
    a = 2
    b = 2
    assert a == b
</pre>



<p class="wp-block-paragraph">When you run Pytest, you can see that it does not pick up the newly added function.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini, testpaths: tests
collected 1 item

tests/a_basic.py::test_that_always_passes PASSED       [100%]

===================== 1 passed in 0.00s ======================
</pre>



<p class="wp-block-paragraph">Now, let’s add the <code>python_functions</code> option to <code>pytest.ini</code>.</p>



<p class="wp-block-paragraph"><code><strong>pytest.ini</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[pytest]
addopts = -v
testpaths = tests
python_files = a_*.py
python_functions = my_*
</pre>



<p class="wp-block-paragraph">When you rerun Pytest, you can see that it now runs only the newly added function.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini, testpaths: tests
collected 1 item

tests/a_basic.py::my_test_that_always_passes PASSED    [100%]

===================== 1 passed in 0.00s ======================
</pre>



<h3 class="wp-block-heading">Register Custom Markers</h3>



<p class="wp-block-paragraph">When you add custom markers in the previous section, you see &#8220;<code>PytestUnknownMarkWarning</code>&#8221; in the output. You can eliminate this warning by registering your custom markers in <code>pytest.ini</code>.</p>



<p class="wp-block-paragraph">For demonstration purposes, remove the <code>python_files</code> and <code>python_functions</code> options in <code>pytest.ini</code>.</p>



<p class="wp-block-paragraph"><code><strong>pytest.ini</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[pytest]
addopts = -v
testpaths = tests
</pre>



<p class="wp-block-paragraph">Then rename the test file name prefix back to <code>test_</code> so that the test file names look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ tree -I venv
.
├── pytest.ini
├── subdir
│   └── test_subdir.py
└── tests
    ├── test_advanced.py
    └── test_basic.py
</pre>



<p class="wp-block-paragraph">Let&#8217;s add a custom marker called basic to the test file <code>tests/test_basic.py</code> like this:</p>



<p class="wp-block-paragraph"><code><strong>tests/test_basic.py</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pytest

@pytest.mark.basic
def test_that_always_passes():
    a = 1
    b = 1
    assert a == b
</pre>



<p class="wp-block-paragraph">As we saw in the previous section, running Pytest with <code>-m basic</code> will pick up only the functions marked as basic.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -m basic
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini, testpaths: tests
collected 2 items / 1 deselected / 1 selected

tests/test_basic.py::test_that_always_passes PASSED    [100%]

====================== warnings summary ======================
tests/test_basic.py:3
  /Users/mikio/pytest2/tests/test_basic.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.basic - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.basic

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========= 1 passed, 1 deselected, 1 warning in 0.00s =========
</pre>



<p class="wp-block-paragraph">You can also see the warning &#8220;<code>PytestUnknownMarkWarning</code>&#8220;. It means that the marker &#8220;<code>basic</code>&#8221; is not registered, so Pytest is asking whether it is intentional or it might be a typo. In this case, we are sure that this is not a typo, so let&#8217;s register this marker in <code>pytest.ini</code> to remove this warning.</p>



<p class="wp-block-paragraph">Add the following option markers to <code>pytest.ini</code>:</p>



<p class="wp-block-paragraph"><code><strong>pytest.ini</strong></code></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[pytest]
addopts = -v
testpaths = tests
markers =
    basic: marks basic tests
</pre>



<p class="wp-block-paragraph">The text after the colon (:) is an optional description of the marker. It is generally a good practice to add explanations so that other people (and often for future yourself) can understand what it is and why it is necessary.</p>



<p class="wp-block-paragraph">Then, rerun Pytest.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pytest -m basic
==================== test session starts =====================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/mikio/pytest2/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mikio/pytest2, configfile: pytest.ini, testpaths: tests
collected 2 items / 1 deselected / 1 selected

tests/test_basic.py::test_that_always_passes PASSED    [100%]

============== 1 passed, 1 deselected in 0.00s ===============
</pre>



<p class="wp-block-paragraph">You will no longer see the warning because the marker is already registered.</p>



<p class="wp-block-paragraph">When running the <code>pytset</code> command with <code>--markers</code>, you can see all the markers, including the custom marker you just registered.</p>



<h2 class="wp-block-heading"><strong>Summary</strong></h2>



<p class="wp-block-paragraph">In this article, I explained some of the options that Pytest offers to make your test activities more efficient.</p>



<p class="wp-block-paragraph">First, we looked at how you can selectively run tests. You can specify the file name or directory name in the command line arguments. You can also use the <code>-k</code> option to identify a part of the file names.</p>



<p class="wp-block-paragraph">Then, we looked at the pytest command options to change the output. You can use the <code>-v</code> option to make it more verbose and the <code>-q</code> option to make it less verbose. You can also use the <code>-l</code> option to see the values in the local variables in the traceback. You can also capture the standard output and display it in the result. </p>



<p class="wp-block-paragraph">One of the powerful options we looked at next is markers. You can skip specific tests by adding <code>@pytest.mark.skip</code> or <code>@pytest.mark.skipif</code>. You can mark failing tests to <code>@pytest.mark.xfail</code>. You can also use your custom markers to group tests and selectively run them.</p>



<p class="wp-block-paragraph">Finally, we looked at the configuration file <code>pytest.ini</code>, where you can save various settings.</p>



<p class="wp-block-paragraph">You can find more options in the <a href="https://docs.pytest.org/en/6.2.x/customize.html">Pytest documentation</a>. I hope you find some of the options in this article helpful and continue exploring other options that suit your needs.&nbsp;</p>



]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-11-21 10:53:54 by W3 Total Cache
-->