{"id":11467,"date":"2019-11-04T05:00:49","date_gmt":"2019-11-04T10:00:49","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=11467"},"modified":"2020-07-10T10:16:26","modified_gmt":"2020-07-10T14:16:26","slug":"pytest-command-line-options","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/pytest-command-line-options\/","title":{"rendered":"Exploring pytest command line options"},"content":{"rendered":"<p>This post will serve as a quick tutorial for using pytest command-line options. Anyone who is new to pytest will benefit from this article.<\/p>\n<p>Pytest comes with many useful command-line options right out of the box. When I started learning pytest, I had to search through a lot of material available on the official <a href=\"https:\/\/docs.pytest.org\/en\/latest\/index.html\">pytest website<\/a> as well as other Internet resources. I hope this tutorial will help any newbie, who wants to start using pytest commands effectively.<\/p>\n<hr \/>\n<h3>Installation:<\/h3>\n<p><strong>1<\/strong>. Run the following command in your command line:<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/pytest_installation-2.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/pytest_installation-2.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>2<\/strong>. Check the installed version.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/Installed_version.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/Installed_version.png\" alt=\"\" \/><\/a><\/p>\n<hr \/>\n<h3>Pytest command line options<\/h3>\n<p>Note: <a href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">Qxf2 Page object model<\/a> has been used to demonstrate the examples in the post.<\/p>\n<p><strong>1. python -m pytest<\/strong><\/p>\n<p>You can invoke testing through python interpreter from the command line.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/1.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>2<\/strong>.\u00a0<strong>pytest&#8211;version<\/strong><\/p>\n<p>This gives you a version of the pytest module.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/2.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/2.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>3<\/strong>. <strong>pytest &#8211;fixtures<\/strong><\/p>\n<p>This will give you available <a href=\"https:\/\/docs.pytest.org\/en\/latest\/fixture.html\">fixtures<\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/3.png\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/3.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/3.1.png\" data-rel=\"lightbox-image-5\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/3.1.png\" alt=\"\" width=\"984\" height=\"293\" \/><\/a><\/p>\n<p><strong>4<\/strong>. <strong>pytest -h or pytest &#8211;help<\/strong><\/p>\n<p>This shows help on command line and config-line options<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/4.1.png\" data-rel=\"lightbox-image-6\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/4.1.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/4.2.png\" data-rel=\"lightbox-image-7\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/4.2.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>5<\/strong>. <strong>pytest -x<\/strong><\/p>\n<p>This command stops execution after the first failure.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/5.png\" data-rel=\"lightbox-image-8\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/5.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/5.2.png\" data-rel=\"lightbox-image-9\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/5.2.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>6<\/strong>.\u00a0<strong>pytest &#8211;maxfail=n<\/strong><\/p>\n<p>This command stops execution after nth failure. n can be any number such as 1.2.3<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/6.1.png\" data-rel=\"lightbox-image-10\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/6.1.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/6.2.png\" data-rel=\"lightbox-image-11\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/6.2.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>7<\/strong>.<strong> pytest module name<\/strong><\/p>\n<p>This command helps to run tests by specifying the module name.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/7.1.png\" data-rel=\"lightbox-image-12\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/7.1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>8<\/strong>. <strong>pytest directory_name\/<\/strong><\/p>\n<p>This command runs the tests from the directory<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/8.png\" data-rel=\"lightbox-image-13\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/8.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>9<\/strong>. <strong>pytest -k &#8220;keyword expression&#8221;<\/strong><\/p>\n<p>This command runs the test based on the keyword expression. e.g. in the below example, we will be running only API tests not bitcoin tests from our suite.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/9.png\" data-rel=\"lightbox-image-14\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/9.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/9.1.png\" data-rel=\"lightbox-image-15\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/9.1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>10<\/strong>. <strong>Detailed summary report options<\/strong><\/p>\n<p>The -r flag can be used to display a short summary report at the end of the test. Various options are discussed in the below section.<\/p>\n<p><strong>10.1<\/strong>\u00a0\u00a0<strong>pytest -rA<\/strong><\/p>\n<p>This gives output of all tests<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.1.png\" data-rel=\"lightbox-image-16\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.1.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.1.1.png\" data-rel=\"lightbox-image-17\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.1.1.png\" alt=\"\" \/><\/a><\/p>\n<p>Below is the full list of characters available.<\/p>\n<p>f- failed<\/p>\n<p>E- error<\/p>\n<p>s- skipped<\/p>\n<p>x- xfailed<\/p>\n<p>X- xpassed<\/p>\n<p>p- passed<\/p>\n<p>P-passed with output<\/p>\n<p>a- all except pP<\/p>\n<p>A- all<\/p>\n<p>More than one character can be used so in the below example failed and skipped tests can be seen after running following command<\/p>\n<p><strong>10.2<\/strong>\u00a0\u00a0<strong>pytest -rfs<\/strong><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.2.png\" data-rel=\"lightbox-image-18\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.2.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.2.1.png\" data-rel=\"lightbox-image-19\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/10.2.1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>11<\/strong>. <strong>pytest &#8211;ignore=ignorepaths during tests<\/strong><\/p>\n<p>You can specify module, directory or tests to ignore the tests.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/11.png\" data-rel=\"lightbox-image-20\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/11.png\" alt=\"\" \/><\/a><\/p>\n<p>In the below screen you can see the test has been ignored from the execution.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/11.1.png\" data-rel=\"lightbox-image-21\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/11.1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>12<\/strong>.\u00a0<strong>pytest &#8211;collect-only<\/strong><\/p>\n<p>You can peep into the collections without actually executing the tests.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/12.png\" data-rel=\"lightbox-image-22\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/12.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>13<\/strong>. <strong>pytest -m &lt;markername&gt;<\/strong><\/p>\n<p><a href=\"https:\/\/docs.pytest.org\/en\/latest\/mark.html\">Markers<\/a> that have been defined in the test_example_table.py file and pytest module imported in the test file as shown below.<\/p>\n<p>Only the tests defined by the marker will run after this command.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/13.png\" data-rel=\"lightbox-image-23\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/13.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/13.1.png\" data-rel=\"lightbox-image-24\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/13.1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>14<\/strong>. <strong>pytest &#8211;junitxml=&#8221;xmlfilename&#8221;<\/strong><\/p>\n<p>After running this command pytest will generate XML report<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/14.png\" data-rel=\"lightbox-image-25\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/14.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/14.1.png\" data-rel=\"lightbox-image-26\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/14.1.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>15<\/strong>. <strong>py.test -s<\/strong><\/p>\n<p>Users can influence output capturing mechanisms from the command line, where -s option disables all capturing.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/15.png\" data-rel=\"lightbox-image-27\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/15.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>16<\/strong>. <strong>py.test -E<\/strong><\/p>\n<p>Users can create custom marker and command-line options before running tests following additions to be done in the conftest.py file and test file<\/p>\n<p>In the conftest.py file<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.1.png\" data-rel=\"lightbox-image-28\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.1.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.2.png\" data-rel=\"lightbox-image-29\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.2.png\" alt=\"\" \/><\/a><\/p>\n<p><span class=\"p\">In the test_example_form.py<\/span><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.3.png\" data-rel=\"lightbox-image-30\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.3.png\" alt=\"\" \/><\/a><\/p>\n<p>Then run the test it will give output as shown in the below screen.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.4.png\" data-rel=\"lightbox-image-31\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/10\/16.4.png\" alt=\"\" \/><\/a><\/p>\n<p><strong>17. pytest -n NUM<\/strong><\/p>\n<p>This will speed up tests by sending tests to multiple CPUs. This can be useful in longer running tests or tests requiring a lot of I\/O. This can lead to considerable speedups.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/11\/17.png\" data-rel=\"lightbox-image-32\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/11\/17.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/11\/17.1-1.png\" data-rel=\"lightbox-image-33\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/11\/17.1.png\" alt=\"\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>I hope you have liked the blog. There are many more useful pytest commands that can be used by testers or developers for their testing. This post captures the most commonly used commands in our experience. A complete list of commands will be available at <a href=\"https:\/\/docs.pytest.org\/en\/latest\/index.html\">pytest<\/a> documentation.<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>This post will serve as a quick tutorial for using pytest command-line options. Anyone who is new to pytest will benefit from this article. Pytest comes with many useful command-line options right out of the box. When I started learning pytest, I had to search through a lot of material available on the official pytest website as well as other [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-11467","post","type-post","status-publish","format-standard","hentry","category-pytest"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=11467"}],"version-history":[{"count":79,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11467\/revisions"}],"predecessor-version":[{"id":11667,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11467\/revisions\/11667"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=11467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=11467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=11467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}