PyCharm 2017.3 Help

Testing JavaScript with JSTestDriver

This feature is supported in the Professional edition only.

PyCharm supports integration with the JSTestDriver test framework.

Before you start

  1. Configure your testing framework as PyCharm JavaScript library.
  2. Make sure the JSTestDriver repository plugin is installed and enabled. The plugin is not bundled with PyCharm, but it can be installed from the JetBrains plugin repository as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.

    Through this plugin, PyCharm provides the JSTestDriver server and the assertion framework. During the test creation, the plugin detects the unit testing framework and suggests the Add <test framework> support intention action.

Configuring a testing framework in a project

  1. Download the framework of your choice and configure it as a PyCharm JavaScript library.
  2. Do one of the following:
    • Add the project folder to the library scope.
    • Enable the framework support on-the-fly during test creation using the Add <test framework> support intention action.
    • To use Jasmine, add jasmine-jstd-adapter to the configuration file.

      Open jsTestDriver.conf and type the following code in it:

      load:lib/jasmine/jasmine.jslib/jasmine-jstd-adapter/JasmineAdapter.js

Creating a test runner configuration file manually

A test runner configuration file defines the test and production files to load the loading order. PyCharm treats any file with the extension *.jstd or *.conf as a test runner configuration file.

To create a configuration file

  1. In the Project tree, select the parent folder of the production and test folders, and choose New | File on the context menu.
  2. In the New File dialog box, that opens, type the name of the configuration file with the extension jstd or conf.
  3. Open the new file in the editor and specify the full path to the current folder and the paths to the files to load relative to it. Use wildcards in file name patterns.
  4. Complete the configuration file using YAML, see description of test runner configuration files.

Running tests

With PyCharm, you can quickly run a single JSTestDriver test right from the editor or create a run/debug configuration to execute some or all of your tests.

To run a single test from the editor
Click run.png or rerun.png in the left gutter and choose Run <test_name> from the pop-up list. You can also see whether a test has passed or failed right in the editor, thanks to the test status icons ws_icon_test_status.png in the left gutter.

To create a JSTestDriver run configuration

  1. Open the Run/Debug Configuration dialog box (Run | Edit Configurations on the main menu).
  2. Click /help/img/idea/2017.3/new.png on the toolbar and select JSTestDriver from the list. The Run/Debug Configuration: JSTestDriver dialog box opens.
  3. Specify the tests to run, the path to the configuration file, and the activities to perform before test execution.

To start the PyCharm default JSTestDriver test server

  1. Make sure you have at least one configuration file in your project.
  2. Open the JSTestDriver Server tool window (View | Tool Windows | JSTestDriver Server), and click run.png on the toolbar.

To stop the server when you are through with unit testing
Click the Stop the local server toolbar button /help/img/idea/2017.3/stop.gif.

To capture a browser

  1. Start the JSTestDriver Server if it is not running yet and then switch to the JSTestDriver Server tool window.
  2. To start a local browser with the Remote Console of the JSTestDriver, do one of the following:
    • Click the icon that indicates the browser of your choice.
    • If the browser is already opened, copy the contents of the Capture a browser using the URL read-only field and paste the URL in the address bar.
    In either case, the icon that indicates the chosen browser becomes active.
  3. Switch to the JSTestDriver Server tool window and click the icon that indicates the browser you just opened. PyCharm displays a message informing you that it is ready for executing tests.

To run tests via a run configuration

  1. Select the JSTestDriver run/debug configuration from the list on the main toolbar and click /help/img/idea/2017.3/run.png to the right of the list.
  2. Monitor test execution in the Test Runner tab of the Run tool window as described in Monitoring and Managing Tests.

Navigation

With PyCharm, you can jump between a file and the related test file. Navigation from a test result in the Test Runner Tab to the test is also supported.

To jump between a file and the related test file
Open the file in the editor and choose Go To | Test or Go To | Test Subject on the context menu, or just press Ctrl+Shift+T.

To jump from a test result to the test
Select the test name in the Test Runner tab and choose Jump to Source on the context menu.

ws_test_jump_to_test
The test file opens in the editor with the cursor placed at the test definition.

Debugging tests

A debugging session for JSTestDriver tests is started only through a run/debug configuration.

To start debugging tests

  1. Create a JSTestDriver run/debug configuration as described above.
  2. Select the JSTestDriver run/debug configuration from the list on the main toolbar and click /help/img/idea/2017.3/debug.png to the right of the list.
  3. In the Debug tool window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, etc.

Monitoring code coverage

With PyCharm, you can also monitor how much of your code is covered with Karma tests. PyCharm displays this statistics in a dedicated tool window and marks covered and uncovered lines visually right in the editor.

To launch tests with coverage

  1. Create a JSTestDriver run/debug configuration as described above.
  2. Start the JSTestDriver server and capture a browser to run the tests in.
  3. Select the JSTestDriver run/debug configuration from the list on the main toolbar and click /help/img/idea/2017.3/runWithCoverage.png to the right of the list.
  4. Monitor the code coverage in the Coverage tool window.

To exclude files from coverage analysis

  1. Create a JSTestDriver run/debug configuration as described above.
  2. In the Coverage tab, specify the paths to the files to exclude.

See Also

Last modified: 15 September 2017