Development Environment Setup with PyCharm

Quick summary for Python experts:

  1. Install stb-tester into a virtualenv with python stbt_rig.py setup This enables code completion / IntelliSense, but it doesn’t allow running the test scripts locally (see the note in the first section below for details).
  2. Configure your IDE’s “Save” button to run python stbt_rig.py -v snapshot. This syncs your local code to the Stb-tester Portal on a special “snapshot” branch — without affecting your local git checkout.
  3. Configure your IDE to sync & run a single test remotely (on your Stb-tester Node) when you click “Run Test” from the IDE.

For more details, read on.

IntelliSense for stbt APIs

Create a virtual environment and install stb-tester into it:

python stbt_rig.py setup

Note

This package doesn’t include video-capture infrastructure and other complex dependencies, so it won’t allow you to run your test scripts locally (on your PC). It does enable your IDE’s “IntelliSense” or “Code Insight” features such as code completion, type inference, inline documentation, and linting.

Sync the code from your IDE to the Stb-tester portal

Bind the “Save” button of your IDE to run python -m stbt_rig -v snapshot after saving. This will push your local code to the Stb-tester Portal on a special branch called “<your username>’s snapshot” that is only visible to you. It doesn’t affect your local git checkout (it doesn’t make any commits on your checked-out branch) so after you have tested your changes & got them working, you still need to do “git commit” and “git push” to make it official. See Development Workflow.

Before setting this up in your IDE, make sure that you can run python -m stbt_rig -v snapshot from your terminal, to debug any authentication issues. See stbt_rig installation and stbt_rig authentication here.

In Preferences > Plugins install the File Watchers plugin.

Now in Preferences > Tools > File Watchers add a new watcher with the following settings:

  • Name: stbt_rig snapshot
  • File type: Any
  • Scope: Project Files
  • Program: python
  • Arguments: -m stbt_rig -v snapshot
  • Working directory: path to your test-pack checkout
../_images/file-watchers-configuration.png

Run a test from your IDE

PyCharm can detect the test functions in a file and show a small button to run the test:

../_images/pycharm-run-test.png

With the configuration below, clicking the button will sync your local code-changes and run the test remotely (on your Stb-tester Node). This is useful during test-development, when you are writing or debugging the test.

Before setting this up in your IDE, make sure that you can run python -m stbt_rig -v snapshot from your terminal, to debug any authentication issues. See stbt_rig installation and stbt_rig authentication here.

First go to File > Settings and select Tools > Python Integrated Tools. Under Testing: Default test runner select pytest. Click OK.

Next, go to Run > Edit Configurations and select Templates > Python tests > pytest. Fill in the following settings:

  • Target: Module name
  • Additional Arguments: -p stbt_rig --node-id=stb-tester-123456789abc
  • Add content roots to PYTHONPATH: True

(Change “stb-tester-123456789abc” to the actual ID of your Stb-tester Node.)

Note that you aren’t creating a new configuration (don’t click the little “+” icon on the left-hand pane). You are editing the Template from which PyCharm creates new configurations on the fly for each test.

The configuration should now look like:

../_images/configuration.png

You can now run the current test with Ctrl-Shift-F10 or by clicking the green play button in the margin on the left and selecting “Run ‘pytest for …’”:

../_images/pycharm-run-test.png

Troubleshooting

If any of the steps above don’t work, make sure that you have the latest version of stbt_rig.

Make sure that you can run python -m stbt_rig -v snapshot from your terminal. This will prompt for your access token and save it into your operating system’s keyring for future invocations. See stbt_rig CLI.