{"id":11572,"date":"2019-12-23T05:18:12","date_gmt":"2019-12-23T10:18:12","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=11572"},"modified":"2019-12-23T05:27:19","modified_gmt":"2019-12-23T10:27:19","slug":"run-python-behave-from-visual-studio-code","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/run-python-behave-from-visual-studio-code\/","title":{"rendered":"Run python behave from Visual Studio Code"},"content":{"rendered":"<p>I was working at a client that uses behave and was primarily designed to work on Linux systems. It has several configuration variables stored as environmental variables. I tried running the behave tests on Windows using Visual Studio Code since I wanted debugging functionality. I documented the steps so that it will help anyone who wants to try the same.<\/p>\n<h4>Prerequisite<\/h4>\n<p>Before getting started, please install the required packages needed for an automation environment. The primary ones would be <a href=\"https:\/\/selenium.dev\/downloads\/\">Selenium,<\/a> <a href=\"https:\/\/www.python.org\/downloads\/\">Python,<\/a> <a href=\"https:\/\/pypi.org\/project\/pip\/\">pip,<\/a>\u00a0<a href=\"https:\/\/pypi.org\/project\/behave\/\">behave,<\/a>\u00a0<a href=\"https:\/\/chromedriver.chromium.org\/downloads\">chromedriver\u00a0<\/a>(or any browser) on your Windows machine.<\/p>\n<h4><strong><br \/>\n<\/strong>Setting up configuration in Visual Studio Code<\/h4>\n<ol>\n<li><span><span>In Visual Studio Code, open the folder you want to work on.<\/span><\/span><\/li>\n<li><span>Click on Debug Icon\u00a0<\/span><span><span><span>and then on &#8216;Configure or Fix launch.json&#8217; icon.\u00a0The Select Environment dropdown gets opened.\u00a0Select Node.js.<\/span><\/span><\/span>&nbsp;\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Select_environment.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11987 \" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Select_environment.png\" alt=\"Select_environment\" width=\"465\" height=\"101\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Select_environment.png 756w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Select_environment-300x65.png 300w\" sizes=\"auto, (max-width: 465px) 100vw, 465px\" \/><\/a>&nbsp;<\/li>\n<li>The launch.json will be displayed. This file is used to configure the debugger in Visual Studio Code.<\/li>\n<li><span>Add the following configuration details to the launch.json<\/span>\n<pre>{\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Use\u00a0IntelliSense\u00a0to\u00a0learn\u00a0about\u00a0possible\u00a0attributes.\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Hover\u00a0to\u00a0view\u00a0descriptions\u00a0of\u00a0existing\u00a0attributes.\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0For\u00a0more\u00a0information,\u00a0visit:\u00a0<a href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=830387\">https:\/\/go.microsoft.com\/fwlink\/?linkid=830387<\/a>\r\n\u00a0\u00a0\u00a0\u00a0\"version\":\u00a0\"0.2.0\",\r\n\u00a0\u00a0\u00a0\u00a0\"configurations\":\u00a0[\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"name\":\u00a0\"Python:\u00a0Behave\u00a0current\u00a0file\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"type\":\u00a0\"python\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"request\":\u00a0\"launch\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"module\":\u00a0\"behave\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"console\":\u00a0\"integratedTerminal\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"env\":\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"BASE_DIR\":\u00a0\"&lt;your_automation_repository&gt;\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"args\":[\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"--no-capture\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"--no-capture-stderr\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"${file}\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0]\r\n}<\/pre>\n<p><span><\/span><strong>Note<\/strong><span>: In BASE_DIR we have set the path to automation repository. So, we will have the flexibility to change it to the directory we are working on.<br \/>\n<\/span><\/li>\n<li><span>The final file should look like this:<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Final_launch.json_.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11988\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Final_launch.json_.png\" alt=\"Final launch.json\" width=\"547\" height=\"382\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Final_launch.json_.png 887w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Final_launch.json_-300x210.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Final_launch.json_-768x537.png 768w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/a><br \/>\n<\/span><\/li>\n<li>Set the integrated terminal as gitbash so that tests will be run through it.<br \/>\n<span>Do Ctrl + Shift + P to open the settings editor. Type settings and clicking on the JSON option.<\/span><br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11989\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor.png\" alt=\"Settings editor\" width=\"262\" height=\"59\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor.png 333w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor-300x68.png 300w\" sizes=\"auto, (max-width: 262px) 100vw, 262px\" \/><\/a><br \/>\nProvide the path to the git bash in the &#8220;terminal.integrated.shell.windows&#8221; option<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11991\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_.png\" alt=\"\" width=\"543\" height=\"84\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_.png 791w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_-300x46.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_-768x118.png 768w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/a><\/li>\n<li><span><span>Set bashrc.<br \/>\n<\/span><\/span><\/p>\n<pre>if [ -z $BASE_DIR ]; then\r\n BASE_DIR=&lt;path of your automation repository&gt;\r\nfi\r\ncd $BASE_DIR<\/pre>\n<p>The BASE_DIR specified here is a default directory which will be used in case the directory is not set in VSCode and prevent the script from failing.<\/li>\n<li>If you are using a virtual environment, set the path to it in\u00a0<span>settings.json.<br \/>\nDo Ctrl + Shift + P to open the settings editor. Type settings and clicking on the JSON option.<\/span><br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor-1.png\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11990\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor-1.png\" alt=\"Settings editor\" width=\"257\" height=\"58\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor-1.png 333w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settins_editor-1-300x68.png 300w\" sizes=\"auto, (max-width: 257px) 100vw, 257px\" \/><\/a><br \/>\nSet the path of the virtual environment <span>under &#8220;python.pythonPath&#8221;<\/span><br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11991\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_.png\" alt=\"\" width=\"543\" height=\"84\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_.png 791w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_-300x46.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Settings.json_-768x118.png 768w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/a><\/li>\n<\/ol>\n<p>That is it. Open the feature file you want to run and click on<span>\u00a0the Run icon\u00a0<\/span><span class=\"confluence-embedded-file-wrapper confluence-embedded-manual-size\">to start running your test in debug mode.\u00a0<span>There are various option such as Pause, Step Over, Step Into etc. while running the test.\u00a0\u00a0<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Debug_icons.png\" data-rel=\"lightbox-image-6\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11995 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/12\/Debug_icons.png\" alt=\"Debug icons\" width=\"215\" height=\"33\" \/><\/a><br \/>\n<\/span><br \/>\n<\/span><\/p>\n<h4>References<\/h4>\n<ol>\n<li>https:\/\/code.visualstudio.com\/docs\/editor\/variables-reference<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I was working at a client that uses behave and was primarily designed to work on Linux systems. It has several configuration variables stored as environmental variables. I tried running the behave tests on Windows using Visual Studio Code since I wanted debugging functionality. I documented the steps so that it will help anyone who wants to try the same. [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11572","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11572","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\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=11572"}],"version-history":[{"count":27,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11572\/revisions"}],"predecessor-version":[{"id":12000,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11572\/revisions\/12000"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=11572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=11572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=11572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}