{"id":4458,"date":"2016-07-21T01:04:46","date_gmt":"2016-07-21T05:04:46","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=4458"},"modified":"2024-10-30T03:23:28","modified_gmt":"2024-10-30T07:23:28","slug":"xdist-run-tests-parallel-pytest","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/xdist-run-tests-parallel-pytest\/","title":{"rendered":"Pytest-xdist: Run tests in parallel"},"content":{"rendered":"<p><strong>Problem:<\/strong> How do you run tests in parallel with pytest?<\/p>\n<p>We have <a href=\"https:\/\/qxf2.com\/blog\/modify-python-gui-automation-use-pytest\/\">begun using pytest<\/a> as our test runner at most of our clients. At one client, our tests are triggered by <a href=\"https:\/\/circleci.com\/\">CircleCI<\/a> and run on <a href=\"https:\/\/www.browserstack.com\/start\">BrowserStack<\/a>. Our GUI automation suite grew in size and was starting to take a long time to run against a vast combination of OSes and browser versions. Luckily, our client purchased 5 BrowserStack licenses for us. We wanted to run tests in parallel on the 5 channel BrowserStack from CircleCI using pytest. We searched on Google, but we didn\u2019t get much information about running pytest in parallel. So this post is to help testers facing a similar issue. We will show you how to use <a href=\"https:\/\/pypi.python.org\/pypi\/pytest-xdist\">pytest-xdist<\/a> to run pytests in parallel.<\/p>\n<h3>Setup:<\/h3>\n<p>To run pytests in parallel, we need to install pytest-xdist plugin. To install x-dist plugin use:<\/p>\n<pre lang=\"python\">pip install pytest-xdist<\/pre>\n<h3>Command to run pytest in parallel:<\/h3>\n<p>The command to tell pytest to run your tests is parallel depends on the OS.<\/p>\n<p>A)<strong>Windows:<\/strong><br \/>\nTo run tests in parallel on Windows, use:<\/p>\n<pre lang=\"python\">py.test \u2013n NUM<\/pre>\n<p>Here NUM = Number of parallel tests you want running at one time<\/p>\n<p>In our case, we had 5 BrowserStack channels. So our command ended up being:<\/p>\n<pre lang=\"python\"> py.test \u2013n 5<\/pre>\n<p>B)<strong>Linux:<\/strong><\/p>\n<p>Our CircleCI container runs Ubuntu. To run tests in parallel from on Linux, use:<\/p>\n<pre lang=\"python\">py.test -d --tx NUM*popen\/\/python=python2.7<\/pre>\n<p>Here NUM = Number of parallel tests you want running at one time<\/p>\n<p>In our case, our command ended up being:<\/p>\n<pre lang=\"python\">py.test -d --tx 5*popen\/\/python=python2.7<\/pre>\n<p>We liked pytest-xdist. It helped us distribute and run our tests in parallel in a straightforward way. We hope this helps you too.<\/p>\n<p>This article was made possible by Qxf2&#8217;s commitment to sharing our work with the larger testing community. Qxf2 specializes in QA for early-stage products, understanding the unique demands startups face in testing. Our offerings include automation, consulting, and on-demand expertise tailored to startup needs. Explore our <strong><a href=\"https:\/\/qxf2.com\/?utm_source=pytest-xdist&#038;utm_medium=click&#038;utm_campaign=From%20blog\">quality assurance solutions for startups<\/a><\/strong> that deliver real value right from the start.<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>Problem: How do you run tests in parallel with pytest? We have begun using pytest as our test runner at most of our clients. At one client, our tests are triggered by CircleCI and run on BrowserStack. Our GUI automation suite grew in size and was starting to take a long time to run against a vast combination of OSes [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,93,52,107,18],"tags":[],"class_list":["post-4458","post","type-post","status-publish","format-standard","hentry","category-browserstack","category-circleci","category-continuous-integration","category-pytest","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4458","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\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=4458"}],"version-history":[{"count":18,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4458\/revisions"}],"predecessor-version":[{"id":22995,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4458\/revisions\/22995"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=4458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=4458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=4458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}