{"id":8537,"date":"2021-02-24T14:53:35","date_gmt":"2021-02-24T09:23:35","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8537"},"modified":"2021-02-24T14:53:36","modified_gmt":"2021-02-24T09:23:36","slug":"pytest-tutorial","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/pytest-tutorial\/","title":{"rendered":"PyTest Tutorial"},"content":{"rendered":"\n<p>In software houses when many people get together to create a single software product, they are working on small modules. All modules need to be verified weather they are working fine or not. There is a team in every department that tests every modules called <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-software-qa-and-why-should-you-care\/\" class=\"rank-math-link\">software quality assurance<\/a>. In Python there is a build in library called Pytest for writing tests. Let\u2019s take a look at how this library works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Pytest<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install pygame<\/pre>\n\n\n\n<p>To test weather pygame is installed or not use the following command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">py.test -h<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/mX7jAPTKacq8DzbYYZlMRrspNqMJo5UZ8L4LPztGW1GLclaCSvNSjestb1qIfNZ5KmhqFlDsUvPcRduQIdqJ0sdqx6qURxNQI8YfnHrLbc9tn7MgguUb03AizJ452KCsIqhtquI\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s write some code.<\/p>\n\n\n\n<p>Put the following code in a file named testfile.py<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pytest\ndef test_method1():\n\u00a0 \u00a0 x=2\n\u00a0 \u00a0 y=4\n\u00a0 \u00a0 assert x+1 == y,\"test passeed\"\n\u00a0 \u00a0 assert x == y,\"test failed\"<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/cMQ-O9Tl61pB7kqo2m8awXw13HRcy2qEslUsrUXu7JZe4-yVRldVv92hw9uZ9EkN99KqtiUVo0toHUbg6SZOOcyzWhpjLXDuzpWPP3hdtNv5DyFIRzRAVikZd2tZZt6oKqTP5zo\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>To run the above code use the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pytest testfile.py<\/pre>\n\n\n\n<p>The following will be the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/1TRtJje1MIeDa4vEfYZ3vzh9_ufGo0l0Fl1aKJ8vZxnr4kQagFNOSBqL-sClWsmNcH_o4bi8eDCuPGZsY8IXR_IxslRNpf4ywAGJcyefiKZrWdLhuK9RQm-rQMvRz0dImLOALP8\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>This shows that 1 test failed and one test is passed. As you can 5 is not equal to 6.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Assertions in PyTest<\/strong><\/h2>\n\n\n\n<p>Pytest assertions are checks that return either True or False status. In Python Pytest, if an assertion fails in a test method, then that method execution is stopped there. The remaining <a href=\"https:\/\/www.python.org\/\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">python code<\/a> in that test method is not executed, and Pytest assertions will continue with the next test method.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Pytest identify files<\/h2>\n\n\n\n<p>Pytest search for files that starts with _test or test_. We can also declare with our unique name like above then we have to explicitly mention the file name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running Multiple files<\/h2>\n\n\n\n<p>If we follow the namming convention then we can run multiple files using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pytest<\/pre>\n\n\n\n<p>Otherwise we need to explicitly define the file name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pytest testfile.py<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Running Pytest using Markers<\/h2>\n\n\n\n<p>We can call different methods using markers. For example there are two test methods written inside a test file. We can call them separately using command line.<\/p>\n\n\n\n<p>Take a look at the code below to define different markers.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pytest\n\n@pytest.mark.one\ndef test_func1():\nx = 2\ny = 9\nassert x == y\n\n@pytest.mark.two\ndef test_func1():\nx = \"h2k\"\ny = \"h2k\"\nassert x == y<\/pre>\n\n\n\n<p>Let\u2019s run the marker two first. We need to run the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pytest -m two<\/pre>\n\n\n\n<p>The following will be the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/_T0UFencI23TIuxFfY13KW95O3kdZlwbgXVnUVzeafs2SyZA7zPl6DcnM54nC9b2DEz2_VZr5Y_EJR8aNBRbxnqBdg-gMKlToUi9CKaxdX3FBSV0eo5FwYmSFy0ZMhnHRSqzol4\" alt=\"\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In software houses when many people get together to create a single software product, they are working on small modules. All modules need to be verified weather they are working fine or not. There is a team in every department that tests every modules called software quality assurance. In Python there is a build in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8541,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8537","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8537"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8537\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8541"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}