PyCharm is a Python IDE, released by JetBrains. I quite enjoy using it and, as I've recently started contributing some tests to Mozilla's Web QA department (mozwebqa), I wanted to use it to interactively debug some mozwebqa tests. This turned out to be a lot trickier than I had imagined, so I am documenting the steps to do so via this post.
This post assumes you have a project open in PyCharm for one of the mozwebqa projects. I am going to use marketplace-tests for this example. As I am on a Mac the instructions and screen shots will be OS X specific, but I imagine they will translate pretty closely to Windows.
Step 1 - Configure py.test as your default test runner
- Open up the Preferences... dialog and choose Python Integrated Tools under Project Settings.
- Choose py.test as the Default test runner.
- Click OK.
Step 2 - Set default run configuration parameters for your project
- Type ctrl + alt + R to open the Run configuration selector.
- Type 0, or choose the first option (Edit configurations...) from the select list.
- In the Run dialog, expand Defaults > Python's test and choose py.test.
- Make sure the Python interpreter that you want to use for this project is selected for Python interpreter. If you are using virtualenv you may have to configure a new Python interpreter for your virtualenv. More on that in a separate post.
- Choose the root of your project for Working directory.
- Click Apply, then Close.
Step 3 - Create a pytest.ini file in the root of your project
This is necessary to pass command line options to py.test. It would be nice if there were a way to do this via the IDE, but I was not able to do it. If anyone knows how, or can figure out how, to do that I'd love to know. I was able to pass a single option to the command line from PyCharm, but could not get it to work with multiple options.
- Create a file called pytest.ini in the root of your project.
- Add the command line options you need into that file under the [pytest] section. See below for an example from marketplace-tests.
- Click OK.
addopts = --driver=firefox --credentials=mine/credentials.yaml --destructive
Step 4 - Create a copy of credentials.yaml in a personal folder
You are going to have to edit credentials.yaml to place some actual credentials in it, so in order to not have it overwritten each time you do a pull, you should put a copy that you are going to use somewhere else. I tend to create a /mine folder under the project root and place it there, but you can put it anywhere you like. You will notice that the command line option above uses that /mine folder to locate the credentials file.
Step 5 - Run your tests
With a file that contains tests open in an editor window, type ctrl + shift + R and PyCharm will run all of the tests in the file. If you wish to run just one test, type ctrl + alt + R, followed by 0 to open the Edit configurations... dialog and then place the name of your test in the Keywords input. Click Run.